The purpose of this article is to deploy microservices based on docker-compose. We won’t talk about the implementation details of Docker and SpringCloud, so you should have a basic understanding of Docker and SpringCloud before reading this article.

Simple single-node service architecture deployment

1. Simple architecture diagram

  • nacosAli open source service discovery
  • fp-gatewayGateway service
  • fp-apiAPI service
  • fp-userCustomer service

2. Create a service image

As can be seen from the simple architecture diagram, fp-gateway, fp-api and fp-user are the applications we built based on SpringCloud and registered with nacos. For nacOS, we can use the official image directly, and the remaining three applications need to write the image.

2.1 PREPARATION of FP service image

Dockerfile: All three services use this
FROM java:8
Add the run script to the container
COPY startup.sh /shell/startup.sh
# Grant run permission
RUN chmod +x /shell/startup.sh
Set the container time zone to east 8
ENV TZ=Asia/Shanghai 
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
Create jar package directory
RUN mkdir jar
# Mount jar directory
VOLUME ["/jar"]
Run the script
CMD [ "sh"."/shell/startup.sh" ]
Copy the code
Startup. sh: The difference is only server_name
#! /bin/bash
Gateway -> API /user
server_name="fp-gateway"
kill -s 9 `ps -aux | grep $server_name | awk '{print $2}'`
Switch to jar directory to start, destination configuration external
cd /jar
# start
java -jar -Duser.timezone=GMT+08 -XX:+HeapDumpOnOutOfMemoryError -Xms512m -Xmx512m $server_name.jar
Copy the code

2.2 Starting to Build an Image

Get the images and shells ready for the three services

Start building the three service images
docker build -t fp-api:v1 ./fp-api/
docker build -t fp-gateway:v1 ./fp-gateway/
docker build -t fp-user:v1 ./fp-user/
Copy the code
# When docker images is executed, the image is successfully constructed
docker images
Copy the code

3 write docker – compose

3.1 installation docker – compose

curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
Copy the code
docker-compose -version
Copy the code

3.2 write a standalone. Yml

version: "3.5"
services:
  fp-api:
    image: fp-api:v1
    container_name: fp-api
    volumes:
      - /docker/fp/services/fp-api:/jar
    ports:
      - "10001:10001"  
    depends_on:
      - fp-gateway  
  fp-user:
    image: fp-user:v1
    container_name: fp-user
    volumes:
      - /docker/fp/services/fp-user:/jar
    ports:
      - "10002:10002"
    depends_on:
      - fp-gateway  
  fp-gateway:
    image: fp-gateway:v1
    container_name: fp-gateway
    volumes:
      - /docker/fp/services/fp-gateway:/jar
    ports:
      - "10003:10003"
Copy the code

3.3 Packing JAR and NACOS initial configuration

Nacos is a piece of service discovery that each team chooses differently, and it’s flexible, so skip the deployment process.

Fp / ├ ─ ─ builds │ ├ ─ ─ fp - API │ │ ├ ─ ─ Dockerfile │ │ └ ─ ─ startup. Sh │ ├ ─ ─ fp - gateway │ │ ├ ─ ─ Dockerfile │ │ └ ─ ─ startup. Sh │ └ ─ ─ fp - user │ ├ ─ ─ Dockerfile │ └ ─ ─ startup. Sh ├ ─ ─ services │ ├ ─ ─ fp - API │ │ ├ ─ ─ the config │ │ │ └ ─ ─ application. Yml │ │ └ ─ ─ fp - API. Jar │ ├ ─ ─ fp - gateway │ │ ├ ─ ─ the config │ │ │ └ ─ ─ application. Yml │ │ └ ─ ─ fp - gateway. Jar │ └ ─ ─ fp - user │ ├ ─ ─ The config │ │ └ ─ ─ application. Yml │ └ ─ ─ fp - user. Jar └ ─ ─ yaml └ ─ ─ standalone. YmlCopy the code

4 Starting services

docker-compose -f ./yaml/standalone.yml up -d
Copy the code

4.1 After the container is successfully started, check whether the container is started

docker ps
Copy the code

4.2 Accessing the FP-API Service through the FP-gateway

The FP-USER service interface is invoked in the FP-API service.

@RestController
public class UserController {

    @GetMapping("user")
    public String user() {
        return "welcome to user app."; }}Copy the code
@FeignClient(value = "footprint-user")
public interface UserFeign {

    @GetMapping("user")
    public String user();

}
Copy the code
@RestController
public class ApiController {

    @Autowired
    private UserFeign userFeign;

    @GetMapping("test")
    public String test() {
        returnuserFeign.user(); }}Copy the code

Access the API service Test interface

The curl http://192.168.0.4:10003/fp/api/test
Copy the code