[SpringCloud | Linux] CentOS7 deploys SpringCloud microservices

Table of Contents

1. Environmental preparation

1. Tool preparation

2. Virtual machine environment

3. Docker environment

2. Project preparation

1. Configure the Dockerfile of each module (microservice)

2. Configure the docker-compose.yml file

3. Maven packaging

4. File integration and transmission

3. Microservice deployment

1. Deploy to Docker

2. Access microservices

4. Summary of issues

1. Loading and startup are very slow

2. jdk8 is not installed for CentOS


1. Environment preparation

1. Tool preparation

  • The virtual machine requires: VMware;
  • To operate a virtual machine, you need to use: Xshell, Xftp;
  • To deploy microservices on Linux, you need to use: Docker;
  • Microservice project packaging needs to use: IDEA, Maven;
  • To edit Dockerfile and docker-compose.yml files, you need to use: any editor;

2. Virtual machine environment

(1) Use CentOS7 as the server operating system for project deployment

Since accessing microservices requires the operating system to have ports open, two measures can be taken:

  • The first one: turn off the firewall;
  • The second type: open which ports are used by microservices;

Please refer to the following content: https://www.cnblogs.com/ketoli/p/15111625.html

Here I choose to use the port and then open it. It should be noted that: Every time after updating the firewall configuration, you need to restart the firewall and Docker.

As shown in the picture above: ports 10086, 8091, 8081, 8080, and 8848 are opened.

(2) Install JDK8

  • First, install the yum tool;
yum install -y yum-utils device-mapper-persistent-data lvm2 --skip-broken
  • Use the following command to install jdk8 directly
yum install java-1.8.0-openjdk* -y

3. Docker environment

(1) Configure Alibaba Cloud warehouse and configure image source

yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

sed -i '[email protected]@mirrors.tuna.tsinghua.edu.cn/docker-ce@g' /etc/yum.repos.d/docker-ce.repo

yum makecache fast

(2) Install docker-ce

yum install -y docker-ce

(3) Configure image accelerator

  • registry-mirrors can use its own Alibaba Cloud mirror accelerator, just click Baidu.
sudo mkdir -p /etc/docker

sudo tee /etc/docker/daemon.json <<- 'EOF'
{
  "registry-mirrors": ["https://n0dwemtq.mirror.aliyuncs.com"]
}

EOF

sudo systemctl daemon-reload

sudo systemctl restart docker

(4) Install Docker-Compose

curl -SL https://get.daocloud.io/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose</pre >
<p><strong>(6) Set Docker to start automatically when booting</strong></p>
<pre>systemctl enable docker

2. Project preparation

A microservice project will be deployed below, including 3 microservices:

  • gateway gateway;
  • order service;
  • user service;

1. Configure the Dockerfile of each module (microservice)

(1) Writing Dockerfile

  • Here is a Dockerfile of the gateway module as an example;
  • Other modules only need to modify the ports exposed by EXPOSE;
# Specify base image
FROM java:8-alpine
# Copy the jar package to the container
COPY ./app.jar /app.jar
#Expose port
EXPOSE 10086
# Entry, startup command of java project
ENTRYPOINT java -jar /app.jar

2. Configure docker-compose.yml file

The Dockerfile file builds the image, and the docker-compose file builds the container based on the image.

What needs attention is:

  • Service name, such as: nacos, user-service;
  • build: It will look for the folder corresponding to the directory where docker-compose is currently located, and build the container based on the contents of the folder;
  • ports: Port number, which is the mapping between server port and docker port;
  • restart=always: Restart the container only when the container has been stopped or Docker stopped/restarted;
version: "3.2"

services:
  nacos: # You need to change the address localhost of these middlewares in the application configuration file in the java project to the service name nacos (the database is mysql)
    image: nacos/nacos-server
    environment:
      MODE: standalone
    ports:
      - "8848:8848"
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "100m"

  user-service: # Consistent with the name of the nacos registration center
    build: ./user-module # Find the folder in the directory where the current yml is located, which contains the dockerfile
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "100m"

  order-service:
    build: ./order-module
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "100m"

  gateway:
    build: ./gateway
    ports:
      - "10086:10086"
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "100m" 

3. Maven packaging

(1) Project structure

  • feign-api: Integrates nacos and ribbon, but does not need to be packaged, just use the nacos container instead;
  • gateway: Gateway, which performs common authentication and cross-domain interception;
  • order-module: order-service service;
  • user-module: user-service service;

(2)pom file

There are two points to note when writing pom files:

  • Only modules with application startup classes need to add ;
  • In , is uniformly set to app, so that the names of the obtained jar packages are all app, which is convenient for writing;
  • Take my project as an example: feign-api is used for remote calling and load balancing, and does not require a startup class, so it is the only one without ;

(3) Modify application and bootstrap configuration files

  • Change the ip in the database url to mysql; (or other database you use)
  • Change the IP address of the address discovered by the nacos service and the address of the unified configuration to nacos;
  • This is because in actual use, the IP address must not be the local address;
  • bootstrap:

  • application:

  • Because I have not used the database, I will not demonstrate the modification of the database IP;

(4)Packaging

  • After completing the previous two steps, you can package it. Just use maven’s clean + package command directly;

  • After completing the jar package, app.jar can be found in the target directory;

4. File integration and transfer

(1) Place the jar package into the specified folder

  • In docker-compose.yml, we specified user-service to be placed in the user-module folder;
  • Therefore, user’s jar package must be placed in user-module, and the same applies to others;

  • Below are the files for the entire project;

(2) Transfer to Linux

  • Use Xftp (or other file transfer tool) to transfer the Cloud01 folder to Linux;
  • You can decide where to put it;

3. Microservice deployment

1. Deploy to Docker

(1) cd to the location of the project in Linux

  • Create the directory using: mkdir -p /usr/myProject/springcloud/;
  • Then cd to the directory containing docker-compose.yml;

(2) Using docker-compose

  • Use the command: docker compose up -d; (-d runs in the background)
  • Its function is: create and start Container;

Notice:

  • We have configured the image attribute in the nacos service, and the image will be automatically pulled;
  • The java:8-alpine base image is introduced in the Dockerfile and will be automatically pulled;
  • Therefore, it will take a long time to use it for the first time. It is recommended not to add -d first and you can observe the creation process;

(3) Problems you may encounter

If we use docker compose up to create and start all containers at once, there will be a problem:

  • If nacos is not started first, other services will not be able to register.

So we have two options:

  • The first method: first compose up nacos to start and create nacos containers, and then compose up to start all containers;
  • The second type: directly compose up to start all containers, and then compose restart [service] [service] to restart services except nacos;

2. Access microservices

(1) Visit nacos

  • We can try to visit the nacos service center first;
  • It can be found that the services have been registered;

(2) Send a request to http://ip:port/xxx

  • Here I will initiate a query request to order-service from the gateway;
  • order-service will call user-service remotely;

4. Summary of Problems

1. Loading and starting are very slow

The problem I’m having is:

  • After docker compose up, it will take several minutes to access the nacos home page. And you need to wait until you can access the nacos homepage, and then restart other services before you can register the service to nacos.

(1) Solution

Considering that microservices actually occupy a lot of memory, Iincreased the memory of the virtual machine to 4G.

After deployment, the service loading speed becomes very fast.

2. jdk8 is not installed for CentOS

Since you want to run a java project, a jdk is also essential. (Personally, I just forgot to install jdk and debugged for several hours but failed to deploy successfully)

This has been introduced in the previous article Preparing the Virtual Machine Environment.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138824 people are learning the system