Author: HelloGitHub- Qin Ren
This article is suitable for SpringBoot and SpringCloud basic knowledge of the crowd, follow this article can use and quickly build a SpringCloud project.
HelloGitHub launched the “Open source project” series, today to bring you a micro-service development based on SpringCloud2.1 hand open source project — SpringCloud
Project source code address: github.com/zhoutaoo/Sp…
A brief introduction of micro services
Microservices are units of service that can be independently deployed, horizontally scaled, and independently accessed. The smallest common unit of microservices in Java is a standalone project based on the SpringBoot framework. A microservice does only one thing (a single responsibility) before a combination of microservices can be called a complete project or product. Multiple microservices need to be managed, and SpringCloud is the master steward of these microservices. It is a collection of ordered frameworks, easy to understand, easy to deploy and maintain distributed system development toolkit.
The open source project introduced today is a scaffolding based on SpringCloud2.1 that allows project development to move quickly into business development without spending too much time on architecture. Let’s take a look at the use of this project.
Ii. Project structure
This is illustrated with a gateway (gateway-admin) microservice.
The project directory structure is shown below:
Catalog Description:
- Db: project initialization database script.
- Docker: docker configuration file directory that packages microservices as Docker images.
- Config: directory of project configuration information, including database configuration and message conversion configuration.
- Dao: Database operation directory, which is used to add, delete, check and modify underlying data.
- Entity: Project entity class directory.
- Events: event processing directory.
- Exception: Exception handling directory that handles global exceptions through the aspect.
- Rest: The microservice controller directory, which is the interface provided externally.
- Service: microservices 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
Three, actual combat operation
3.1 the premise
- Ensure that Git, Java8, and Maven are installed locally.
- Know something about SpringMVC, because SpringBoot evolved from SpringMVC.
- Know some knowledge about application container engine Docker and Docker-compose.
3.2 Microservice architecture description
A complete project, microservices architecture generally includes the following services:
- Registries (commonly used frameworks Nacos, Eureka)
- Unified Gateway (common frameworks Gateway and Zuul)
- Certification Center (common technical implementation scheme Jwt, OAuth)
- Distributed transactions (commonly used frameworks Txlcn and Seata)
- File service
- Business services
3.3 Running projects
Here are three ways to run it:
The first: one-click operation
In Linux or Mac OS, run the./install.sh command in the root directory of the project to quickly set up the development environment.
Second: run in the local environment
This method is not recommended, but is briefly introduced.
-
Basic environment installation: mysql, Redis, RabbitMQ
-
Environment operation:
git clone https://github.com/zhoutaoo/SpringCloud.git # Clone project Copy the code
-
Install the authentication common package to your local Maven repository and run the following command:
cd common mvn clean install Install the certified public package to the local Maven repository Copy the code
-
Install the registry Nacos
- Download Nacos
- Run the following command:
Unzip nacos-server-0.9.0.zip OR tar-xvf nacos-server-0.9.0.tar.gzcd nacos/bin bash startup.sh -m standalone # Linux startup command cmd startup.cmd Windows startup command Copy the code
-
Run gateway services, authentication services, and business services
Here in gateway service, for example: executive GatewayAdminApplication. Java
Note: Authentication services (AUTH), Gateway services (Gateway), and Organization Management Services (sysadmin) need to execute database initialization scripts.
Through swager interface: http://localhost:8445/swagger-ui.html test whether build successful, if it can be said access services started successfully.
Description:
-
The application. Yml file configures rabbitMQ, redis, and mysql connections.
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} Copy the code
-
The bootstrap.yml file configures basic service information (port number, service name), registry address, and so on.
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} Copy the code
Third: Docker environment run
-
Installation of basic Environment
-
Install using the docker command
# installed redis docker run -p 6379:6379 --name redis -d docker.io/redis:latest --requirepass "123456" # mysql installationDocker run --name mysql5.7 -p 3306:3306-e MYSQL_ROOT_PASSWORD=root123 -dDocker. IO/mysql: 5.7# to install the 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 Copy the code
-
It can also be installed using the docker-compose command
cd docker-compose docker-compose up -d Docker-compose install mysql, Redis, rabbitMQ service Copy the code
-
-
Download the project locally
git clone https://github.com/zhoutaoo/SpringCloud.git # Clone project Copy the code
-
Install the public authentication package to your maven repository and run the following command:
cd common && mvn install Install the certified public package to the local Maven repository Copy the code
-
Nacos docker – compose run
cd docker-compose docker-compose -f docker-compose.yml -f docker-compose.nacos.yml up -d nacos Start the registry Copy the code
-
Build the message center image
cd ./center/bus mvn package && mvn docker:build cd docker-compose # Start the message center docker-compose -f docker-compose.yml -f docker-compose.center.yml up -d bus-server Copy the code
Other services that need to be mirrored are: (Note: Operations and message center mirrors are built in a similar way)
-
Gateway Management Service (gateway-admin, gateway-Web)
-
Organization Services (sysadmin/ Organization)
-
Authentication Service (Auth /authentication-server)
-
Auth Authorization-Server
-
Management Console Service (Monitor /admin)
3.4 Running Effect
Nacos Service Center
All services start normally and can be viewed in the NACOS management center. The number of instances indicates the number of running services. A value of 1 can be interpreted as a normal service start.
Viewing Background Services
Docker ps -a Displays information about all docker processes
Detects service availability by accessing the Microservice’s Exposed interface (Swagger).
Swager Interface address: http://IP:port/swagger-ui.html
The test is shown below:
Four, the last
Microservices (SpringBoot, SpringCloud, Docker) are getting a lot of buzz right now. It’s not a new technology, it’s an extension of an older technology with new features.
At this point, you should be able to quickly build microservices through the SpringCloud project. It’s time to update your skill tree. Let’s learn about microservices!
5. Reference materials
-
Install Nacos locally
-
Use of the NACOS registry
-
Docker – compose tutorial
-
Docker technology
“Explain Open Source Project series” — let the people who are interested in open source projects not be afraid, let the initiator of open source projects not be alone. Follow along as you discover the joys of programming, use, and how easy it is to get involved in open source projects. Welcome to leave a message to contact us, join us, let more people fall in love with open source, contribute to open source ~