I have written 16 articles in SpringCloud Alibaba series, and the basic framework has been basically completed. The business-related logic still needs to be sorted out and transformed according to the business of the project itself. Today will be the last installment in this series – SpringCloud container deployment (theoretically the last installment, and there will be updates to related components later)
In the name of writing so much should not be a point to read?
Enable Remote access to Docker
Since I was developing on Windows and did not install Docker, I need to find a server with Docker installed and enable remote access. If you use a MAC, please ignore it.
-
Open the docker vi/lib/systemd configuration file/system/docker. Service
-
Open port 2376 find ExecStart = / usr/bin/dockerd line, behind an extra – H TCP: / / 0.0.0.0:2376 – H Unix: / / / var/run/docker. The sock, modify the finish effect is as follows:
ExecStart = / usr/bin/dockerd -h fd: / / -- containerd = / run/containerd containerd. The sock - H TCP: / / 0.0.0.0:2376 - H unix:///var/run/docker.sockCopy the code
- Restart the Docker service
systemctl daemon-reload
systemctl restart docker.service
Copy the code
- Use netstat to view ports
netstat -nptl
- Visit /info to make sure the port is open properly
The curl http://127.0.0.1:2376/info
Docker-maven-plugin builds a Docker image
Be sure to pack before you startbom
andcommon
Module performsmvn clean install
Command, or the package will not succeed.
Docker and Docker-compose directives are already well known to those of you who have read these articles.
- Introduce the Docker-Maven-plugin in modules where components need to be built
<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>
<! 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>
Copy the code
This port is the same as the port opened earlier.
- In the module
src/main/docker
Create a Dockerfile file in the directory
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADDCloud - gateway - 1.0.0. Jar app. The jarRUN sh -c 'touch /app.jar'
ENTRYPOINT ["java"."-Djava.security.egd=file:/dev/./urandom"."-jar"."/app.jar"]
Copy the code
Each module needs to modify the ADD directive itself
- Run the following command in the module to build the Docker image
mvn clean package docker:build -DskipTests
Copy the code
The execution effect is as follows:
- Log in to the server to view the Docker image after construction
docker images
- Start the mirror, bring
--rm
The directive facilitates the removal 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
Copy the code
- Check whether the startup is normal
- Use Postman for testing
Service is responding!
Start with Docker-compose
- create
cloud-service.yml
File choreography mirroring
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
Copy the code
- Upload the file to the server and start the service using the following script
docker-compose -f cloud-service up
- Use Postman for testing
Service is responding!
The container deployment of SpringCloud is relatively simple. Why not give it a try?