Spring Cloud Microservice Use Case

Sample Project Details

This document uses Pig Rapid Development Framework as an example to demonstrate how to deploy a complete Spring Cloud project on Kato.

Pig Microservice Architecture V2.1.0

  • RBAC authority management system based on Spring Cloud Finchley and Spring Security OAuth2
  • Package Element-ui based on the concept of data-driven view, so you can get started quickly even without Vue experience
  • Provide support for common containerization Docker, Kubernetes, Rancher2 support
  • Provide production practice of lambda, stream api, webflux

Module Description

pig
├── pig-ui - front-end engineering [80]
├── pig-auth - Authorized service provision [3000]
└── pig-common - system common module
     ├── pig-common-core - core package of common tools
     ├── pig-common-log - log service
     └── pig-common-security - security tools
├── pig-config - Configuration Center [8888]
├── pig-eureka - service registration and discovery [8761]
├── pig-gateway - Spring Cloud Gateway [9999]
└── pig-upms - general user rights management module
     └── pigx-upms-api - common api module of general user authority management system
     └── pigx-upms-biz - General user authority management system business processing module [4000]
└── pigx-visual - graphical module
     ├── pigx-monitor - Spring Boot Admin monitoring [5001]
     └── pigx-codegen - Graphical code generation [5003]
     └── pigx-zipkin - microservice link tracking [5002]

The above modules can be divided into two categories: class libraries and services. Readers can compare their Spring Cloud projects:

  • A jar package is generated after construction, which is a collection of classes used to implement various functions, that is, a class library, such as the pig-common module in Pig
  • Generate jar package or war package after construction, start it through java -jar or tomcat, etc., open a port to provide services, that is, services, such as the pig-eureka module in Pig

Only need to construct the service module in Kato.

After sorting out, the service components that the project needs to build include:

Startup sequenceService component nameOperation portComponent function
1pig-eureka8761spring cloud service discovery registration and discovery
2pig-config8888spring cloud configuration center
3pig-gateway9999spring cloud microservice gateway
4pig-auth3000Authorized service provision
5pig-upms-biz4000General user authority management system business processing module
6pig-monitor5001Spring Boot Admin monitoring
7pig-codegen5003Graphical code generation
8pig-zipkin5002Microservice link tracking
9pig-ui80Front-end project (vue project)

Deployment Environment Description

To deploy pig, the following environment support is required:

Middleware or environmental requirementsVersion requirementsRemarks
JDK1.8Mandatory version
MySQL5.7+Mandatory version
Redis3.2 +Mandatory version
node8.0 +Used to run front-end projects
npm6.0 +Used to build front-end projects

Module Construction

Create a new application and name it spring-cloud

Get the project clone/download address: https://gitee.com/log4j/pig.git

Java Multi-module Deployment Pig Project

Refer to Java multi-module source code build

Deploy the Pig Project Step by Step

Take pig-eureka as an example to demonstrate the construction process from source code:

Add service components for spring-cloud-starting from the source code-custom warehouse:

Click New Service, Kato will automatically pull the code and automatically recognize the code language as Java-maven according to the pom.xml in the code root directory. Cancel the and build startup option:

Click Create to enter the page where the service component is not deployed. Edit the port tab, open pig-eureka’s own port 8761:

pig-eureka needs to be accessed by other microservice components for registration, so open the internal service to create dependencies later. This component also provides a web page to display the registration and health status of the microservice component, so it opens external services for external access.

Edit the build source tab and specify the pig-eureka build parameters

Since Pig itself is a multi-module project, it is necessary to specify the module constructed by the current service component. The specified method is the Maven build global parameter in the Build runtime environment settings: clean dependency:list install -pl pig-eureka -am

The above parameters specify the ordinary submodule construction method. For submodules in another submodule, such as the pig-codegen module, the parameter specification method is: clean dependency:list install -pl pig-visual/pig- codegen -am

