Traffic components of Sentinel distributed system
Sentinel is an open source project of Alibaba. It provides multiple dimensions such as flow control, fuse degradation and system load protection to ensure the stability between services.
Official website: https://github.com/alibaba/Sentinel/wiki
1. Main characteristics
2. Open source ecology
3. Basic concepts of Sentinel
resources
Resource is a key concept of Sentinel. It can be anything in a Java application, for example, a service provided by an application,
Or services provided by other applications called by the application, or even a piece of code. In the following documents, we will use resources to describe code blocks.
As long as the code defined through Sentinel API is a resource, it can be protected by Sentinel. In most cases, you can use square
Legal signatures, URL s, and even service names are used as resource names to identify resources.
rule
The rules set around the real-time state of resources can include flow control rules, fuse degradation rules and system protection rules. All rules can be dynamically adjusted in real time.
4. Sentinel's function and design concept
flow control
Flow control is a common concept in network transmission. It is used to adjust the sending data of network packets. However, from the perspective of system stability
In addition, there are many concerns about the speed of processing requests. The request coming at any time is often random and uncontrollable, and the system is at the same time
Management ability is limited. We need to control the flow according to the processing capacity of the system. Sentinel, as a scheduler, can adjust random requests to appropriate shapes as needed
Flow control has the following angles:
The calling relationship of resources, such as the calling link of resources and the relationship between resources; Operation indicators, such as QPS, thread pool, system load, etc;
Control effects, such as direct current limiting, cold start, queuing, etc. Sentinel's design concept is to let you freely choose the angle of control and make flexible combination to achieve the desired effect.
Fuse degradation
In addition to flow control, timely fusing unstable factors in the call link is also one of Sentinel's missions. Due to calling relationship
If a resource in the call link is unstable, it may lead to request accumulation and cascade errors.
On the means of restriction:
Sentinel has taken two approaches to this problem:
Limit by number of concurrent threads
Unlike the resource pool isolation method, Sentinel reduces the impact of unstable resources on other resources by limiting the number of concurrent threads of resources
Ring. In this way, there is no loss of thread switching, and you do not need to pre allocate the size of the thread pool. When a resource is unstable
For example, if the response time becomes longer, the direct impact on resources is the gradual accumulation of threads. When the number of threads accumulates on a specific resource
After a certain amount, new requests for the resource will be rejected. The stacked thread does not continue to receive requests until it completes its task.
Degrade resources by response time
In addition to controlling the number of concurrent threads, Sentinel can also quickly degrade unstable resources through response time. When relying on resources
When the response time of the source is too long, all access to the resource will be directly denied until the specified time window has elapsed
Reply.
Sentinel also provides adaptive protection in the system dimension. Preventing avalanche is an important part of system protection.
Sentinel's main working mechanism is as follows:
Provide API s for adaptation or display for mainstream frameworks to define resources to be protected, and provide facilities for real-time statistics and call link analysis of resources.
According to the preset rules, combined with the real-time statistical information of resources, the flow is controlled. At the same time, sentinel provides an open interface to facilitate you to define and change rules. Sentinel
Provide real-time monitoring system to facilitate you to quickly understand the current system status.
Sentinel HelloWorld implementation
1. Create parent maven empty project, ---- sentineltest
Import dependency:
<modules> <module>sentinel-springcloud</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.bdqn</groupId> <artifactId>sentineltest</artifactId> <packaging>pom</packaging> //Remember to add the packaging tag here. The parent type is pom type <version>0.0.1-SNAPSHOT</version> <name>sentineltest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-cloud.version>2020.0.4</spring-cloud.version> <springboot.version>2.5.5</springboot.version> <springcloudalibaba.version>2021.1</springcloudalibaba.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${springboot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${springcloudalibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. Create a new module - sentinel HelloWorld
Import dependency:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2021.1</version> </dependency>
2. application.yml configuration file:
server: port: 8082 servlet: context-path: / spring: application: name: sentinel-helloWorld cloud: sentinel: transport: dashboard: localhost:8080
Create a new startup class SentinelHelloworldApplication:
@SpringBootApplication @EnableAsync //Support asynchronous call public class SentinelHelloworldApplication { public static void main(String[] args) { SpringApplication.run(SentinelHelloworldApplication.class, args); } }
3. Create SentinelHelloWorld in the controller layer: define rules and use current limiting rules
/** * @author Jiang Jiangjiang * @create 2021/10/12 15:00 */ @RestController public class SentinelHelloWorld { /** * Define current limiting rules * PostConstruct After the construction method executes the method definition and loads the current limiting rules */ @PostConstruct public void initFlowRules(){ List<FlowRule> rules=new ArrayList<>(); //Define current limiting rule set FlowRule rule=new FlowRule(); //Define current limiting rules rule.setResource("helloworld"); rule.setGrade(RuleConstant.FLOW_GRADE_QPS); //Define the type of current limiting rule rule.setCount(2); //Define the maximum number of requests passed by QPS threshold per second rules.add(rule); //Add rule to collection FlowRuleManager.loadRules(rules); //Load rule collection } }
4. Test:
Sentinel dashboard console construction
Sentinel console is the entrance for unified configuration and management of flow control and degradation rules. It provides users with machine self discovery and cluster points
Link self discovery, monitoring, rule configuration and other functions. On the Sentinel console, we can configure rules and view the flow control effect in real time
Fruit.
1. Download sentinel-dashboard-1.8.1.jar
https://github.com/alibaba/Sentinel/releases/download/1.8.1/sentinel-dashboard-1.8.1.jar
2. JDK version 1.8 and above is required to start Sentinel console.
java -jar sentinel-dashboard-1.8.2.jar
You can also use this command
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -
Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar
3, http://localhost:8080/ The access user name and password are sentinel
4. Client sentinel HelloWorld module
Create AspectJ configuration class
@Configuration public class SentinelResourceAspectConfiguration { @Bean public SentinelResourceAspect sentinelResourceAspect(){ return new SentinelResourceAspect(); } }
controller layer SentinelHelloWorld
@RestController public class SentinelHelloWorld { @Resource private AsyncService asyncService; @RequestMapping("helloworld") public String helloWorld(){ try(Entry entry= SphU.entry("HelloWorld")){ //Use current limiting to watch HelloWorld return "Sentinel Hello!"+System.currentTimeMillis(); } catch (Exception e) { e.printStackTrace(); return "System busy,Please wait!"; //handle at a lower grade } } /** * The return value boolean Defines the resource * @return */ @RequestMapping("helloWorld2") public String helloWorld2(){ //Resource names can use strings in any business language if(SphO.entry("HelloWorld2")){ //Make sure finally is executed try { /** * Protected business logic */ return "Sentinel2 Hello!"+System.currentTimeMillis(); }finally { SphO.exit(); } }else { //Resource access is blocked, restricted or degraded //Carry out corresponding processing operations return "System busy,Please wait!"; //handle at a lower grade } } /** * Define resources by annotation * @return */ @SentinelResource(value = "helloWorld3",blockHandler = "blockHandlerForHelloWorld3") @RequestMapping("helloWorld3") public String helloWorld3(){ return "Sentinel3 Hello!by Annotation method@SentinelResource"+System.currentTimeMillis(); } /** * Called when the original method call is current limited, degraded or system protected * @param ex * @return */ public String blockHandlerForHelloWorld3(BlockException ex){ ex.printStackTrace(); return "System busy,Please wait!"; } /** * Asynchronous Support */ @RequestMapping("helloWorld4") public void helloWorld4(){ AsyncEntry asyncEntry=null; try { asyncEntry =SphU.asyncEntry("helloWorld4"); asyncService.doSomethingAsync(); }catch (BlockException e){ System.out.println("System busy,Please wait!"); }finally { if(asyncEntry!=null){ asyncEntry.exit(); } } } }
5. Create AsyncService asynchronous call classes and methods
@Service public class AsyncService { @Async public void doSomethingAsync(){ System.out.println("async start...."); try { Thread.sleep(400); }catch (InterruptedException e){ e.printStackTrace(); } System.out.println("async end...."); } }
6. Create a new sentinel spring cloud module
Import dependency:
sentinel integrates nacos to realize configuration persistence
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2021.1</version> </dependency> <--sentinel integration nacos Implement configuration persistence--!> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
Configure the application.yml file
server: port: 8082 servlet: context-path: / spring: application: name: sentinel-springcloud #Set app name cloud: sentinel: transport: dashboard: localhost:8080 #Set the host address and port of sentinel connection console datasource: ds: nacos: server-addr: localhost:8848 #nacos connection address group-id: DEFAULT_GROUP #Grouping of nacos connections rule-type: flow #The flow control rule rule rule type configuration indicates which type of rule (flow, grade, authority, system, param flow, GW flow, GW API Group) the rules in the data belong to data-id: java1234_sentinel # Read the data ID of the configuration file data-type: json # The type of training file read is json
controller layer SentinelHelloWorld class
@RestController public class SentinelHelloWorld { /** * Define resources by annotation * @return */ @SentinelResource(value = "helloWorld_springcloud",blockHandler = "blockHandlerForHelloWorld3") @RequestMapping("helloWorld3") public String helloWorld3(){ return "Sentinel3 Hello!by Annotation method@SentinelResource"+System.currentTimeMillis(); } /** * Called when the original method call is current limited, degraded or system protected * @param ex * @return */ public String blockHandlerForHelloWorld3(BlockException ex){ ex.printStackTrace(); return "System busy,Please wait!"; } }
6. Test successful
Enter sentinel console to monitor and edit in real time
The principle of flow control is to monitor the QPS of application traffic or the number of concurrent threads. When the specified threshold is reached
Control the flow to avoid being overwhelmed by the instantaneous flow peak, so as to ensure the high availability of the application.
Current limiting types are divided into:
QPS: limit of requests per second
Number of threads: resource usage thread limit
Flow control mode
Direct: direct resource flow restriction, which is a simple flow restriction.
Association: the association mode needs to fill in the path of the associated resource, which means that if the flow of the associated resource exceeds the limit, the flow will be limited (self funded)
Source name (filled in path).
Link: if it is a link mode, you need to fill in the entry resource to limit the call of the entry resource to yourself.
Flow control effect
Fast failure: (Ruleconstant.CONTROL_BEHAVIOR_DEFAULT) is the default flow control mode. When QPS exceeds
After the threshold of any rule, the new request will be rejected immediately by throwing F1owException. This method is applicable to the system
When the system processing capacity is exactly known, such as when the accurate water level of the system is determined through pressure measurement.
Warm Up: (RuleConstant.CONTROL_BEHAVIOR_WARM_UP) mode, i.e. warm-up / cold start mode. When the system is long-term
Under the condition of low water level, when the flow suddenly increases, directly pull the system to high water level, which may crush the system instantly
Let the flow through slowly increase and gradually increase to the upper limit of the threshold within a certain time, so as to give the cooling system a warm-up time and avoid the cold system
The system was crushed.
Queue waiting: (RuleConstant.cONTROL_BEHAV or_rate_limit) mode will strictly control the interval when the request passes
That is, let the request pass at a uniform speed, corresponding to the leaky bucket algorithm.
The same resource can have multiple flow restriction rules at the same time, which will be checked in turn when checking the rules.
Fusing rules
It is necessary to fuse and downgrade unstable weak dependent service calls and temporarily cut off unstable calls,
Avoid the overall avalanche caused by local instability factors. Fuse degradation, as a means to protect itself, is usually configured at the client (caller)
Set.
Start the sentinel spring cloud project. It is found that the sentinel console reads the configuration in nacos. Frequent refresh can also flow control;