Spring Cloud learning one

1. Evolution of system architecture
The system architecture has generally gone through several processes: single application architecture—>vertical application architecture—>distributed architecture—>SOA architecture—>microservice architecture, and the emerging Service Mesh (service meshing) )
1. Monolithic application architecture
In the early days of the Internet, general website application traffic was relatively small and did not require too many applications, so only one application was used to deploy all functional codes together, which not only reduced development and deployment, but also And can save the cost of maintenance.
Advantages: The project structure is simple, suitable for small projects, and the development cost is low; the project is deployed on one node, which is easy to maintain.
Disadvantages: The system functions are all concentrated in one engineering monkey, which is not easy to develop and maintain for medium and large projects; the coupling between system modules is high, and the single-point fault tolerance rate is low; it cannot be optimized and horizontally expanded for different modules.
2. Vertical application architecture
Split an original application into several independent applications to improve efficiency.
Advantages: System splitting realizes traffic sharing, solves concurrent problems, and can optimize and expand horizontally for different modules; problems in one system will not affect other systems, improving the fault tolerance rate.
Disadvantages: The systems are independent of each other, they cannot call each other, and there will be repeated development tasks.
3. Distributed architecture
The project will be split into two parts: the presentation layer and the service layer. The service layer contains business logic. The presentation layer only needs to process the interaction with the page. The business logic is realized by calling the services of the service layer.
Advantages: extract common functions as service layer, improve code reusability.
Disadvantages: The degree of coupling between systems becomes high, the calling relationship is intricate, and it is difficult to maintain.
4. SOA architecture
A call center manages the cluster in real time, and it is the key for resource scheduling and governance center (service-oriented architecture).
Advantages: Using the registration center solves the automatic adjustment of the calling relationship between services.
Disadvantages: There are dependencies between services. Once a certain link goes wrong, it will have a great impact (service avalanche); the service relationship is complicated, and it is difficult to operate and maintain, test and deploy.
5. The microservice framework
is to some extent the next step in the continued development of the service-oriented architecture SOA, which puts more emphasis on the "complete split" of services.
Advantages: services are atomically split, packaged, deployed and upgraded independently, ensuring clear task division of each microservice, which is conducive to expansion; microservices use restful and other lightweight http protocols to call each other.
Disadvantages: The technical cost of distributed system development is high (fault tolerance, distributed transactions, etc.).

2. Microservice architecture 1. Commonly used service governance and registry management
of microservice architecture . restful, rpc to call. Gateways control client access. Fault tolerance After a problem occurs, perform self-processing. Link tracking can be used for troubleshooting after a problem occurs. 2. The concept of microservice architecture 2.1. Service governance is the automatic management of services, the core of which is the automatic registration and discovery of services. Service registration: The service instance registers its own service information to the registration center. Service discovery: The service instance obtains the information of the service instance registered in it through the registration center, and uses this information to request the services provided by them. Service removal: The service registration center will automatically remove the problematic service from the available list, so that it will not be called. 2.2. Service call, HTTP-based RESTful interface and TCP-based RPC protocol REST: This is an HTTP call format, which is more standard and more general, and no matter which language supports the http protocol. RPC: A method of inter-process communication that allows remote services to be called as if they were local services. The main goal of the RPC framework is to make remote service calls simpler and more transparent. The RPC framework is responsible for shielding the underlying transmission method, serialization method and communication details. 2.3. Service gateway, API gateway is to connect all API calls to the API gateway layer uniformly, and the gateway layer uniformly connects and outputs. The basic functions of the gateway include unified access, security protection, protocol adaptation, traffic control, long and short link support, and fault tolerance. 2.4. Service fault tolerance. In microservices, a request often involves calling several services. If one of the services is unavailable without service fault tolerance, it is very likely that a series of services will be unavailable. This is the avalanche effect .
















The three core ideas of service fault tolerance:
not affected by the external environment,
not crushed by upstream requests ,
not crushed by downstream responses
Multiple services are often involved. Therefore, it is necessary to perform logging, performance monitoring, or link tracking for multiple service links requested at one time.
2.6. Solutions for microservice architecture
2.6.1. ServiceComb, which provides a one-stop open source solution for microservices, is committed to helping enterprises, users and developers to easily microservice enterprise applications to the cloud, and to realize microservice Efficient operation and maintenance management of applications.
2.6.2, SpringCloud is a collection of a series of frameworks, which subtly simplifies the development of distributed infrastructure by using the development convenience of SpringBoot, such as service discovery registration, configuration center, message bus, load balancing, circuit breaker, data monitoring, etc. , can use the SpringBoot development style to achieve one-click startup and deployment.
2.6.3. SPringCloud Alibaba provides a one-stop solution for microservice development, including the necessary components for developing distributed application microservices

