当前位置:网站首页>Sentinel fusing and current limiting
Sentinel fusing and current limiting
2022-07-26 03:53:00 【RB_ VER】
summary
sentinel Take flow as the starting point , Slave flow control 、 Fusing the drop 、 Multiple dimensions such as system load protection protect the stability of services .
sentinel The main features :
sentinel The component consists of two parts ( The front desk + backstage ):
- Core library (Java client ) Don't rely on any framework / library , Can run on all Java Runtime environment , At the same time Dubbo/spring cloud And other frameworks also have better support .
- Console (dashboard) be based on spring boot Development , It can run directly after packing , No additional tomcat Etc. Application containers .
download sentinel-dashboard-1.7.0.jar, need Java8 Environment and 8080 The port is not occupied , Run the command :
java -jar sentinel-dashboard-1.7.0.jar
Sign in sentinel Management interface http://localhost:8080, The account and password are sentinel.
Initialization project
start-up nacos8848.
newly build cloudalibaba-sentinel-service8401 modular .
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080 # To configure sentinel dashboard Address
port: 8719 # Default 8719 port , If occupied, then +1 scanning
management:
endpoints:
web:
exposure:
include: '*'
MainApp8401
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401 {
public static void main(String[] args) {
SpringApplication.run(MainApp8401.class,args);
}
}
FlowLimitController
@RestController
public class FlowLimitController {
@GetMapping("/testA")
public String testA() {
return "----testA";
}
@GetMapping("/testB")
public String testB() {
return "----testB";
}
}
sentinel Use lazy loading mechanism , Perform an access http://localhost:8401/testA

Flow control rules

Resource name : Unique name , Default request path .
For source :sentinel You can limit the flow for the caller , Fill in the microservice name , Default default( Don't differentiate between sources ).
Threshold type / Stand alone threshold :
- QPS( Requests per second ): When calling the API Of QPS When the threshold is reached , Carry out current limiting .
- Number of threads : When calling the API When the number of threads reaches the threshold , Carry out current limiting .
Cluster or not : No need for clustering .
Flow control mode :
- direct :API When the current limiting condition is reached , Direct current limiting .
- relation : When the associated resource reaches the threshold , Just limit yourself .
- link : Only the traffic on the specified link is recorded ( Specify the flow of resources from the entry resources , If the threshold is reached , Just limit the flow )
Flow control effect :
- Fast failure : Direct failure , Throw exceptions .
- Warm Up: according to codeFactor( Cold loading factor , Default 3) Value , From threshold /codeFactor Start , After preheating for a long time , To achieve the set QPS threshold .
- Waiting in line : Line up at a constant speed , Let the request pass at a constant speed , Threshold type must be set to QPS, Otherwise it will not work .
QPS Direct quick failure
Configure flow control rules :

qps exceed 1 Then the current is limited .
relation
When associated resources /testB Of qps Threshold exceeded 1 after , Just limit the current /testA own .
preheating

The threshold for 10, Preheating duration setting 5 second , The threshold for system initialization is 10/3 About equal to 3, That is, the threshold at the beginning is 3, And then after 5 After a few seconds, the threshold slowly rises to 10.
The second kill system starts at the moment , There will be a lot of traffic coming up , It's possible to kill the system , The purpose of preheating is to protect the system , Slowly put the flow in .
Waiting in line

Line up at a constant speed , Let the request pass at an even rate , The threshold type must be set to QPS, Otherwise it will not work . The corresponding is the leaky bucket algorithm .

/testA Per second 1 Requests , If you time out, wait in line , The waiting timeout is 20000 millisecond .
Demotion rules

RT( Mean response time , Second level ): Requests that have an average response time that exceeds the threshold and pass through the time window >=5, When both conditions are met, degradation is triggered , Close the circuit breaker after the window period ,RT Maximum 4900( The greater need to pass -Dcsp.sentinel.statistic.max.rt=XXXX To take effect ).
Abnormal proportion ( Second level ):QPS>=5 And the abnormal proportion ( Second statistics ) Threshold exceeded , Trigger downgrade ; After the time window , Turn off demotion .
Number of abnormal ( Minutes of class ): Number of abnormal ( Minute count ) Threshold exceeded , Trigger downgrade ; After the time window , Turn off demotion .
sentinel Fuse degradation will occur when a resource in the call link is unstable ( For example, the call timeout or the exception ratio increases ), Restrict calls to this resource , Let the request fail quickly , Avoid cascading errors that affect other resources .
When resources are demoted , Within the degradation time window of the next class , Calls to this resource are automatically blown ( The default behavior is to throw DegradeException).
sentinel The circuit breaker is not half open .
In the half open state, the system automatically detects whether there is any abnormality in the request , If there is no abnormality, close the circuit breaker and resume use , If there is any abnormality, continue to open the circuit breaker and it is unavailable , May refer to hystrix.
RT

@GetMapping("/testD")
public String testD() {
try{
TimeUnit.SECONDS.sleep(1); } catch(InterruptedException e) {
e.printStackTrace(); }
return "----testD";
}
Set up sentinel:
To configure jmeter:
Call in a second 10 Thread calls testD, And a thread needs to handle 1 second , So the circuit breaker is open , Microservices are not available .
Abnormal proportion

@GetMapping("/testD")
public String testD() {
int a = 10 / 0;
return "----testD";
}

Number of abnormal


@GetMapping("/testD")
public String testD() {
int a = 10 / 0;
return "----testD";
}
At the beginning of the interview, an error was reported , After five error reports , Enter fuse degradation .
hotspot key Current limiting
Hot spots are frequently accessed data . Most of the time, we want to count the most frequently visited data in a hotspot TopK data , And restrict their access . such as :
- goods ID Is the parameter , Count the most frequently purchased items in a period of time ID And limit .
- user ID Is the parameter , For users who visit frequently over a period of time ID And limit .
Hot spot parameter current limiting will count the hot spot parameters in the incoming parameters , And according to the configured current limiting threshold and mode , Restrict the flow of resource calls with hotspot parameters . Hot spot parameter current limiting can be regarded as a special flow control , Only valid for resource calls with hotspot parameters .
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1",require = false) String p1, @RequestParam(value = "p2",require = false) String p2) {
return "testHotKey";
}
public String deal_testHotKey(String p1, String p2, BlockException ex) {
return "deal_testHotKey";
}

