Spring Cloud Microservice Architecture

Microservices (or microservice architecture) is a cloud-native architectural approach in which a single application is composed of manyLoosely coupled and independentA deployment is made up of smaller components or services.
Spring Cloud is a series框架的有序集合that simplifies the development of distributed systems by using the development convenience of Spring Boot, such as service discovery, service gateway, service routing, link tracking, etc. The Baidu Encyclopedia
technology stack relationship is as follows:insert image description here

concept

The concept of Spring cloud Chinese website
microservices originated from an article "Microservices" written by Martin Fowler in March 2014. The microservice architecture is an architectural pattern. He advocates dividing a single application into a set of small services. Each service coordinates and cooperates with each other to provide users with ultimate value. Each service runs in its own independent process, and services communicate with each other using a lightweight communication mechanism (usually HTTP-based RESTFUL API). Each service is built around a specific business, and can be independently deployed to a production environment, production-like environment, etc. In addition, a unified and centralized service management mechanism should be avoided as much as possible. For a specific service, it should be According to the business context, choose the appropriate language and tools to build it. 微服务是一种架构风格, a large complex software application consists of one or more microservices. Each microservice in the system can be deployed independently, and each microservice is loosely coupled (can call each other). Each microservice is only focused on doing one task and doing it well, and in all cases, each task represents a small business capability.
Coupling: the degree of correlation between two things, according to the degree of correlation, the degree of coupling is divided
into:
✔ Complete coupling: closely related, cannot be separated, the next step is based on the previous step
✔ Loose coupling: the degree of correlation is not high
✔ Complete solution Coupling: No association.
Microservices can run in containers. Containers need to provide an environment for microservices. What runs in containers is microservices. Containers have nothing to do with microservices.

为什么叫微服务架构?
As long as there is a microservice layer in the entire architecture, it can be called a microservice architecture.

容易混淆的概念
Spring boot: spring boot is a framework, a brand-new programming specification, and its generation simplifies the use of the framework. The so-called simplification refers to the simplification of a large number of cumbersome configuration files required in many spring frameworks, so springboot is a service Frame by frame, service scope is simplified configuration files!

Spring cloud: spring cloud provides a complete set of service solutions based on spring boot, including service registration and discovery, configuration center, full link monitoring, service gateway, load balancing, fuse and other components Spring cloud uses the development convenience of spring
boot Ingeniously simplifying the infrastructure development of distributed systems, spring cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-agents, and event buses. Use the development style of spring boot to achieve one-click startup and deployment!

The difference between Springboot and Springcloud

Spring boot: Focus on fast and convenient development of individual microservices (focus on micro, simplify configuration);
Spring cloud: focus on the global microservice coordination governance framework, combine and manage individual microservices developed by spring boot (focus on macro);
Spring boot can be used independently without spring cloud, but spring cloud cannot be separated from spring boot, which is a dependency.

Analysis of all components of Spring cloud

insert image description here

1.Fegin(接口调用): Microservices communicate through the rest interface. Springcloud provides the fegin framework to support rest calls. fegin enables the rest interface calls of different processes to be carried out in an elegant way. This elegance behaves like the same process call (simplified micro service communication process)

2.Eureka(注册发现): In the microservice mode, a large web application is usually split into many smaller web applications (services). At this time, there needs to be a place to store the relevant information of these services, so that each small application can know each other , this time you need to register in the registration center. When each application starts, it registers its own information (IP address, port number, service name, etc.) with the configured registration center, and the registration center saves them. When the services call each other, they can be found in the registration center through the service name. Corresponding service information, so as to communicate, annotation and discovery services bring convenience to calls between microservices, and solve the problem of hard coding. Services only pass each other's service id without knowing their IP and port. Yes, to obtain the service of the other party
Hard-coded: the code is hard-coded, and the caller can only be found by writing whoever calls it

3.Ribbon(负载均衡): Ribbon is a load balancer released by Netflix. It helps to control the behavior of http and TCP clients. After configuring the address list of the service provider for the ribbon, the ribbon can automatically help the service consumer to go based on a certain load balancing algorithm. ask. Ribbon provides us with many load balancing algorithms by default, such as polling, random, etc. Of course, we can also implement custom load balancing algorithms for ribbon. In spring cloud, when ribbon is used in conjunction with eureka, ribbon can automatically Obtain the address list of service providers from eurekaserver, and request an instance of one of the service providers based on the load balancing algorithm (for service reliability, a microservice may deploy multiple instances)

4.Hystrix(熔断器): When the service provider responds very slowly, the consumer's request to the provider will be forced to wait until the provider's response times out. In a high-load scenario, if nothing is done, such problems may lead to resource exhaustion of service providers and even the collapse of the entire system (avalanche effect). Hystrix is ​​designed to prevent such problems from happening. Hystirx is a delay and fault-tolerant library open sourced by Netflix, which is used to isolate access to remote systems, services or third-party libraries, prevent cascading failures, and improve system availability and fault tolerance.