3、SpringCloud Alibaba
	3.1、主要功能
		服务限流降级:默认支持WebServlet、WebFlux、OpenFeign、RestTemplate、Spring Cloud Gateway、Zuul、Dubbo和RocketMQ限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级Metrics监控。
		服务注册与发现:适配Spring Cloud服务注册与发现标准,默认集成了Ribbon的支持。
		分布式配置管理:支持分布式系统中的外部化配置,配置更改时自动刷新。
		消息驱动能力:基于Spring CLoud Stream为微服务应用构建消息驱动能力。
		分布式事务:使用@GlobalTransactional注解,高效并且对业务零侵入地解决分布式事务问题。
		阿里云对象存储:阿里云提供的海量、安全、低成本、高可靠的云存储服务,支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。
		分布式任务调度:提供秒级、精准、高可靠、高可用的定时(基于Cron表达式)任务调度服务。同时提供分布式的任务执行模型。
		阿里云短信服务:覆盖全球的短信服务,友好、高效、智能的互联网通信能力,帮助企业迅速搭建客户通道。
	3.2、组件
		Sentinel:把流量作为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
		Nacos:一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
		RocketMQ:一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。
		Dubbo:Apache Dubbo是一款高性能Java RPC框架。
		Seata:阿里巴巴开源产品,一个易于使用的高性能微服务分布式事务解决方案。
		Alibaba Cloud ACM:一款在分布式架构环境中应用配置进行集中管理和推送的应用配置中心产品。
		Alibaba Cloud OSS:阿里云对象存储对象,是阿里云提供的海量、安全、低成本、高可靠的云存储服务。
		Alibaba Cloud SchedulerX:一款分布式任务调度产品,提供秒级、精准、高可靠、高可用的定时任务调度服务。
		Alibaba Cloud SMS:覆盖全球的短信服务,友好、高效、智能的互联网通讯能力,帮组企业迅速搭建客户通道

3. Nacos Discovery—Service Governance
1. Service governance is the core and basic module in the microservice architecture, which is used to realize the automatic registration and discovery service registration of each microservice
: in the service governance framework, a registration center will be built. A service unit registers the details of the services it provides with the registration center, and forms a list of services in the registration center. The service registration center needs to monitor whether the services in the list are available in a heartbeat manner. If not, it needs to be in the service list. Remove unavailable services.
Service discovery: The service caller consults the service registration center for services, obtains a list of all service instances, and realizes access to specific service instances.
The commonly used registration center
Zookeeper is a distributed service framework and a sub-project of Apache Hadoop, which is mainly used to solve some data management problems often encountered in distributed applications.
Eureka is an important component in SpringCloud Netflix, and its main function is service registration and discovery.
Consul is an open source tool developed based on the GO language. It mainly provides functions of service registration, service discovery and configuration management for distributed and service-oriented systems. The functions of Consul are very practical, including: service registration/discovery, health check, key/value storage, multi-data center and distributed consistency guarantee, etc. Nacos is a dynamic service discovery, configuration management
and The service management platform is one of the components of SpringCloud Alibaba, responsible for service registration discovery and service configuration
2. nacos provides a set of easy-to-use feature sets, express delivery realizes dynamic service discovery, service configuration, service metadata and traffic management

3、实现服务调用的负载均衡,就是将负载(工作任务、访问请求)进行分摊到多个操作单元(服务器、组件)上进行执行;分为服务器负载均衡、客户端负载均衡。
4、基于Ribbon实现负载均衡
	Ribbon是SpringCloud的一个组件,使用一个注解就能轻松的搞定负载均衡;在RestTemplate的生成方法上添加@LoadBalanced注解;Ribbon内置了多种负载均衡策略,顶级接口为com.netfix.loadbalancer.IRule
5、基于Feign实现服务调用
	Feign是Spring Cloud提供的一个声明式的伪http客户端,它使得调用远程服务就像调用本地服务一样简单,只需要创建一个接口并添加一个注解即可。
	Nacos很好的兼容了Feign,Feign默认集成了Ribbon,所以在Nacos下使用Feign默认就实现了负载均衡的效果

