Sentinel's current limiting (QPS, number of threads, correlation, warm-up, waiting in line) hot key, parameter exceptions

Please see gitee for the entire project: https://gitee.com/xwb1056481167/spring-cloud

Sentinel installation and project integration: https://blog.csdn.net/www1056481167/article/details/113679945

Flow control rules

  • Resource name:  unique name, default request path
  • For the source:  Sentinel can limit the flow of the caller of the team and fill in the service name. The default is default (does not distinguish the source)
  • Threshold type/click threshold
    • QPS (the number of requests per second):  When the QPS calling the api reaches the threshold, limit the current
    • Number of threads:  when the number of threads calling the api reaches the threshold, limit the current
  • Whether to cluster:  no need to cluster
  • Flow control mode:
    • Direct connection: When the  api reaches the current-limiting condition, the current is directly limited
    • Association:  When the associated resource reaches the threshold, limit the flow of itself
    • Link:  Refers to recording the traffic on the specified link (the flow of the specified resource coming in from the ingress resource, if it reaches the threshold, if it reaches the threshold, it will limit the flow) [api level for the source]
  • Flow control effect:
    • Fast failure: Fail directly, throw an exception +  Warm Up:  According to the value of codeFactor (cold load factor, the default is 3), from the threshold/codeFactor, after the warm-up time, it hits the set QPS threshold.
    • Uniform queuing:  queuing at a uniform speed, allowing requests to pass at a uniform speed, the threshold type must be set to QPS, otherwise it is invalid.

Sentinel's current limit

Configuration (the following testA and testB are the interfaces of the project cloudalibaba-sentinel-service8401 )

1、QPS

When the number of requests per second exceeds the set threshold, limit the current

2. Number of threads

Principle: When the number of threads calling the api reaches the set threshold, the current limit is
similar to doing business in a bank. Only one customer is processing the same window at the same time, and other customers must wait for the customer to complete the processing before they can do it.

How to demonstrate:
1. Set the execution time of testB method to exceed 1 second, as follows

@GetMapping("/testB")
public String testB() {
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "-----------testB";
}

2. Set the request threshold for the number of threads

The method of calling the api/testB only supports one thread, but if the number of continuous access refresh threads exceeds one, an exception will be reported.

3. Association

When the associated resource reaches the threshold, it limits the current to itself
eg: When the resource B associated with A reaches the threshold, it limits the current to A itself

jmeter download address https://archive.apache.org/dist/jmeter/source/#sig

Visit: ApacheJMeter.jar tests the testB interface, and then the result of accessing the testB interface at this time

4. Warm up

According to the value of codeFactor (cold load factor, default is 3), from the threshold/codeFactor, after the warm-up time, it hits the set QPS threshold.
For example: the moment the spike system is turned on, there will be a lot of traffic coming up, which is likely to kill the system. The warm-up method is to protect the system, but slowly let the traffic in, and slowly increase the threshold to the set value. Set value

The default codeFactor is 3, that is, the QPS is requested to start from (threshold/3), and it will gradually rise to the set QPS threshold after how many warm-ups. Case: Threshold 10 + warm-up often set the threshold for 5 seconds system initialization to 10/ 3 is approximately equal to 3, that is, the threshold is 3 at the beginning, and the threshold slowly rises to 10 after 5 seconds.


How to test the results:
At the beginning, if you click testA madly, you will find an error Blocked by Sentinel (flow limiting). Then after 5 seconds, after the threshold is changed from 3->10, you will find that refreshing the browser will not limit the flow

5. Wait in line

To allow requests to pass at a uniform request rate, the threshold type must be QPS, otherwise it is invalid.
Setting meaning/testA 1 request per second, please wait in line if you time out, the waiting timeout time is 2000 milliseconds (ie 2 seconds)

The browser keeps refreshing the request testB (service is limited)

Hotspot key current limit

Hotspots are frequently accessed data. Many times we want to count the Top K data with the highest access frequency in a certain hotspot data and restrict access to it.

For example, the
product ID is a parameter, and the most frequently purchased product IDs in a period of time are counted and restricted. The
user ID is a parameter, and the user IDs that are frequently accessed in a period of time are restricted
. And according to the configured current-limiting threshold and mode, the resource calls that include hotspot parameters are current-limited. Hotspot parameter current limiting can be regarded as a special kind of flow control, which only takes effect for resource calls containing hotspot parameters.

1. Test class  @SentinelResource

@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                         @RequestParam(value = "p2", required = false) String p2) {
    return "------------------testHotKey";
}
public String deal_testHotKey(String p1, String p2, BlockException blockException) {
    //sentinel 系统默认的提示,Blocked by sentinel (flow limiting)
    return "------deal_testHotKey  ☹- _ -☹";
}

2. Configure the hot key interface  resource name must be the same as the value of @SentinelResource

Special attention is that if the hotspot current limit is set, the back-end controller method must have a blockHandler method, if not, it will directly return to the error page.

Principle analysis: The hotspot limit of the testHotKey method is set on the web page, and the corresponding method is also annotated with @SentinelResource, but no specific blockHandler method is specified, so when p1 is passed the parameter, and the number of refreshes on the browser side reaches one second If the clock is 2 times, the hotspot current limit for testHotKey is reached. At this time, the blockHandler specified by the hotspot current limit must be executed, but if it is not found, an error page error will be reported.

Parameter exception

The parameter subscript is the 0th parameter (it is known to be String when receiving in the background, and the corresponding data type needs to be selected). Under normal circumstances, the QPS threshold is 1, and the current limit is immediately exceeded, but if the parameter value of p1 is 5 (p1 =5), his QPS threshold is 200.
note:

@SentinelResource handles the violations of the Sentinel console configuration, which is handled by the blockHandler method configuration.
RuntimeException  int a=10/0, this handles the exception of java runtime, and it takes RuntimeException, so at this time @SentinelResource regardless of the
configuration error of @SentinelResource, the operation error is abnormal

Guess you like

Origin blog.csdn.net/www1056481167/article/details/113681761