5.Zuul(微服务网关): Different microservices generally have different network addresses, and external clients may need to call multiple service interfaces to complete a business requirement, such as a mobile app for movie ticket purchases. It is possible to call the interfaces of multiple microservices to complete the business process of purchasing a ticket. If the client directly communicates with each microservice, there will be the following problems: 01. The client
will request different microservices multiple times, which increases the number of customers 02. There are cross-domain requests, and the
processing is relatively complicated in certain scenarios.
03. Authentication is complex, and each service requires independent authentication.
04. Some microservices may use protocols that are unfriendly to firewalls and browsers. There will be certain difficulties.
The above problems can be solved with the help of microservice gateways.

The microservice gateway is an intermediate layer between the client and the server, and all external requests will first pass through the microservice gateway. After the microservice gateway, the microservice gateway will encapsulate the internal structure of the application. The client only needs to interact with the gateway without directly calling the interface of a specific microservice. In this way, development can be simplified. Not only that, using microservices The gateway also has the following advantages:
1) Easy monitoring: monitoring data can be collected on the microservice gateway and sent to external systems for analysis
2) Easy authentication: authentication can be performed on the microservice gateway, and then the request is forwarded to the backend Microservices without authentication in each microservice
3) Reduces the number of interactions between clients and individual microservices

Spring cloud bus (unified configuration service)

Spring Cloud Bus uses a lightweight message broker to link nodes in a distributed system. This proxy can then be used to broadcast state changes (such as configuration changes) or other administrative instructions. A key idea is that this bus is like a distributed actuator for Spring Boot applications that can scale out. But it can also be used as a communication channel between applications. This project provides starters for AMQP brokers or Kafka as transporter.
For traditional monolithic applications, configuration files are often used to manage all configurations. For example, a single application developed by springboot can put the configuration content in the application.Yml file. If you need to switch the environment, you can set multiple profiles and specify them when starting the application spring.profiles.active={profile}. However, in the microservice architecture, the configuration management of microservices generally has the following requirements:
01. Centralized management configuration: An application system using the microservice architecture may contain hundreds or thousands of microservices, so centralized management configuration is very useful Necessary
02. Different environments, different configurations: For example, data source configurations are different in different environments (development, testing, pre-release, production, etc.).
03. It can be dynamically adjusted during operation: for example, the size of the data source connection pool or the fusing threshold can be dynamically adjusted according to the load of each microservice, and the microservice will not be stopped when the configuration is adjusted. 04. After the configuration is modified, it can be automatically updated: such as the configuration
content Changes occur, and microservices can automatically update the configuration.
In summary, for the microservice architecture, a general configuration management mechanism is essential. The common practice is to use the configuration server to manage the configuration, and spring cloudbus uses GIT or SVN for management. Configure and use information buses such as Kafka or rabbit MQ to notify all applications, so as to realize automatic configuration update and refresh the configuration of all microservice instances.

deployment configuration

Experimental environment
The first 192.168.235.10
and the second 192.168.235.11

firewall-cmd --zone=public --add-port=8000/tcp --permanent
firewall-cmd --zone=public --add-port=8761/tcp --permanent
firewall-cmd --zone=public --add-port=8888/tcp --permanent
firewall-cmd --zone=public --add-port=9000/tcp --permanent
firewall-cmd --zone=public --add-port=9001/tcp --permanent
firewall-cmd --reload

Eureka

[Cluster of Registration Center]

  • The first
[root@localhost ~]# unzip spring-cloud-examples.zip 
[root@localhost ~]# cd spring-cloud-examples/
[root@localhost spring-cloud-examples]# cd eureka-producer-consumer/spring-cloud-eureka/
[root@localhost spring-cloud-eureka]# ls
pom.xml  src
[root@localhost spring-cloud-eureka]# vim src/main/resources/application.properties 
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=true		#表示是否将自己注册到Eureka Server
eureka.client.fetch-registry=true			#表示是否从Eureka Server中获取注册的服务信息
eureka.client.serviceUrl.defaultZone=http://192.168.235.10:8000/eureka/,http://192.168.235.11:8000/eureka/
[root@localhost spring-cloud-eureka]# mvn clean package			"清除由项目编译创建的target,并打包"
[root@localhost spring-cloud-eureka]# java -jar target/spring-cloud-eureka-0.0.1-SNAPSHOT.jar	   "运行jar包命令"
···会阻塞终端
  • the second
