Sentinel resource protection rules

(1 Introduction

Sentinel supports a variety of protection rules: flow control rules, circuit breaker downgrade rules, system protection rules, source access control rules, and hot parameter rules.
The flow restriction rules first define the flow restriction rules through FlowRule, and then load the rule list through FlowRuleManager.loadRules. The complete current limiting rule setting code is as follows:

 //定义规则列表
        List<FlowRule> rules = new ArrayList<>();
        //构建一个规则
        FlowRule rule = new FlowRule();
        rule.setCount(20);
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setLimitApp("default");
        rule.setStrategy(RuleConstant.STRATEGY_CHAIN);
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        rule.setClusterMode(false);
        rules.add(rule);
        //加载到规则管理器中
        FlowRuleManager.loadRules(rules);

(2) Resource protection rules

Insert picture description here

count: current limit threshold.

Grade: Current limit threshold type, QPS mode (1) or concurrent thread number mode (0).

resource: Set the resource to be protected. The name of this resource must be consistent with the name used in SphU.entry

limitApp: Whether it is necessary to limit the current of the call source, the default is default, that is, the call source is not distinguished.

strategy: Calling relationship current limiting strategy-direct, link, and association.

controlBehavior: Flow control behavior, including direct rejection, waiting in line, and slow start mode. The default is direct rejection.

clusterMode: Whether it is cluster current limiting, the default is No.

(3) Grade: current limiting threshold type

There are two types of Sentinel flow control statistics, which are controlled by the grade attribute;
1. The number of concurrent threads (FLOW_GRADE_THREAD)
2. QPS (FLOW_GRADE_QPS) The
current limit of the number of concurrent threads in Sentinel is to count the number of context threads currently requested. If the threshold is exceeded, a new one The request will be rejected.
QPS (Queries Per Second) represents the number of queries per second, that is, the number of queries a server can respond to per second. When the QPS reaches the current-limiting threshold, the current-limiting strategy is triggered.

(4) controlBehavior: flow control behavior

When QPS exceeds the threshold, it will trigger the flow control behavior. This behavior is set by controlBehavior. It includes:
1. Direct rejection (RuleConstant.CONTROL_BEHAVIOR_DEFAULT)
2. Warm Up (RuleConstant.CONTROL_BEHAVIOR_WARM_UP), cold start (warm up) )
3. Uniform queuing (RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
4. Cold start + uniform queuing (RuleConstant.CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER)

Direct rejection
Direct rejection is the default flow control method, that is, when the request flow exceeds the threshold, a FlowException is directly thrown.

Warm Up
Warm Up is a cold start (warm up) method. When the traffic increases suddenly, it means that the system suddenly switches from the idle state to the busy state, which may overwhelm the system in an instant. Warm Up can achieve this goal when we hope that the number of request processing will gradually increase and reach the maximum allowed processing request after an expected time.
For example, the maximum number of concurrency that the current system can handle is 480. First, the system has been in an idle state, and then the amount of requests suddenly rises linearly. At this time, the system does not directly pull the QPS to the maximum value, but gradually increases the threshold within a certain period of time, and the intermediate period is a process of gradual warm-up of the system.

Uniform queuing The
uniform queuing method will strictly control the interval time between requests, that is, let the requests pass at a uniform speed, which is actually equivalent to the leaky bucket current limiting algorithm of the current limiting algorithm.

(5) Invoking relationship traffic strategy

The calling relationship includes the caller and the callee. One method may call other methods to form a call chain. The so-called call relationship flow strategy is to trigger flow control based on different call dimensions.
1. Limit current according to the caller.
2. The current limit is based on the invocation link entry.
3. Relational resource flow control (associated flow control).

[Caller current limit] The
so-called caller current limit is to control the flow according to the source of the request. We need to set the limitApp property to set the source information. It has three options.
1. Default: means that the caller is not distinguished, that is, any request to access the caller will be subject to current limit statistics.
2. (some_origin_name}: Set a specific caller, and only requests from this caller will perform traffic statistics and control.
3. other: Means to perform traffic control for callers other than {some_origin_name}.

Since multiple rules can be configured for the same resource, if the limitApp settings of multiple rules are not the same, the order of effect of the rules is: {some_origin_name}→other→default.

[According to the invocation link ingress current limit]
A method of current limit protection may come from different invocation links. For example, for resource nodeA, both entry Entrance1 and entry Entrance2 call resource nodeA, then Sentinel allows traffic statistics to be performed only based on a certain entry. For example, we set the call to Entrance1 for the nodeA resource to count the number of requests. It is somewhat similar to caller current limiting to a certain extent.
Insert picture description here

[Associated flow control]
When there is a dependency or resource contention between two resources, we say that the two resources are related. The execution of these two dependent resources may affect the execution efficiency of the other resource because the execution of one resource is too frequent. Therefore, the associated flow control (flow control) is to limit the execution flow of one of the resources.

Guess you like

Origin blog.csdn.net/Octopus21/article/details/114902083