Method testHotKey The first parameter inside is just QPS More than per second 1 Time , Downgrade immediately .
Parameter exceptions
If desired p1 Parameter when it is a special value , Its current limit is not the same as usual . Suppose that p1 The value is equal to the 5 when , Its threshold can reach 200.

System rules
The system protection rule is to control the inlet flow at the application level , From a single machine load、CPU Usage rate 、 Average RT、 entrance QPS And the number of concurrent threads , Let the system run at the maximum throughput and ensure the overall stability of the system .
System protection rules apply the whole dimension , Not the resource dimension , And only effective for inlet flow . Inlet flow is the flow into the application , such as web Service or dubbo The request received by the server , All belong to the inlet flow .
System rules support the following patterns :
- load The adaptive ( Only on linux/unix-like The machine works ): Systematic load1 As an indicator of inspiration , Adaptive system protection . When the system load1 Exceed the set heuristic value , When the number of concurrent threads exceeds the estimated system capacity, system protection will be triggered (BBR Stage ). The system capacity is determined by the maxQps * minRt It is estimated that . The setting reference value is generally CPU cores * 2.5.
- CPU usage: When the system CPU When the utilization rate exceeds the threshold, system protection will be triggered ( Value range 0.0-1.0), It's sensitive .
- Average RT: When the average flow of all inlets on a single machine RT When the threshold value is reached, system protection is triggered , In milliseconds .
- Number of concurrent threads : When the number of concurrent threads of all the entrances on a single machine reaches the threshold, system protection will be triggered .
- entrance QPS: When all the inlet flow on a single machine QPS When the threshold value is reached, system protection is triggered .

@SentinelResource To configure
Current limiting by resource name
@RestController
public class RateLimitController {
@GetMapping(value = "/byResource")
@SentinelResource(value = "byResource", blockHandler = "handleException")
public CommonResult byResource() {
return new CommonResult(200,"OK",new Payment(2020L,"serial001"));
}
public CommonResult handleException(BlockException ex) {
return new CommonResult(444,ex.getClass().getCanonicalName());
}
}

Press URL Address restriction
@RestController
public class RateLimitController {
@GetMapping(value = "/rateLimit/byUrl")
@SentinelResource(value = "byUrl")
public CommonResult byUrl() {
return new CommonResult(200,"OK",new Payment(2020L,"serial002"));
}
}

Custom current limiting processing logic
RateLimitController
@RestController
public class RateLimitController {
@GetMapping(value = "/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handlerException2")
public CommonResult customerBlockHandler() {
return new CommonResult(200,"OK",new Payment(2020L,"serial002"));
}
}
CustomerBlockHandler
public class CustomerBlockHandler{
public static CommonResult handlerException1(BlockException ex) {
return new CommonResult(4441,ex.getClass().getCanonicalName());
}
public static CommonResult handlerException2(BlockException ex) {
return new CommonResult(4442,ex.getClass().getCanonicalName());
}
}