[root@localhost ~]# cd spring-cloud-examples/eureka-producer-consumer/spring-cloud-eureka/
[root@localhost spring-cloud-eureka]# vim src/main/resources/application.properties 
spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=http://192.168.235.10:8000/eureka/,http://192.168.235.11:8000/eureka/
[root@localhost spring-cloud-eureka]# mvn clean package
[root@localhost spring-cloud-eureka]# java -jar target/spring-cloud-eureka-0.0.1-SNAPSHOT.jar

Open another terminal on the first one
[root@localhost ~]# firefox 192.168.235.10:8000
insert image description here

Producer

【Start producer】

  • first and second

Open another terminal

[root@localhost ~]# cd /root/spring-cloud-examples/eureka-producer-consumer/spring-cloud-producer/
[root@localhost spring-cloud-producer]# vim  src/main/resources/application.properties
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://192.168.235.10:8000/eureka/,http://192.168..235.11:8000/eureka/
[root@localhost spring-cloud-producer]# mvn spring-boot:run
···
  • Modify the output information in the second station

Load balancing components:
[root@localhost spring-cloud-producer]# vim src/main/java/com/neo/controller/HelloController.java

@RestController
public class HelloController {
    
    

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
    
    
        return "hello "+name+",this is zhaohuaizhe";
    }
}

[root@localhost spring-cloud-producer]# mvn spring-boot:run

  • The first consumer to run
[root@localhost ~]# cd /root/spring-cloud-examples/eureka-producer-consumer/spring-cloud-consumer/
[root@localhost spring-cloud-consumer]# vim src/main/resources/application.properties 
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://192.168.235.10:8000/eureka/,http://1
92.168.235.11:8000/eureka/
[root@localhost spring-cloud-consumer]# source /etc/profile
[root@localhost spring-cloud-consumer]# mvn spring-boot:run

In addition, the terminal is opened to access the consumer's ip and port feedback information is on the producer

[root@localhost ~]# curl 192.168.235.10:9001/hello/aa
hello aa,this is first messge[root@localhost ~]# curl 192.168.235.10:9001/hello/aa
hello aa,this is zhaohuaizhe

[root@localhost ~]# firefox 192.168.235.10:8000
insert image description here

Hystrix

[The fuse of microservices, the role of health checks]

  • The consumer (consumer) on the first station ends

Run consumer with fuse

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-hystrix/spring-cloud-consumer-hystrix/
[root@localhost spring-cloud-consumer-hystrix]# vim src/main/resources/application.properties
spring.application.name=spring-cloud-consumer-hystrix
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://192.168.235.10:8000/eureka/,http://192.168.235.11:8000/eureka/
[root@localhost spring-cloud-consumer-hystrix]# mvn spring-boot:run

Open another terminal

[root@localhost ~]# curl 192.168.235.10:9001/hello/aa
hello aa,this is first messge
[root@localhost ~]# curl 192.168.235.10:9001/hello/aa
hello aa,this is zhaohuaizhe
第一台上的producer停止
[root@localhost ~]# curl 192.168.235.10:9001/hello/aa
hello aa,this is zhaohuaizhe

[root@localhost ~]# firefox 192.168.235.10:8000
insert image description here

Zuul

  • The cosumer with monitoring on the first machine stops

run gateway

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-zuul/spring-cloud-zuul/
[root@localhost spring-cloud-zuul]# vim src/main/resources/application.properties 
spring.application.name=gateway-service-zuul
server.port=8888
eureka.client.service-url.defaultZone=http://192.168.235.10:8000/eureka/,http://192.168.235.11:8000/eureka/
#是否开启重试功能
zuul.retryable=true
#对当前服务的重试次数
ribbon.MaxAutoRetries=2
#切换相同Server的次数
ribbon.MaxAutoRetriesNextServer=0
[root@localhost spring-cloud-zuul]# mvn spring-boot:run

Open another terminal

[root@localhost ~]# curl 'http://192.168.235.10:8888/spring-cloud-producer/hello?name=haha&token=123'
hello haha,this is zhaohuaizhe

[root@localhost ~]# firefox 192.168.235.10:8000
insert image description here

Sleuth+zipkin (tracking service)

Spring Cloud Sleuth implements a distributed 服务链路跟踪solution. By using Sleuth, we can quickly locate the problem of a certain service.
✔ Sleuth and zipkin can be used in combination to 图形化的界面view the delay of microservice requests and the dependencies of each microservice, to depict the topology of the microservice system, and to optimize a certain microservice based on time-consuming analysis!
✔ Sleuth is one of the components of Spring Cloud. It implements a 分布式追踪solution for Spring Cloud, compatible with Zipkin, HTrace and other log-based tracking systems, such as ELK (Elasticsearch, Logstash, Kibana)
✔ Zipkin is an APM for distributed systems Tool (Application Performance Management), implemented based on Google Dapper. 延迟问题It helps to collect the time data needed to solve the microservice architecture and manage this data. Combined with Sleuth, it can provide a visualized web interface to analyze the time consumption of calling links.