In Kato v5.1+, you can specify the startup command of the current service component. The specified method is the start command in the build runtime environment settings: web: java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app/pig-eureka/target/*. jar, the command format is consistent with Procfile.

Edit the dependency tab to specify that the current service component depends on other service components

The dependency relationship between the various service components can be specified at the time of creation. All spring cloud microservice components need to depend on pig-eureka. Taking pig-gateway as an example, the dependencies should be added as follows:

  • After all the settings are completed, you can click Build. After the build is completed, the service component will run automatically.

  • Deploy all service components in sequence.

Deploy Mysql

The Pig microservice project needs to deploy Mysql 5.7+ as a data source. And the database initialization script is provided in the code repository: https://gitee.com/log4j/pig/blob/master/db/pig.sql

In order to load the initialization script when the database is started, a Dockerfile is made:

https://github.com/gdevs/pri-percona-mysql.git

FROM percona:5.7.23-stretch
LABEL creater="gdevs"
ENV MYSQL_VERSION=5.7.23
ENV TZ=Asia/Shanghai
ADD docker-entrypoint.sh /run/docker-entrypoint.sh
ADD ./run/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
ADD ./run/mysqld.cnf /etc/mysql/percona-server.conf.d/mysqld.cnf
RUN fetchDeps=' \
		ca-certificates \
		wget \
	'; \
	sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list; \
	apt-get update; \
	apt-get install -y --no-install-recommends $fetchDeps; \
	rm -rf /var/lib/apt/lists/*; \
    wget -O /usr/local/bin/env2file -q https://github.com/gdevs/env2file/releases/download/v0.1/env2file-linux; \
    chmod +x /run/docker-entrypoint.sh && chmod +x /usr/local/bin/env2file; \
    apt-get purge -y --auto-remove $fetchDeps
EXPOSE 3306
VOLUME ["/var/lib/mysql", "/var/log/mysql"]
# Put the scripts in the sql directory of the code repository into the corresponding initialization directory
COPY sql/*.sql /docker-entrypoint-initdb.d/
# change ENTRYPOINT exec some custom command
ENTRYPOINT [ "/run/docker-entrypoint.sh" ]
CMD [ "mysqld" ]

Add service components for spring-cloud - starting from the source code-custom repository

Name the service component pig-db and specify the code branch pig.

Adding ?dir=5.7 at the end of the code repository address url allows Kato to specify the build directory as the 5.7 folder under the code repository root directory.

In order for the database to work normally and the connection information can be obtained by other dependent applications of this database, several environment variables need to be set:

Environment variable nameEnvironment variable valueSetting location
MYSQL_USERrootConnection Information
MYSQL_HOST127.0.0.1Connection Information
MYSQL_PORT3306Connection Information
MYSQL_ROOT_PASSWORDmysqlpasswordConnection Information
MYSQL_DATABASEpigConnection information

By modifying the configuration file in pig-config to define the configuration of multiple microservice components to connect to the database:

pig/pig-config/src/main/resources/config/pigx-auth-dev.yml
pig/pig-config/src/main/resources/config/pigx-upms-dev.yml
pig/pig-config/src/main/resources/config/pigx-codegen-dev.yml
pig/pig-config/src/main/resources/config/pigx-zipkin-dev.yml.yml

The modification example is as follows:

# data source
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.jdbc.Driver
    username: ${MYSQL_USER}
    password: ${MYSQL_ROOT_PASSWORD}
    url: jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai

Deploy Redis

Through the Kato application market, Redis 4.0.13 can be installed directly.

This version of Redis provides connection information by default:

Define the configuration of all service components to connect to Redis by modifying pig/pig-config/src/main/resources/config/application-dev.yml in pig-config:

The modification example is as follows:

# Spring Related
spring:
  repeat:
    password: ${REDIS_PASS}
    host: $ {REDIS_HOST}

Deploy Pig-ui

pig-ui is a vue project written in nodejs language as the front-end static page of the entire system. Kato currently supports source code construction of nodejs front-end project, reference document: NodeJS front-end language

In order to facilitate the transformation of the project, we fork the project for modification, the code address: https://gitee.com/gdevs/pig-ui

Refer to the Kato code support specification, and add an identification file in the code root directory: nodestatic.json

{"path":"dist"}

And added the nginx configuration file in the code repository: www/web.conf to handle proxy forwarding:

server {
    listen       80;
    root   /app/www;

    location ~* ^/(code|auth|admin|gen) {
        proxy_pass http://127.0.0.1:9999;
        proxy_connect_timeout 15s;
        proxy_send_timeout 15s;
        proxy_read_timeout 15s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Use this code to build the source code to complete the deployment of the pig-ui service component.

Dependency and Port Sorting

Service component nameDependencyPort internal servicePort external service
pig-uipig-gatewayoffon
pig-gatewaypig-eureka Redisonoff
pig-monitorpig-eureka Redisoffoff
pig-configpig-eurekaoffoff
pig-upms-bizpig-eureka pig-db Redisoffoff
pig-zipkinpig-eureka pig-db Redisoffon
pig-authpig-eureka Redisoffoff
pig-codegenpig-eureka pig-db Redisoffoff

Final Results

After the deployment is completed, the topology diagram is as follows:

Landing effect:

Login account password:
admin
123456