Microservice study notes--advanced articles--(Sentinel's current limiting rules)

As mentioned in the previous article, there are four solutions to solve the avalanche problem, and Sentinel mainly implements three of them, namely current limiting, that is, flow control, thread isolation, that is, bulkhead mode, and finally, downgraded fusing.

In this article, let's learn about throttling rules.

Table of contents:

Limiting rules

  • quick start
  • flow control mode
  • Flow control effect
  • Hotspot parameter current limit

cluster link

Cluster point link: It is the call link in the project, and each interface monitored in the link is a resource. By default, sentinel will monitor every endpoint (Endpoint) of S's usual MVC, so each endpoint (Endpoint) of SpringMVC is a resource in the call chain.


#### Quick Start

Click the flow control button behind resources/order/{orderId}, and a form pops up to add flow control rules in the donation form

资源名:/order/{orderId}
阀值类型:QPS   单机阈值:1

Its meaning is to limit the stand-alone OPS of the resource /order/{orderId} to 1, that is, only 1 request is allowed per second, and excess requests will be intercepted and an error will be reported.


Case: Getting Started with Flow Control Rules

Requirement: Set flow control rules for the resource /order/{orderId}, and the QPS cannot exceed 5. Then use jemeter to test.

1. Set flow control rules

资源名:/order/{orderId}
阀值类型:QPS   单机阈值:5

2. Jemeter test:

线程属性
线程数:20    #2秒内发送20个请求
Ramp-Up时间(秒):2
循环次数:1

flow control mode

When adding a flow-limiting rule, click Advanced Options to choose from three flow-control modes:

  • Direct: The request for the current resource at the same level, when the threshold is triggered, the current resource is directly limited, which is also the default mode
  • Association: Statistics of another resource related to the current resource, when the threshold is triggered, the current resource is limited #There are two resources, A and B, A triggers the threshold, but limits the flow of B
  • Link: Count the requests to access this resource from the specified link. When the threshold is triggered, limit the flow of #A, B, and C resources on the specified link. Both A and B must access resource C. When resource C is counted , only the requests sent from A are counted, and the requests from B are ignored
资源名:/order/{orderId}
阈值类型:OPS  单机阈值:2
流控模式:直接、关联、链路

Flow Control Mode-Association

  • Association mode: count another resource related to the current resource, and limit the flow of the current resource when the threshold is triggered
  • Usage scenario: For example, the user needs to modify the order status when paying, and the user wants to query the order at the same time. Query and modification operations compete for database locks, creating contention. The business requirement is limited payment and order update business, so when modifying the order business trigger threshold, it is necessary to limit the query order business flow.
资源名:/read
流控模式:关联
关联资源:/write

When the /write resource access triggers the threshold. It will limit the flow of /read resources to avoid affecting /write resources.


Case: flow control mode-association

need:

  • Create two endpoints in OrderControler: /order/query and /order/update, no need to implement business
  • Configure flow control rules, when the QPS accessed by the /order/update resource exceeds 5, limit the flow of /order/query requests
@RestController
@RequestMapping("order")
public class OrderController {
	@GetMapping("/query")
	public String queryOrder() {
		return "查询订单成功";
	}
		
	@GetMapping("/update")
	public String updateOrder() {
		return "更新订单成功";
	}
}
资源名:/query
流控模式:关联
关联资源:/update

summary:

The association mode can be used if the following conditions are met:

  • two competing resources
  • One with higher priority and one with lower priority

Case: flow control mode - link

Requirements: There are order query and order creation services, both of which need to query commodities. For the statistics of requests from querying orders to querying products, and setting a current limit.

Steps:
1. Add a queryGoods method in OrderService without implementing business
2. In OrderController, modify the /order/query endpoint and call queryGoods in OrderService
3. Add an /order/save endpoint in OrderController and call OrderService queryGoods method
4. Set a current limiting rule for queryGoods, and the QPS limit for entering queryGoods from /order/query must be less than 2

OrderService.java

public void queryGoods(){log.error("查询商品")}

OrderController.java

@RestController
@RequestMapping("order")
public class OrderController {
	@GetMapping("/query")
	public String queryOrder() {
	    //查询商品
	    orderService.queryGoods();
	    log.info("查询订单")
		return "查询订单成功";
	}
		
	@GetMapping("/save")
	public String saveOrder() {
	    //查询商品
	    orderService.queryGoods();
		return "新增订单成功";
	}
}

By default, Sentinel only marks the methods in the Controller as resources. If you want to mark other methods, you need to use the @SentinelResource annotation. Example:

@SentinelResouce("goods")
public void queryGoods() {
	log.error("查询商品");
}

Sentinel will integrate the Controller method into the context by default, resulting in the failure of the flow control in the link mode. You need to modify the application.xml and add the configuration:

spring:
  cloud:
    sentinel:
      web-context-unify: false #关闭context整合
资源名:goods
阀值类型:QPS  单机阈值:2
流控模式:链路
入口资源:/order/query

Such /order/query interface. If 4 requests are initiated per second, some of them cannot be sent.


Summarize:

What are the flow control modes?

  • Direct: limit current resources
  • Association: High-priority resources trigger thresholds, and low-priority resources are limited.
  • Link: When counting the threshold, only the requests entering the current resource from the specified resource are counted, which is the current limit on the source of the request

Flow control effect
Flow control effect refers to the measures that should be taken when the request reaches the flow control threshold, including three types:

  • Fail fast: Once the threshold is reached, new requests are rejected immediately and a FlowException is thrown. is the default processing method.
  • warm up: warm up mode, requests exceeding the threshold are also rejected and an exception is thrown. But this mode threshold will change dynamically, gradually increasing from a small value to a maximum threshold.
  • Waiting in queue: Let all requests be executed in a queue in order, and the interval between two requests cannot be less than the specified time
流控模式:链路
入口资源:/order/query
流控效果:快速失败  Warm Up  排队等待

Flow control effect - warm up

Warm up is also called warm-up mode, which is a solution to cold start of services. The initial value of the request threshold is threshold/coldFactor, and it will gradually increase to the threshold value after a specified duration. And the default value of coldFactor is 3

For example, if the threshold of QPS is set to 10 and the warm-up time is 5 seconds, then the initial threshold is 10/3, which is 3, and then gradually increases to 10 after 5 seconds

Case: flow control effect - warm up

Requirements: Set a current limit for the resource /order/{orderId}, the maximum QPS is 10, use the warm up effect, and the warm-up time is 5 seconds

资源名:/order/{orderId}
阀值类型:QPS  单机阈值:10
流控模式:直接
流控效果:Warm up
预热时长:5

Set the threshold of QPS to 10 and the warm-up time to 5 seconds, then the initial threshold is 10/3, which is 3, and then gradually increases to 10 after 5 seconds

If 20 requests are initiated in 2s, only 3 requests will pass at the beginning, and then gradually increase. In 5 seconds, 10 requests will pass


Flow control effect - waiting in line

Fail fast and warm up reject new requests and throw exceptions when requests exceed the QPS threshold. Queuing and waiting is to let all requests enter a queue, and then execute them sequentially according to the time interval allowed by the threshold. Subsequent requests must wait for previous executions to complete, and will be rejected if the expected waiting time exceeds the maximum duration.

For example: QPS=5 means that a request in the queue is processed every 200ms; timeout=2000 means that requests that are expected to wait for more than 2000ms will be rejected and an exception will be thrown


Case: flow control effect - waiting in line

Requirement: Set current limit for the resource /order/{orderId}, the maximum QPS is 10, use the flow control effect of queuing, and set the timeout period to 5s

资源名:/order/{orderId}
阀值类型:QPS  单机阈值:10
流控模式:直接
流控效果:排队等待
预热时长:5000

In the test, 300 requests are sent in 20 seconds, and the QPS is 15, which exceeds the 10 we configured, but we can see that our requests are all sent successfully.


Summarize:

What are the flow control effects?

  • Fail fast: reject new requests when the QPS exceeds the threshold
  • warm up: When the QPS exceeds the threshold, new requests are rejected; the QPS threshold is gradually prompted, which can avoid service downtime caused by high concurrency during cold start
  • Waiting in queue: the request will enter the queue, and the request will be executed sequentially according to the time interval allowed by the threshold; if the expected waiting time of the request is longer than the timeout time, it will be rejected directly

#### Hotspot parameter current limit

The previous current limiting is to count all the requests to access a certain resource and judge whether it exceeds the QPS threshold. The hotspot parameter current limit is to count requests with the same parameter value separately , and judge whether it exceeds the QPS threshold.

For example, /good/{id} has four requests id=1, id=1, id-1, id=2

parameter value SWC
id=1 3
id=2 1

Configuration example:

资源名:hot
限流模式:QPS模式
参数索引:0
单机阈值:5  统计窗口时长:1

The meaning of the representative is: to make statistics on parameter 0 (the first parameter) of the hot resource, and requests for the same parameter value per second cannot exceed 5

In the advanced options of hotspot parameter current limit, you can set exception configuration for some parameters:

参数类型:long
参数值        参数类型     限流阀值  
100             long            10
101             long             15

Combined with the previous configuration, the meaning here is to limit the current of the long type parameter of number 0, and the QPS of the same parameter cannot exceed 5 per second, with two exceptions:

  • If the parameter value is 100, the QPS allowed per 1 second is 10
  • If the parameter value is 101, the QPS allowed per 1 second is 15

Case: current limiting of hotspot parameters

Add a hotspot parameter current limit to the resource /order/{orderId}, the rules are as follows:

  • The default hotspot parameter rule is that the number of requests per second should not exceed 2
  • Set an exception for the parameter 102: the number of requests per second should not exceed 4
  • Set an exception for the parameter 103: the number of requests per second should not exceed 10

Note: hotspot parameter current limiting is not valid for default SpringMVC resources

OrderController.java

@RestController
@RequestMapping("order")
public class OrderController{
	@Autowired
	private OrderService orderService;

	@SentinelResource("hot")
	@GetMapping("{orderId}")
	public Order queryOrderByUserId(@PathVariable("orderId") Long orderId){
		// 根据id查询订单并返回
		return orderService.queryOrderById(orderId);
	}
}
资源名:hot
限流模式:QPS模式
参数索引:0
单机阈值:2   统计窗口时长:1秒
参数类型:long
参数值   参数类型   限流阈值
102         long          4
103         long         10

Guess you like

Origin blog.csdn.net/weixin_42594143/article/details/130966154