"Spring Cloud Alibaba Microservice Architecture" topic (14)-Spring Cloud Alibaba Sentinel hot parameter current limit

1 Introduction

What is a hot spot? 热点即经常访问的数据. 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. such as:

  • Commodity ID is a parameter, count the most frequently purchased commodity IDs in a period of time and limit
  • User ID is a parameter, restricting user IDs that are frequently accessed within a period of time

The hotspot parameter current limit will count the hotspot parameters in the incoming parameters, and according to the configured current limit threshold and mode, the resource call that contains the hotspot parameters will be 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.
Insert picture description here
Sentinel uses LRUstrategies to count the most frequently accessed hotspot parameters, and combines the token bucket algorithm to perform parameter-level flow control. The hotspot parameter current limit supports the cluster mode.

2. Hotspot parameter rules

Hot parameter rules ( ParamFlowRule) are similar to traffic control rules ( FlowRule)

Attributes Description Defaults
resource Resource name, required
count Current limit threshold, required
grade Current limit mode QPS mode
durationInSec The length of the statistical window (in seconds), the mode is supported since version 1.6.0 1s
controlBehavior Flow control effect (support fast failure and uniform speed queuing mode), starting to support version 1.6.0 Fail fast
maxQueueingTimeMs Maximum waiting time in queuing (only valid in uniform speed queuing mode), supported by version 1.6.0 0ms
paramIdx Index hot parameters required, the corresponding SphU.entry(xxx, args)parameter in the index position
paramFlowItemList For parameter exceptions, the current limit threshold can be set separately for the specified parameter value, and is not restricted by the previous count threshold. Only basic types and string types are supported
clusterMode Whether it is a cluster parameter flow control rule false
clusterConfig Cluster flow control related configuration

3. Hotspot parameter current limiting case

We have already understood what is the hot parameter current limit, the following example shows how to configure and how Sentinel limits the current.

[A] Add the following method to the control layer

package com.bruce.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @BelongsProject: springcloud-alibaba-nacos
 * @BelongsPackage: com.bruce.controller
 * @CreateTime: 2021-02-20 17:19
 * @Description: TODO
 */
@RestController
@Slf4j
public class HotKeyRuleController {
    
    

    /**
     * @SentinelResource: 指定资源名称,作用与HystrixCommand大体一致
     * blockHandler: 指定降级处理方法,类似于Hystrix的fallbackMethod兜底方法
     */
    @GetMapping("/testHotKeyRule")
    @SentinelResource(value = "testHotKeyRule", blockHandler = "testHotKeyRule_blockHandler")
    public String testHotKeyRule(@RequestParam(value = "username", required = false) String username,
                                 @RequestParam(value = "password", required = false) String password) {
    
    
        return "[热点限流规则]testHotKeyRule..";
    }

    public String testHotKeyRule_blockHandler(@RequestParam(value = "username", required = false) String username,
                                              @RequestParam(value = "password", required = false) String password,
                                              BlockException exception) {
    
    
        return "[热点限流规则-兜底方法]testHotKeyRule_blockHandler..";
    }

}

Restart the project and visit the browser: http://localhost:8401/testHotKeyRule
Insert picture description here
shows that our interface is connected, let's see how to configure it.

[B] Sentinel hotspot current limiting configuration, the specific configuration is as shown in the figure
Insert picture description here
Insert picture description here
Insert picture description here
above . The above configuration indicates: when accessing testHotKeyRuleresources, testHotKeyRulethe request for the first parameter of the method [here corresponds to the username parameter in our controller], if the request is made within one second If the threshold QPS exceeds 1 time, then the bottoming method will be triggered.

Browser access: http://127.0.0.1:8401/testHotKeyRule?username=bruce
Insert picture description here
Note that at this time, we are accessing it once within one second. You can see that the interface returns data normally.
Suddenly, we increased the frequency of requesting the interface, and manually triggered the number of requests more than once within one second: It
Insert picture description here
can be seen that when the above configuration request rules are violated, the downgrading process will be immediately performed, and the blockHandler method we specified is successfully executed.

Below we visit: http://localhost:8401/testHotKeyRule?password=123456 , note that this request does not include the parameter [username] of our Sentinel configuration with a parameter index of 0, regardless of whether we request once a second or make crazy requests within a second Resource: It
Insert picture description here
can be seen that when the request interface does not contain the specified current limit key, the blockHandler method will not be triggered.

4. Hotspot current limit-parameter exception items

Earlier we introduced the normal version of the hot parameter current limit. When the first parameter is accessed more than once per second, it will be limited immediately after reaching the threshold 1. However, we sometimes expect our current limit parameter Key to be a certain value. When it is a special value, its current limit value is not the same as usual. Therefore Sentinel, the configuration of parameter exception items is provided to meet this special requirement.

For example: if our first parameter username=bruce, its threshold can reach 500.
Insert picture description here
[A] Sentinel parameter exception configuration
Insert picture description here
[b] test

Browser crazy visit: http://localhost:8401/testHotKeyRule?username=bruce123
Insert picture description here

It can be seen that when the value of the parameter does not meet the value of the exception item configuration, the normal current limit is still used by default [degrade directly after more than once per second].

Below we test the parameter exception: http://localhost:8401/testHotKeyRule?username=bruce

Because we configured earlier when the parameter value is equal to "bruce", the QPS threshold is 500, so when we keep refreshing in the browser, it is basically impossible to exceed the access threshold of 500 in one second, so it will not be triggered. Downgrade method. As shown in the figure below:
Insert picture description here
Let's talk about a place that needs special attention. We simulate an exception in the business method:

@GetMapping("/testHotKeyRule")
@SentinelResource(value = "testHotKeyRule", blockHandler = "testHotKeyRule_blockHandler")
 public String testHotKeyRule(@RequestParam(value = "username", required = false) String username,
                              @RequestParam(value = "password", required = false) String password) {
    
    
     int i = 10 / 0;
     return "[热点限流规则]testHotKeyRule..";
 }

Restart the project and visit the browser: http://localhost:8401/testHotKeyRule?username=bruce,
Insert picture description here
you can see that the error report with the divisor of 0 is displayed directly, and it will not be downgraded. Please don't do it here. Wrong, the blockHandler method is only for the current limiting rules you configure in Sentinel. When you violate it, the method will be used to degrade; if it is an exception thrown by the business, Sentinel will not be degraded.

Small summary:

@SentinelResource: Deal with the violations of the Sentinel console configuration, which is handled by the blockHandler method configuration;

RuntimeException: The runtime exception thrown by java runtime, @SentinelResource doesn't care, @SentinelResource manager configuration error, the operation should go abnormal or go abnormal.

5. Summary

The hotspot parameter current limiting rule is mainly based on the statistics of the request parameters, and realizes the current limiting. First, the hotspot parameters are based on QPS statistics. If the parameter index is set to 0, the first parameter statistics will prevail. The threshold is also controlled according to the threshold in the basic parameters, but the subscript of the additional parameter list is specified, then The value of the specified hotspot parameter needs to be provided. If the currently accessed parameter is inconsistent with the preset parameter, the threshold value of the first parameter still prevails.

Guess you like

Origin blog.csdn.net/BruceLiu_code/article/details/113898998