This is the 17th day of my participation in the First Challenge 2022

1, the preface

Those who have developed and built SpringCloud microservice projects know that it is quite troublesome to deploy a large number of services. The emergence of Docker has largely solved this problem. However, when deploying the services through Docker, it will be troublesome to start and stop the services due to the large number of services. Docker-compose is composed for docker-compose. Docker-compose is composed for docker-compose. Docker-compose is composed for docker-compose.

Based on springCloud micro-service, it will build a registry, configuration center and gateway service (three containers) running on Docker Compose, so as to master the basic use of Docker Compose.

2. Service preparation

Registry, configuration center, gateway services, using previously developed services, which are not specified here, the code can be obtained from Github, github.com/xcbeyond/sp… . Since code on Github is often optimized, a copy of this demo code has been packaged for download so that it can be consistent with what is mentioned in this article.

The services involved are as follows:

The serial number The project name Packaged jar name instructions
1 springCloudEureka The register center – 1.0. The jar The registry
2 springCloudConfigServer Config – center – 1.0. The jar Configuration center
3 springCloudZuulGateway Gateway – 1.0. The jar Gateway service

The structure of yml project involving service, dockerfile and docker-compose is as follows:

(List only the file locations of primary interest)

├─ ├─ ├─ SRC │ ├─ main │ ├─ The docker │ └ ─ ─ Dockerfile registry Dockerfile ├ ─ ─ springCloudConfigServer configuration center │ └ ─ ─ the SRC │ └ ─ ─ the main │ └ ─ ─ docker │ └ ─ ─ Dockerfile configuration center Dockerfile └ ─ ─ springCloudZuulGateway gateway service └ ─ ─ the SRC └ ─ ─ the main └ ─ ─ docker └ ─ ─ Dockerfile Dockerfile gateway serviceCopy the code

The startup sequence of the three services is as follows:

SpringCloudEureka — > springCloudConfigServer — > springCloudZuulGateway

3, Dockerfile definition

Dockerfile scripts that write docker images for each service are managed under the item \ SRC \main\docker directory. For a detailed description of all the directives in Dockerfile, please refer to the detailed description of the definition of the Dockerfile file. This article only describes the directives used in this example.

(1) springCloudEureka

FROM Java :8 RUN mkdir /microservice WORKDIR /microservice ADD/regist-center-1.0.jar /microservice/ EXPOSE 8761 ENTRYPOINT [" Java ", "-djava.security.egd =file:/dev/./urandom", "-jar", "/microservice/register-center-1.0.jar"]Copy the code

Description:

  • FROM java:8

FROM Specifies the base image to be based on. Must be in the first line, which in this case is based on Java 8 as the base image.

  • RUN mkdir /microservice

RUN Runs the shell command. In this case, it is used to create the MicroService directory.

  • WORKDIR /microservice

WORKDIR Configures the working directory for subsequent RUN, CMD, and ENTRYPOINT commands.

  • The ADD/register – center – 1.0. Jar/microservice /

ADD copies the package at the specified location to the specified location in the container. In this case, the SpringCloudeureka-1.0.jar located in the same directory as the Dockerfile is copied to the /microservice/ directory of the container.

  • EXPOSE 8761

EXPOSE the port of the container for external use. In this case, it is a port that registers only the central service springCloudEureka and ensures external access.

  • ENTRYPOINT [” Java “, “- Djava. Security. Or egd = file: / dev /. / urandom”, “- the jar,”…]

ENTRYPOINT configures the commands only needed after the container is started. In this case, the Java program is launched.

(2) springCloudConfigServer:

FROM Java :8 RUN mkdir /microservice WORKDIR /microservice ADD /config-center-1.0.jar /microservice/ EXPOSE 8888 ENTRYPOINT [" Java ", "-djava.security.egd =file:/dev/./urandom", "-jar", "/microservice/config-center-1.0.jar"]Copy the code

(3) springCloudZuulGateway:

FROM Java :8 RUN mkdir /microservice WORKDIR /microservice ADD /gateway-1.0.jar /microservice/ EXPOSE 8111 ENTRYPOINT [" Java ", "- Djava. Security. Or egd = file: / dev /. / urandom", "- the jar", "/ microservice/gateway - 1.0. The jar"]Copy the code

Docker-comemage. yml definition

This script defines the start and stop of the above three services and the communication between the services. The content of the script is as follows:

version: '2'
services:
     # Registry
     # Custom service name
     register-center: 
       # mirror name
       image: register-center:v1
       # container name
       container_name: register-center
       If the container is down, it restarts automatically
       restart: always
       Mount a directory or an existing data volume container. The format is host directory: container directory
       volumes:
            - $PWD/register - center/register - the center - 1.0. The jar: / microservice /
       # Mapping port. The format is host port: container port
       ports:
            - "8761:8761"
       The command executed after the container is started
       command: java -Djava.security.egd=file:/dev/./urandom -jar Microservice/register center - 1.0. The jar
       
     # Configuration center
     config-center: 
       image: config-center:v1
       container_name: config-center
       restart: always
       volumes:
            - $PWD/config - center/config - center - 1.0. The jar: / microservice /
       ports:
            - "8888:8888"
       command: java -Djava.security.egd=file:/dev/./urandom -jar / microservice/config - center - 1.0. The jar
       depends_on:
            - register-center 
       links:
            - register-center:register-center
     
     # Gateway service
     gateway: 
       image: gateway:v1
       container_name: gateway
       restart: always
       volumes:
            - $PWD/gateway/gateway - 1.0. The jar: / microservice /
       ports:
            - "8111:8111"
       command: java -Djava.security.egd=file:/dev/./urandom -jar / microservice/gateway - 1.0. The jar
       depends_on:
            - register-center
            - config-center
       links:
            - register-center:register-center
            - config-center:config-center
