Building a system of microservices with Docker

The final system is expected to be built in the following shape:

In the preceding diagram, everything resides inside of Docker as containers. Each small rectangle is a Docker container with a process of its own. There is one Redis container that acts as the in-memory data store for replication to have multiple instances of the Taxi Service Apps and Taxi Booking Service Apps. 

All the apps are running behind an NGinx Load Balancer so that it can handle the load by distributing traffic among different apps.

The following docker-compose.yml file is used to define the preceding layout in Docker's understandable configuration:

version: '2'
services:
taxi-service-app:
build: ./spring-boot-2-taxi-service/target
networks:
- backend
depends_on:
- db

taxi-booking-service-app:
build: ./spring-boot-2-taxi-booking-service/target
networks:
- backend
depends_on:
- db

db:
hostname: redis
image: "redis:alpine"
ports:
- "6379:6379"
networks:
- backend

nginx-lb:
container_name: nginx-lb
image: nginx:1.13
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
networks:
- backend
depends_on:
- taxi-service-app
- taxi-booking-service-app


networks:
backend:
driver: bridge

The services section lists all the services required to be defined, while the networks section lists the networks connecting those services together. The db service, in this case, the Redis data store, is the first service to be initialized, as both the taxi-service-app and taxi-booking-service-app services depend on it. The db service will be listening on port 6379 both inside and outside of the network backend. This service is created using an already existing Docker image from the Docker repository named redis:alpine.

After that, either the taxi-service-app or taxi-booking-service-app service can be initialized. It will be created from the Docker image that was created in the previous step and placed inside the respective /target directory. Both of these services depend on db as mentioned earlier and connect to the network backend.

Finally, the nginx-lb service will be initialized when both the taxi-service-app and taxi-booking-service-app services are up and running. This service will expose port 80 for non-secure connections and 443 for secure connections. This service will also use the app.conf configuration file available in the /nginx/conf.d directory, which is inside the /spring-boot-2-taxi project. 

The app.conf file does the following:

server {
listen 80;
charset utf-8;
access_log off;

location /taxibookings {
proxy_pass http://taxi-booking-service-app:9090;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /taxis {
proxy_pass http://taxi-service-app:8080;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

It will listen on port 80, as mentioned in the preceding code explanation, and will route any requests that come into http://<host>/taxibookings to http://taxi-booking-service-app:9090 (9090 is the port for the Taxi Bookings service and is configured with server.port in application.properties file for that project).

It will route any requests that come into http://<host>/taxis to http://taxi-service-app:8080. Also, it will send some headers along with the forwarded requests. 

This is all there for the composing of the final system.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.238.134