4. Sentinel-service fault tolerance
1. Service avalanche effect
In a distributed system, due to network reasons or its own reasons, services generally cannot be guaranteed to be 100% available. If there is a problem with a service, thread blocking will occur when calling this service , if there is a large influx of requests at this time, multiple threads will be blocked and waiting, which will cause the service to be paralyzed.
Due to the dependencies between services, faults will propagate and have catastrophic consequences for the entire microservice system, which is the avalanche effect of service faults.
2. Common fault-tolerant solutions
1) Isolation means that the system is divided into several service modules according to certain principles. Each module is relatively independent and has no strong dependencies. When a fault occurs, the problem and impact can be isolated in a certain Inside the module, risks will not spread, other modules will not be affected, and the overall system services will not be affected. Common isolation methods include thread pool isolation and semaphore isolation.
2) Timeout. When the upstream service calls the downstream service, set a maximum response time. If the downstream service does not respond after exceeding this time, the request will be disconnected and the thread will be released.
3) Current limiting is to limit the input and output flow of the system to achieve the purpose of protecting the system. In order to ensure the stable operation of the system, once the threshold that needs to be limited is reached, it is necessary to limit the flow and take a small amount of measures to complete the flow limit. Purpose.
4) Fuse. When the downstream service responds slowly or fails due to excessive access pressure, the upstream service can temporarily cut off the call to the downstream service in order to protect the overall availability of the system. This measure of sacrificing part and preserving the whole is called fusing.
Generally, there are three states of service fuse.
Fuse off state: When the service is not faulty, the state of the fuse is in, and there is no restriction on the calling of the caller.
Fuse is enabled: Subsequent calls to the service interface do not go through the network, and directly execute the local fallback method
Semi-fuse state: try to restore the service call, allow limited traffic to call the service, and monitor the success rate of the call. If the success rate reaches the expected value, it means that the service has been restored and enters the fuse shutdown state. If the success rate is still low, restart Entering the circuit breaker off state
5), downgrading, is to provide a backing plan for the service, once the service cannot be called normally, the backing plan is used.
The common fault-tolerant component
Hystrix 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.
Resilience4J is a very lightweight, simple, and very clear and rich documentation fusing tool.
Sentinel is a circuit breaker implementation open sourced by Alibaba, which is very stable.
3. Introduction to Sentinel
1), Sentinel (distributed system traffic guard) is a comprehensive solution for service fault tolerance. Protect the stability of the service.
Sentinel has the following features:
Rich application scenarios, such as seckill, message peak shaving, cluster flow control, real-time fusing of downstream unavailable applications Complete real-time
monitoring, through the console, you can see the second-level data of a single cluster connected to the application
Extensive open source ecology, providing out-of-the-box integration modules with other open source frameworks/libraries
Perfect SPI extension point, providing easy-to-use, perfect SPI extension interface, you can quickly customize the logic by implementing the extension interface
2), Sentinel It is divided into two parts.
The core library does not depend on any framework/library, can run in all java runtime environments, and has good support for frameworks such as Dubbo/SpringCLoud
The console is developed based on SpringBoot and can be run directly after packaging without
additional application containers such as Tomcat
sentinel-dashboard-jar sentinel-dashboard-1.8.6.jar
adds dependencies to pom.xml

com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel

Sentinel provides a lightweight console that provides machine discovery, Stand-alone resource real-time monitoring and rule management functions.
5. Concepts and functions of Sentinel
1) Basic concepts:
resources are what Sentinel wants to protect, any content in a Java application, it can be a service, a method, or even a piece of code.
Rules are used to define how to protect resources, mainly including flow control rules, fuse downgrade rules, and system protection rules.
2), important function:
flow control, used to adjust the data of the network packet, the request arriving at any time is often random and uncontrollable, and the processing capacity of the system is limited, the flow needs to be controlled according to the processing capacity of the system, Sentinel As an adjuster, it can adjust random requests into suitable shapes as needed.
Fuse downgrading, when an unstable performance of a resource in the call link is detected, two methods will be adopted:
By limiting the number of concurrent threads, Sentinel reduces the impact of unstable resources on other resources by limiting the number of concurrent threads of resources. When a resource is unstable, such as the response time becomes longer, the direct impact on resources is It will cause the number of threads to gradually accumulate. When the number of threads accumulates to a certain amount on a specific resource, new requests for the resource will be rejected, and the accumulated threads will continue to receive requests after completing their tasks.
Degrade resources through response time, and you can quickly downgrade unstable resources through response time. When the response time of dependent resources is too long, all access to the resource will be directly rejected until the specified time window Only then resumed.
System load protection, providing self-adaptive protection capabilities in the system dimension. When the system load is high, if the request is continued to be accessed, the system may crash and fail to respond. In a cluster environment, the machine that should have been hosted will be The traffic is forwarded to other machines. If other machines are also in a marginal state at this time, Sentinel provides an object protection mechanism to balance the system's ingress traffic and system load to ensure that the system is within the capacity range. Handle the most requests within.
6. Sentinel rules
1), flow control rules, the principle of flow control is to monitor the QPS of application traffic or the number of concurrent threads and other indicators, and control the flow when the specified threshold is reached, so as to avoid being overwhelmed by the instantaneous traffic peak, thus Ensure high availability of applications.
Resource name: unique name, the default is the request path, which can be customized
For the source: specify which microservice to limit the flow, the default is default, regardless of the source, all restrictions
Threshold type/stand-alone threshold:
QPS (request data volume per second) : When the QPS calling this interface reaches the threshold, limit the flow.
Number of threads: When the number of threads calling the interface reaches the threshold, limit the flow

Guess you like

Origin blog.csdn.net/lssffy/article/details/131326814