Copy the code

5. Compile and run

I uploaded each packaged service JAR package, Dockerfile script and docker-comemess. yml to the Docker server. I stored them in the directory with the following structure, depending on my personal habits.

[docker@docker ~]$ cd microservice/
[docker@docker microservice]$ ll
total 4
drwxrwxr-x. 2 docker docker   51 Sep 20 11:09 config-center
-rwxrwxrwx. 1 docker docker   1764 Sep 20 14:18 docker-compose.yml
drwxrwxr-x. 2 docker docker   45 Sep 20 11:10 gateway
drwxrwxr-x. 2 docker docker   53 Sep 20 11:10 register-center
Copy the code

(1) Compile the image

Register-center

[docker@docker register-center]$docker build-t register-center: v1. Sending build context to docker daemon 44.31MB Step 1/6 : FROM java:8 ---> d23bdf5b1b1b Step 2/6 : RUN mkdir /microservice ---> Runninginfcf4af033ac5 ---> c0cf16047e86 Removing intermediate container fcf4af033ac5 Step 3/6 : WORKDIR /microservice ---> cc1fae4f23dc Removing intermediate container d6fd78d760a9 Step 4/6 : ADD /register-center-1.0.jar /microservice/ --> EE8d0DB56843 Removing Intermediate Container DD5b20C5e72c Step 5/6: EXPOSE 8761 ---> Runningin46ae28623bd1 ---> d02692f9c49c Removing intermediate container 46ae28623bd1 Step 6/6 : Egd =file:/dev/./ urandom-jar /microservice/register-center-1.0.jar --> Runningincf63f2ca01af ---> 51431eb470e5 Removing intermediate container cf63f2ca01af Successfully built 51431eb470e5 Successfully  tagged register-center:v1Copy the code

Config-center:

[docker@docker config-center]$docker build -t config-center:v1. Sending build context to docker daemon 44.07MB Step 1/6 : FROM java:8 ---> d23bdf5b1b1b Step 2/6 : RUN mkdir /microservice ---> Using cache ---> c0cf16047e86 Step 3/6 : WORKDIR /microservice ---> Using cache ---> cc1fae4f23dc Step 4/6 : Jar /microservice/ --> 643C9fC7e5a5 Removing Intermediate container 45b51e525c0e Step 5/6: EXPOSE 8888 ---> Running in 602770ffb673 ---> c75a04c7c5a5 Removing intermediate container 602770ffb673 Step 6/6 : Egd =file:/dev/./ urandom-jar /microservice/config-center-1.0.jar --> Running in e84912bfe2a5 ---> 159990521122 Removing intermediate container e84912bfe2a5 Successfully built 159990521122 Successfully  tagged config-center:v1Copy the code

Gateway:

[docker@docker gateway]$docker build-t gateway:v1. Sending build context to docker daemon 42.44MB Step 1/6: FROM java:8 ---> d23bdf5b1b1b Step 2/6 : RUN mkdir /microservice ---> Using cache ---> c0cf16047e86 Step 3/6 : WORKDIR /microservice ---> Using cache ---> cc1fae4f23dc Step 4/6 : ADD /gateway-1.0.jar /microservice/ --> 920D5C366B08 Removing Intermediate Container 86346DA6CB03 Step 5/6: EXPOSE 8111 ---> Running in 0b306fc30056 ---> 91efbbdb7011 Removing intermediate container 0b306fc30056 Step 6/6 : Egd =file:/dev/./ urandom-jar /microservice/gateway-1.0.jar --> Running in 77ac20EC51C0 ---> 1308e926e769 Removing intermediate container 77ac20ec51c0 Successfully built 1308e926e769 Successfully tagged gateway:v1Copy the code

(2) Operation

In the docker-compose. Yml directory, run the docker-compose up -d command to start the background.

[docker@docker microservice]$ ll
total 4
drwxrwxr-x. 2 docker docker   51 Sep 20 11:09 config-center
-rwxrwxrwx. 1 docker docker 1764 Sep 20 14:18 docker-compose.yml
drwxrwxr-x. 2 docker docker   45 Sep 20 11:10 gateway
drwxrwxr-x. 2 docker docker   53 Sep 20 11:10 register-center
[docker@docker microservice]$ docker-compose up -d
Copy the code

At this point, multiple services in the microservice project are started through Docker-compose.

Docker-compose docker-compose docker-compose docker-compose docker-compose docker-compose docker-compose

1) stop

  • Docker-compose up -d starts up in the background
  • Docker-compose stop XXX stops a service
  • Docker-compose stop stops all services
  • Docker-compose start XXX starts a service
  • Docker-compose restart XXX restarts a service

2) other

  • Docker-compose config views all configurations in the current YML file
  • Docker-compose rm XXX delete a service for docker-compose
  • Docker-compose rm delete all services (container)