↓↓↓ registration method: (slide to the bottom of this page)
🍅 Supporting articles for Java learning route: Summary of java learning route, brick movers counter attack Java Architects (the strongest in the whole network)
🍅 Basic recommendations: Java foundation tutorial series
🍅 Practical recommendations: Spring Boot basic tutorial
🍅 Introduction: high quality creator in Java field 🏆, CSDN the author of the official account of the Na Zha ✌ , Java Architect striver 💪
🍅 Scan the QR code on the left of the home page, join the group chat, learn and progress together
🍅 Welcome to praise 👍 Collection ⭐ Leaving a message. 📝
1, Baidu Encyclopedia
Sentinel is a highly available traffic protection component for distributed service architecture. It mainly takes traffic as the starting point to help developers ensure the stability of micro services from multiple dimensions such as current limiting, traffic shaping, fuse degradation, system load protection and hotspot protection.
1. Sentinel properties
Rich application scenarios: Sentinel has undertaken the core scenarios of Alibaba's double 11 traffic promotion in recent 10 years, such as second kill (i.e. sudden traffic control within the range of system capacity), message peak cutting and valley filling, cluster traffic control, real-time fuse downstream unavailable applications, etc.
Complete real-time monitoring: Sentinel also provides real-time monitoring function. You can see the second level data of a single machine connected to the application in the console, and even the summary operation of clusters with a scale of less than 500.
Extensive open source Ecology: Sentinel provides out of the box integration modules with other open source frameworks / libraries, such as Spring Cloud, Dubbo and gRPC. You can quickly access Sentinel by introducing corresponding dependencies and simple configuration.
Perfect SPI extension point: Sentinel provides simple, easy-to-use and perfect SPI extension interface. You can quickly customize logic by implementing extension interfaces. For example, custom rule management, adapting dynamic data sources, etc.
2. Sentinel's open source ecosystem
2, Sentinel's history
Sentinel was born in 2012. Its main function is inlet flow control.
From 2013 to 2017, sentinel developed rapidly within Alibaba group and became a basic technology module, covering all core scenarios. Sentinel has therefore accumulated a large number of traffic consolidation scenarios and production practices.
In 2018, Sentinel was open source and continued to evolve.
In 2019, Sentinel continued to explore the direction of multilingual expansion and launched the native version of C + +. At the same time, it also launched Envoy cluster flow control support for Service Mesh scenarios to solve the problem of multilingual flow restriction under Service Mesh architecture.
In 2020, Sentinel Go will be launched and continue to evolve towards cloud native.
3, Sentinel basic concepts
1. 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 a service provided by other applications called by an 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 method signatures, URL s, or even service names as resource names to identify resources.
2. Rules
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 advantages
Friendly control panel
Support real-time monitoring
Support a variety of current limiting, such as QPS current limiting, number of threads and a variety of current limiting strategies
It supports multiple degradation modes, degradation by average return time, degradation by multiple exception numbers, degradation by exception proportion, etc
It is convenient for extension development and supports SPI mode to extend the chain
It supports link association, and can realize current limiting according to link statistics, system protection, popular resource protection, etc
5, Sentinel function and design concept
1. What is 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, there are also many concerns about the speed of processing requests. Requests coming at any time are often random and uncontrollable, and the processing capacity of the system 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, as shown in the following figure:
2. Flow control design concept
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.
3. What is fuse degradation
In addition to flow control, timely fusing unstable factors in the call link is also one of Sentinel's missions. Due to the complexity of the call relationship, if a resource in the call link is unstable, requests may accumulate, resulting in cascading errors.
Sentinel and Hystrix have the same principle: when it is detected that a resource in the call link is unstable, such as long request response time or an increase in the proportion of exceptions, the call of this resource is limited to make the request fail quickly, so as to avoid affecting other resources and causing cascading failures.
Here is a reference to the elastic engineering work of Hystrix from Netflix API team in 2011. In 2012, Hystrix continued to grow and mature, and many teams within Netflix adopted it. Today, hundreds of billions of thread isolation and thousands of signal isolation calls are executed in Netflix every day, which greatly improves the running time and elasticity.
4. Fuse degradation design concept
Sentinel and Hystrix take a completely different approach to the means of restriction.
Hystrix isolates dependencies (corresponding resources in Sentinel's concept) through thread pool isolation. The advantage of this is the most complete isolation between resources. The disadvantage is that in addition to increasing the cost of thread switching (too many thread pools lead to too many threads), it also needs to allocate the thread pool size of each resource in advance, and there will be problems for some scenarios using ThreadLocal (such as Spring transactions).
Sentinel has taken two approaches to this problem:
(1) 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. 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, the response time becomes longer, the direct impact on the resource is the gradual accumulation of threads. When the number of threads accumulates to a certain number on a specific resource, new requests for that resource will be rejected. The stacked thread does not continue to receive requests until it completes its task.
(2) Demote resources for slow calls and exceptions
In addition to controlling the number of concurrent threads, Sentinel can also quickly control unstable calls according to unstable factors such as response time and exceptions. When the response time of the dependent resource is too long, all access to the resource will be directly denied until the specified time window is passed.
5. System adaptive protection
Sentinel also provides adaptive protection in the system dimension. Preventing avalanche is an important part of system protection. When the system load is high, if you continue to allow requests to enter, the system may crash and fail to respond. In the cluster environment, network load balancing will forward the traffic that should be carried by this machine to other machines. If other machines are also in an edge state at this time, the increased traffic will cause this machine to crash, and finally make the whole cluster unavailable.
In view of this situation, Sentinel provides a corresponding protection mechanism to balance the system inlet traffic and the system load, so as to ensure that the system can handle the most requests within its capacity.
6. How Sentinel works
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 provides a real-time monitoring system to facilitate you to quickly understand the current system status.
7. Comparison of competitive products
6, SpringBoot integrates sentinel
1. Join pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.2.1.RELEASE</version> </dependency>
2. Writing sentinel rules
@PostConstruct public void initFlowRules(){ //1. Create a collection of current limiting rules List<FlowRule> rules = new ArrayList<>(); //2. Create current limiting rules FlowRule rule = new FlowRule(); //Define a resource, indicating that sentinel will take effect on this resource rule.setResource("helloSentinel"); //Define the type of current limiting rule rule.setGrade(RuleConstant.FLOW_GRADE_QPS); //Defines the number of requests that QPS can pass per second rule.setCount(2); //3. Put the current limiting rules into the collection rules.add(rule); //4. Load current limiting rules FlowRuleManager.loadRules(rules); }
@GetMapping("/helloSentinel") public String hello(){ try{ Entry entry = SphU.entry("helloSentinel"); return "helloSentinel success."; }catch (Exception e){ log.error(e.getMessage()); return "helloSentinel error"; } }
3. Testing
(1) When the frequency is less than 1 second, the access is successful
(2) When the frequency is greater than 1 second twice, it fails due to the establishment of current limiting rules
7, springboot integrates sentinel dashboard
1. Installing sentinel dashboard
(1) Download jar package
Sentinel-1.7.1 jar package download link
Link: https://pan.baidu.com/s/1reC7C4sOb4eu2_O3BPBcKg
Extraction code: 11gm
(2) Start the jar package
java -Dserver.port=9000 -jar sentinel-dashboard.jar
2. springboot integrates sentinel dashboard
(1) Introducing pom
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-transport-simple-http</artifactId> <version>1.7.2</version> </dependency>
(2) Configure startup class
-Dcsp.sentinel.dashboard.server=localhost:9000 -Dproject.name=GooReeyProject
(3) Call the interface to display it on the dashboard
3. Configuring flow control with sentinel dashboard
(1) Comment out the flow control setting code in the code
(2) Replace the code with the configuration of the console
8, Other ways of defining resources in sentinel
1. Define resources by throwing exceptions
@GetMapping("/helloSentinel") public String hello(){ try{ Entry entry = SphU.entry("helloSentinel"); return "helloSentinel success."; }catch (Exception e){ log.error(e.getMessage()); return "helloSentinel error"; } }
2. Returns a Boolean defined resource
Code example:
@GetMapping("/sentinelBoolean") public boolean sentinelBoolean(){ if(SphO.entry("sentinelBoolean")){ try { log.info("sentinelBoolean success."); return true; }finally { SphO.exit(); } }else{ log.warn("sentinelBoolean error."); return false; } }
3. sentinel supports the statistics of asynchronous call links
In asynchronous calls, you need to define resources through the SphU.asynvEntry(xxx) method, and you usually call the exit() method in the asynchronous callback function.
Coding:
(1) Add @ EnableAsync annotation to startup class
@EnableDiscoveryClient @SpringBootApplication(scanBasePackages = "com.guor") @MapperScan("com.guor.management.dao") @RefreshScope @EnableAsync public class ManagementApplication { public static void main(String[] args) { SpringApplication.run(ManagementApplication.class, args); } }
(3) Control layer
@GetMapping("/sentinelAsync") public void sentinelAsync(){ AsyncEntry asyncEntry = null; try { asyncEntry = SphU.asyncEntry("sentinelAsync"); userService.sentinelAsync(); log.info("sentinelAsync success."); }catch (BlockException e){ log.error("sentinelAsync error."); }finally { if(asyncEntry != null){ asyncEntry.exit(); } } }
When sentinel dashboard is not defined:
After sentinel dashboard is defined: flow control succeeded!
4. Define resources in annotation form
(1) pom file
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-annotation-aspectj</artifactId> <version>1.7.2</version> </dependency>
(2) Configuration class
package com.guor.management.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect; @Configuration public class SentinelAspectConfig { @Bean public SentinelResourceAspect sentinelResourceAspect(){ return new SentinelResourceAspect(); } }
(3) Control class
@SentinelResource(value = "sentinel_Annotation", blockHandler = "exceptionHandler") @GetMapping("/annotation") public String sentinelAnnotation(){ return "sentinelAnnotation"; } public String exceptionHandler(BlockException e){ e.printStackTrace(); return "The system is busy, please wait"; }
(4) Define flow control rules
9, Nova project
1. About event time & prizes & requirements
For more event details, click Season 2 Nova program
2. Nezha community
- Vue.js family bucket zero foundation introduction to advanced project practice
- Java high concurrency programming guide
- 100 million traffic Java high concurrency and network programming practice
- Node.js Getting Started Guide
Previous: [Spring Boot 28] Spring Boot integrates easyExcel
Next: SpringBoot learning route summary, follow the route and don't get lost (with mind map)