The series "explaining open source projects" launched by HelloGitHub today brings you an open source project for microservice development based on SpringCloud 2.1 - SpringCloud
Project source code address: https://github. com/zhoutaoo/SpringCloud1, Introduction to microservices
Microservices are service units that can be deployed independently, expanded horizontally and accessed independently. The smallest 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 microservices need to be managed, and spring cloud is the housekeeper of these microservices. It is a collection of a series of orderly frameworks, a distributed system development kit that is easy to understand, deploy and maintain.
The open source project introduced today is a scaffold based on spring cloud 2.1, which allows project development to 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 as follows:
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 is mainly used to add, delete, query and modify 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 (common frameworks Gateway and Zuul)
- Certification Center (common technical implementation schemes Jwt and OAuth)
- Distributed transaction (common frameworks Txlcn and Seata)
- File service
- Business services
3.3 operation items
Three operation modes are described below:
First: one key operation
Linux and Mac systems can be executed under the project root directory ./ install.sh Quickly build a development environment.
The second: local environment operation
This method is not recommended, but it is briefly introduced.- Basic environment installation: mysql, redis, rabbitmq
2. Environmental operation:
git clone https://Github.com/zhaouto/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 certified public package to the local maven warehouse
4. Install the registry 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.
You can test whether the setup is successful through the swager interface: http:// localhost:8445/swagger-ui.html. If it can be accessed normally Sale of second-hand mobile games Indicates that the service was started successfully.
explain:
- The application.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}
- The bootstrap.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
2. Download project to local
git clone https://Github.com/zhaouto/springcloud.git # cloning project
3. Install the certification public package to the local maven warehouse and execute the following commands:
cd common && mvn install #Install the certified public package to the local maven warehouse
4. Docker compose runs Nacos
cd docker-compose docker-compose -f docker-compose.yml -f docker-compose.nacos.yml up -d nacos #Start registry
5. Build a 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
- Console 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.
At this point, you should be able to quickly build microservices through the spring cloud project. Then you can start your journey of learning about microservices. It's time to update your skill tree. Let's learn about microservices together!
5, References
- Install Nacos locally
- Use of the nacos registry
- Docker compose tutorial
- Docker Technology
"Explain open source project series" -- let people interested in open source projects no longer fear and let the initiator of open source projects no longer be alone. Following our article, you will find the fun of programming, using and participating in open source projects so simple. Welcome to leave a message, contact us and join us to make more people love and contribute to open source ~