Service function
Basic environment construction
sentinel Integrate ribbon+openFeign+fallback
Service providers
establish cloudalibaba-provider-payment9003/9004 modular
server:
port: 9003
spring:
application:
name: nacos-payment-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
endpoints:
web:
exposure:
include: '*'
PaymentMain9003
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain9003{
public static void main(String[] args) {
SpringApplication.run(PaymentMain9003.class,args);
}
}
PaymentController
@RestController
public class PaymentController{
@Value("${server.port}")
private String serverPort;
public static HashMap<Long,Payment> hashMap = new HashMap<>();
static {
hashMap.put(1L,new Payment(1L,"111"));
hashMap.put(2L,new Payment(2L,"222"));
hashMap.put(3L,new Payment(3L,"333"));
}
@GetMapping("/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
Payment payment = hashMap.get(id);
CommonResult<Payment> result = new CommonResult(200,"ok"+serverPort,payment);
return result;
}
}
Serving consumers
establish cloudalibaba-consumer-nacos-order84 modular .
server:
port: 84
spring:
application:
name: nacos-order-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080
port: 8719
service-url:
nacos-user-service: http://nacos-payment-provider
OrderNacosMain84
@SpringBootApplication
@EnableDiscoveryClient
public class OrderNacosMain84{
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain84.class,args);
}
}
ApplicationContextConfig
@Configuration
public class ApplicationContextConfig{
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback")
public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException("IllegalArgumentException");
} else if (result.getData() == null) {
throw new NullPointerException("NullPointerException");
}
return result;
}
}
The service is not configured
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback")
public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException("IllegalArgumentException");
} else if (result.getData() == null) {
throw new NullPointerException("NullPointerException");
}
return result;
}
}
visit http://localhost:84/consumer/fallback/4 When to return to error Content .
The service only configures fallback
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback",fallback = "handlerFallback")
// fallback Only responsible for business exceptions
public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException("IllegalArgumentException");
} else if (result.getData() == null) {
throw new NullPointerException("NullPointerException");
}
return result;
}
public CommonResult<Payment> handlerFallback(@PathVariable("id") Long id, Throwable e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>("444"," The bottom of the bag is abnormal handlerFallback"+e.getMessage(),payment);
}
}
visit http://localhost:84/consumer/fallback/4 When to return to fallback Content .
The service only configures blockHandler
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", blockHandler = "blockHandler")
// fallback Only responsible for business exceptions
public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException("IllegalArgumentException");
} else if (result.getData() == null) {
throw new NullPointerException("NullPointerException");
}
return result;
}
public CommonResult<Payment> blockHandler(@PathVariable("id") Long id, BlockException e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>("444","BlockException "+e.getMessage(),payment);
}
}

One visit per second http://localhost:84/consumer/fallback/4 When to return to error Content .
Multiple visits per second http://localhost:84/consumer/fallback/4 When to return to blockHandler Content .
Service fuse configuration fallback and blockHandler
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler")
// fallback Only responsible for business exceptions
public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException("IllegalArgumentException");
} else if (result.getData() == null) {
throw new NullPointerException("NullPointerException");
}
return result;
}
public CommonResult<Payment> blockHandler(@PathVariable("id") Long id, BlockException e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>("444","BlockException "+e.getMessage(),payment);
}
public CommonResult<Payment> handlerFallback(@PathVariable("id") Long id, Throwable e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>("444"," The bottom of the bag is abnormal handlerFallback"+e.getMessage(),payment);
}
}

