1, Introduction to microservices
-
Microservices are service units that can be deployed independently, expanded horizontally and accessed independently. The smallest common microservice unit in Java is an independent project based on the SpringBoot framework. A micro service only does one thing (single responsibility), and multiple micro service combinations can be called a complete project or product. So many micro services need to be managed, and spring cloud is the housekeeper of these micro services. It is a collection of a series of ordered frameworks, a distributed system development kit that is easy to understand, deploy and maintain.
-
The open source project introduced today is based on spring cloud 2 1, so that project development can quickly enter business development without spending too much time on architecture. Let's take a look at the use of this project.
2, Project structure
-
This is illustrated by a gateway admin microservice.
-
The project directory structure is shown in the following figure:
-
Catalog Description:
-
db: project initialization database script.
-
Docker: docker configuration file directory, which packages the micro service as a docker image.
-
config: project configuration information directory, including database configuration, message conversion configuration, etc.
-
dao: database operation directory, which mainly adds, deletes, checks and modifies the underlying data.
-
Entity: project entity class directory.
-
events: event processing directory.
-
Exception: exception handling directory, which handles global exceptions through aspect oriented processing.
-
rest: the directory of the micro service controller, that is, the interface provided externally.
-
Service: Micro service business layer directory.
-
GatewayAdminApplication: microservice SpringBoot entry class.
-
resources: project configuration file directory.
-
Test: project unit test directory.
-
pom.xml: maven project object model file.
3, Actual operation
3.1 premise
Make sure Git, Java8, Maven are installed locally.
Understand some knowledge of spring MVC, because spring boot evolved from spring MVC.
Understand some knowledge of Docker and Docker compose.
3.2 microservice architecture description
A complete project, the microservice architecture generally includes the following services:
- Registry (commonly used frameworks Nacos, Eureka)
- Unified Gateway (commonly used frameworks Gateway and Zuul)
- Certification Center (common technical implementation schemes Jwt and OAuth)
- Distributed transaction (commonly used frameworks Txlcn and Seata)
- File service
- Business services
3.3 operation items
-
The following describes three operation modes:
-
First: one key operation
Linux and Mac systems can be executed under the root directory of the project/ install.sh quickly build a development environment. -
Second: local environment operation
This method is not recommended, but it is still briefly introduced. -
1. Basic environment installation: mysql, redis, rabbitmq
-
2. Environmental operation:
git clone https://github.com/zhoutaoo/SpringCloud.git # cloning project
- 3. Install the certification public package to the local maven warehouse and execute the following command:
cd common mvn clean install #Install the certification public package to the local maven warehouse
-
4. Install registration center Nacos
-
Download Nacos
-
Execute the following command
unzip nacos-server-0.9.0.zip OR tar -xvf nacos-server-0.9.0.tar.gz cd nacos/bin bash startup.sh -m standalone # Linux startup command cmd startup.cmd # Windows startup command
-
5. Run gateway services, authentication services, business services, etc
-
Take the gateway service as an example:
GatewayAdminApplication.java
- Note: authentication service (auth), gateway service (Gateway) and organization management service (sysadmin) need to execute database initialization script.
- Via swager interface: http://localhost:8445/swagger -ui. Whether the HTML test is set up successfully. If it can be accessed normally, it indicates that the service is started successfully.
explain:
- application. The YML file mainly configures the connection information of rabbitmq, redis and mysql.
spring: rabbitmq: host: ${RABBIT_MQ_HOST:localhost} port: ${RABBIT_MQ_PORT:5672} username: ${RABBIT_MQ_USERNAME:guest} password: ${RABBIT_MQ_PASSWORD:guest} redis: host: ${REDIS_HOST:localhost} port: ${REDIS_PORT:6379} #password: ${REDIS_PASSWORD:} lettuce: pool: max-active: 300 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:${DATASOURCE_DBTYPE:mysql}://${DATASOURCE_HOST:localhost}:${DATASOURCE_PORT:3306}/sc_gateway?characterEncoding=UTF-8&useUnicode=true&useSSL=false username: ${DATASOURCE_USERNAME:root} password: ${DATASOURCE_PASSWORD:root123}
- bootstrap. The YML file mainly configures basic service information (port, service name), registry address, etc.
server: port: ${SERVER_PORT:8445} spring: application: name: gateway-admin cloud: nacos: discovery: server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848} config: server-addr: ${REGISTER_HOST:localhost}:${REGISTER_PORT:8848} file-extension: yml sentinel: transport: dashboard: ${SENTINEL_DASHBOARD_HOST:localhost}:${SENTINEL_DASHBOARD_PORT:8021}
-
The third type: Docker environment operation
-
Basic environment installation
-
Install via docker command
# Install redis docker run -p 6379:6379 --name redis -d docker.io/redis:latest --requirepass "123456" # Install mysql docker run --name mysql5.7 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root123 -d docker.io/mysql:5.7 # Installing rabbitmq docker run -d -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin --name rabbitmq docker.io/rabbitmq:latest
- You can also use the docker compose command to install
cd docker-compose docker-compose up -d #Docker compose installs mysql, redis, rabbitmq services
- Download project to local
git clone https://github.com/zhoutaoo/SpringCloud.git # cloning project
- Install the authentication public package to the local maven warehouse and execute the following command:
cd common && mvn install #Install the certification public package to the local maven warehouse
Docker compose run Nacos
cd docker-compose docker-compose -f docker-compose.yml -f docker-compose.nacos.yml up -d nacos #Start registry
Build message center image
cd ./center/bus mvn package && mvn docker:build cd docker-compose #Start message center docker-compose -f docker-compose.yml -f docker-compose.center.yml up -d bus-server
- Other services that need to build an image include: (Note: the operation is similar to that of the message center image)
Gateway management service (Gateway admin, gateway WEB)
-
Organization service (sysadmin/organization)
-
Authentication server
-
Authorization server
-
Management desk service (monitor/admin)
-
3.4 operation effect
-
Nacos Service Center
-
All services are started normally, which can be viewed in the nacos management center. The number of instances indicates the number of running this service. A value of 1 can be understood as the normal start of the service.
-
View background services
-
Command line execution: docker ps -a view all process information of docker
-
Check whether the service is available by accessing the exposed interface (swagger) of the micro service.
-
swager interface address: http://IP:port/swagger-ui.html
-
The test is as follows:
4, Finally
-
Microservices (SpringBoot, SpringCloud and Docker) are very noisy now. It is not a new technology, but derived from the old technology and added some new features.
-
So far, you should be able to quickly build microservices through the spring cloud project. It's time for you to learn the skills of the micro service tree. Let's start the micro service journey together!
5, References
Install Nacos locally
https://nacos.io/en-us/docs/quick-start.html
-
There are also small partners who don't understand. You can add the author's contact information
-
QQ:1162798594
-
vx:tan1999nn
-
QQ group: 665845932
-
My favorite friends can give me a little praise and attention~~~