Springcloud Alibaba microservice practice 15 – container deployment of springcloud

Time:2021-5-7

Spring cloud Alibaba has written 16 articles, and the basic framework has been basically completed. Business related logic needs to be sorted and reformed according to the business of the project itself.
Today will be the last article in this series – spring cloud containerized deployment (the last article in theory, I don’t rule out that related components will be upgraded later)

See if you should order one after you have written so many copiesI’m lookingWhat about it?
Springcloud Alibaba microservice practice 15 - container deployment of springcloud

Enable docker remote access

Since I am developing on windows, I don’t have docker installed, so I need to find a server with docker installed and start remote access. Students using mac, please ignore.

  • Open docker profile
    vi /lib/systemd/system/docker.service
  • Open 2376 port
    findExecStart=/usr/bin/dockerdIn the line, add after-H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sockThe effect of the modification is as follows:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock
  • Restart docker service
systemctl daemon-reload
systemctl restart docker.service
  • Using netstat to view ports
    netstat -nptl
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud
  • Visit / info to make sure the port is open normally
    curl http://127.0.0.1:2376/info
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud

Building docker image with docker made plugin

Before you start packing, be sure tobomandcommonModule executionmvn clean installCommand, otherwise the packaging will not succeed.

Anyone who has read this series of articles must also know the related instructions of docker and docker compose, which will not be explained here.
Springcloud Alibaba microservice practice 15 - container deployment of springcloud

  • The docker Maven plugin plug-in is introduced into the modules that need to build components
<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.2</version>
    <configuration>
        <imageName>${project.artifactId}</imageName>
        <imageTags>
            <imageTag>latest</imageTag>
        </imageTags>
        <!-- Specify dockerfile path -- >
        <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
        <dockerHost>http://xxx.xx.xx.xx:2376</dockerHost>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <!--${project.basedir}/target-->
                <directory>${project.build.directory}</directory>
                <!--${project.artifactId}-${project.version}-->
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>

The port here is consistent with the previous open port.

  • At the end of the modulesrc/main/dockerCreate dockerfile file under directory
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD cloud-gateway-1.0.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Each module needs to be modified by itselfADDinstructions

  • Execute the following command under the module to build the docker image
mvn clean package docker:build -DskipTests

The implementation effect is as follows:
Springcloud Alibaba microservice practice 15 - container deployment of springcloud

  • After construction, log in to the server to view the docker image
    docker images
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud
  • Start the image and take it with you--rmInstructions facilitate the deletion of containers.
docker run -d -p 5000:5000  --rm auth-service
docker run -d -p 8010:8010  --rm account-service
docker run -d -p 8090:8090  --rm cloud-gateway
  • Check whether it starts normally
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud
  • Testing with postman
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud

Normal service response!

Starting with docker compose

  • establishcloud-service.ymlFile layout image
version: "3"
services:
  auth-service:
    container_name: auth-service
    image: auth-service:latest
    ports:
      - "5000:5000"
    restart: always

  cloud-gateway:
    container_name: cloud-gateway
    image: cloud-gateway:latest
    ports:
      - "8090:8090"
    restart: always

  account-service:
    container_name: account-service
    image: account-service:latest
    ports:
      - "8010:8010"
    restart: always
  • Upload the file to the server and use the following script to start the service
    docker-compose -f cloud-service up
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud
  • Testing with postman
    Springcloud Alibaba microservice practice 15 - container deployment of springcloud
    Normal service response!

The container deployment of spring cloud is relatively simple, don’t you want to give it a try?