One visit per second http://localhost:84/consumer/fallback/1 Return normal data .
Multiple visits per second http://localhost:84/consumer/fallback/1 When to return to blockHandler Content .
One visit per second http://localhost:84/consumer/fallback/4 When to return to fallback Content .
Multiple visits per second http://localhost:84/consumer/fallback/4 When to return to blockHandler Content .
Service fuse configuration exceptionsToIgnore
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler", exceptionsToIgnore = {
IllegalArgumentException.class})
// fallback Only responsible for business exceptions
public CommonResult<Payment> fallback(@PathVariable("id") Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL+"/paymentSQL/"+id,CommonResult.class,id);
if (id == 4) {
throw new IllegalArgumentException("IllegalArgumentException");
} else if (result.getData() == null) {
throw new NullPointerException("NullPointerException");
}
return result;
}
public CommonResult<Payment> blockHandler(@PathVariable("id") Long id, BlockException e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>("444","BlockException "+e.getMessage(),payment);
}
public CommonResult<Payment> handlerFallback(@PathVariable("id") Long id, Throwable e) {
Payment payment = new Payment(id,"null");
return new CommonResult<>("444"," The bottom of the bag is abnormal handlerFallback"+e.getMessage(),payment);
}
}
If the IllegalArgumentException, No more fallback Methods , No degradation effect .
Service fuse configuration OpenFeign
modify cloudalibaba-consumer-nacos-order84 modular
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
# Activate sentinel Yes feign Support for
feign:
sentinel:
enable: true
OrderNacosMain84
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class OrderNacosMain84{
public static void main(String[] args) {
SpringApplication.run(OrderNacosMain84.class,args);
}
}
PaymentService
@FeignClient(value = "nacos-payment-provider",fallback = PaymentFallbackService.class)
public Interface PaymentService {
@GetMapping(value = "/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}
PaymentFallbackService
@Component
public class PaymentFallbackService implements PaymentService {
@Override
public CommonResult<Payment> paymentSQL(Long id) {
return new CommonResult<>("444"," Service degradation returns ",new Payment(id,"null"));
}
}
CircleBreakerController
@RestController
@Slf4j
public class CircleBreakerController{
public static final String SERVICE_URL = "http://nacos-payment-provider";
@Resource
private RestTemplate restTemplate;
@Resource
private PaymentService paymentService;
@GetMapping(value = "/consumer/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
return paymentService.paymentSQL(id);
}
}
Rule persistence
Once the application is restarted ,sentinel The rules will disappear , The production environment needs to persist the configuration rules .
Persistence of current limiting configuration rules into Nacos preservation , Just refresh 8401 Some rest Address ,sentinel You can see the flow control rules of the console , as long as nacos The configuration is not deleted , in the light of 8401 On sentinel The flow control rules on continue to take effect .
modify cloudalibaba-sentinel-service8401 modular .
pom.xml
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
application.yml
# Activate sentinel Yes feign Support for
spring:
cloud:
sentinel:
datasource:
dsl:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow

resource: Resource name
limitApp: Source application
grade: Threshold type ,0 Number of threads ,1 Express qps
count: Stand alone threshold
strategy: Flow control mode ,0 It means direct ,1 Express Association ,2 Represents a link
controlBehavior: Flow control effect ,0 Express a quick failure ,1 Express warm up,2 Stand in line
clusterMode: Cluster or not
start-up 8401, Refresh http://localhost:8401/rateLimit/byUrl
边栏推荐
- Tactile intelligent sharing-rk3568 application in scenic spot navigation robot
- zk-SNARK:关于私钥、环签名、ZKKSP
- Tf.constant usage
- 电商运营小白,如何快速入门学习数据分析?
- Hurry in!!! Write a number guessing game with dozens of lines of code based on the basic knowledge of C language
- [virtualization] view the log files of vCenter and esxi hosts
- PHP < => spacecraft operator (combined comparator)
- Sersync/lsync real-time synchronization
- 【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
- 开源许可证的传染性问题浅析
猜你喜欢

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

Matlab paper illustration drawing template issue 39 - stairs

Visio: how do Gantt charts merge cells? Solution: overwrite cells

电商运营小白,如何快速入门学习数据分析?

Dracoo master

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database

5年1.4W倍,NFT OG 的封神之路|Web3专栏

Introduction to UFS CLK gate

安装VMware报错failed to install the hcmon driver

通用测试用例写作规范
随机推荐
【Unity3d Shader】角色投影与倒影
Ali II: how to quickly query a table with tens of millions of data?
Introduction to UFS CLK gate
基于SSM选课信息管理系统
Matlab paper illustration drawing template issue 39 - stairs
How to use graffiti magic color product development kit
【读书笔记->数据分析】BDA教材《数据分析》书籍介绍
PHP连接mysql数据库,数据库连接静态工具类,简化连接。
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
安装VMware报错failed to install the hcmon driver
Opencv learning notes -- Hough transform
9-20v input peak charging current 3A dual lithium switch type charging chip sc7102
PHP <=> 太空船运算符(组合比较符)
【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
php 实现从1累加到100的算法
div设置高度不生效
leetcode: 102. 二叉树的层序遍历
Chinese database oceanbase was selected into the Forrester translational data platform report
电商运营小白,如何快速入门学习数据分析?
【程序员必备】七夕表白攻略:”月遇从云,花遇和风,晚上的夜空很美“。(附源码合集)