first host

–> All the above terminals are over, because there is a fixed directory dedicated to link tracking
Memory 3G

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/
[root@localhost spring-cloud-sleuth-zipkin]# ls
pom.xml              spring-cloud-producer  zipkin-server
spring-cloud-eureka  spring-cloud-zuul
# 注册中心
[root@localhost spring-cloud-sleuth-zipkin]# cd spring-cloud-eureka/
[root@localhost spring-cloud-eureka]# vim src/main/resources/application.yml 
server:
  port: 8761
spring:
  application:
    name: eureka
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.235.10:8761/eureka/
[root@localhost spring-cloud-eureka]# mvn spring-boot:run

–> Link tracking opens another terminal [source /etc/profile]

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/zipkin-server/
[root@localhost zipkin-server]# vim src/main/resources/application.yml 
      defaultZone: http://192.168.235.10:8761/eureka/
[root@localhost zipkin-server]# mvn spring-boot:run

producer producer[Ctrl+Shift+T]

[root@localhost zipkin-server]# cd ..       
[root@localhost spring-cloud-sleuth-zipkin]# cd spring-cloud-producer/
[root@localhost spring-cloud-producer]# vim src/main/resources/application.yml
server:
  port: 9001
spring:
  application:
    name: producer
  zipkin:
    base-url: http://192.168.235.10:9000
  sleuth:
    sampler:
      percentage: 1.0
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.235.10:8761/eureka/
[root@localhost spring-cloud-producer]# mvn spring-boot:run

–>Zuul Gateway

[root@localhost ~]# cd /root/spring-cloud-examples/spring-cloud-sleuth-zipkin/spring-cloud-zuul/
[root@localhost spring-cloud-zuul]# vim src/main/resources/application.yml 
server:
  port: 8888
spring:
  application:
    name: zuul
  zipkin:
    base-url: http://192.168.235.10:9000
  sleuth:
    sampler:
      percentage: 1.0
eureka:
  client:
    serviceUrl:
      defaultZone: http://192.168.235.10:8761/eureka/
[root@localhost spring-cloud-zuul]# mvn spring-boot:run

–> open another terminal access

[root@localhost ~]# curl 'http://192.168.235.10:8888/producer/hello?name=aa&token=123'
hello aa,this is first messge
[root@localhost ~]# firefox 192.168.235.10:9000

insert image description here
insert image description here

Spring Boot

Stop the previous link tracking

[root@localhost ~]# cd /root/spring-cloud-examples/spring-boot-admin-eureka/
[root@localhost spring-boot-admin-eureka]# ls
pom.xml                   spring-cloud-producer
spring-boot-admin-server  spring-cloud-producer-2
spring-cloud-eureka

–> Registration Center

[root@localhost spring-boot-admin-eureka]# cd spring-cloud-eureka/
[root@localhost spring-cloud-eureka]# mvn spring-boot:run

–> Open another terminal
and run producer 1

[root@localhost ~]# cd /root/spring-cloud-examples/spring-boot-admin-eureka/spring-cloud-producer
[root@localhost spring-cloud-producer]# mvn spring-boot:run
Updating port to 9000
2021-09-27 11:40:48.035  INFO 54084 --- [           main] com.neo.ProducerApplication              : Started ProducerApplication in 11.475 seconds (JVM running for 35.568)

run producer 2

[root@localhost ~]# cd /root/spring-cloud-examples/spring-boot-admin-eureka/spring-cloud-producer-2/
[root@localhost spring-cloud-producer-2]# mvn spring-boot:run
Updating port to 9001
2021-09-27 11:42:16.187  INFO 54263 --- [           main] com.neo.Producer2Application             : Started Producer2Application in 10.92 seconds (JVM running for 18.074)

–> Run the service for configuring mail
[root@localhost ~]# cd /root/spring-cloud-examples/spring-boot-admin-eureka/spring-boot-admin-server/ [root@localhost spring-boot-
admin- server]# vim src/main/resources/application.yml

  1 server:
  2   port: 8000
  3 spring:
  4   application:
  5     name: admin-server
  6   mail:
  7     host: smtp.qq.com
  8     username: 1684629789@qq.com
  9     password: xxx(授权密码)
 10     properties:
 11       mail:
 12         smtp:
 13           auth: true
 14           starttls:
 15             enable: true
 16             required: true
 17   boot:
 18     admin:
 19       notify:
 20         mail:
 21           from: 1684629789@qq.com
 22           to: 1684629789@qq.com

[root@localhost spring-boot-admin-server]# mvn spring-boot:run
Stop a producer at will to check whether to send mail!
Rather than envy, as their own efforts to catch up!

Guess you like

Origin blog.csdn.net/qq_50573146/article/details/125009770