Sentinel system rule setting

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

The project in this article is:  cloudalibaba-sentinel-service8401

System rules

System protection rules are controlled from application-level ingress traffic, monitoring application indicators from several dimensions such as load, CPU usage, average RT, ingress QPS, and number of concurrent threads of a single machine, so that the system can run at the maximum throughput as much as possible While ensuring the stability of the system as a whole.
System protection rules are applied to the overall dimension, not the resource dimension, and only take effect on ingress traffic. Ingress traffic refers to the traffic entering the application (EntryType.IN). For example, requests received by the Web service or Dubbo server are all ingress traffic.
The system rules support the following modes:
Load adaptive (only valid for Linux/Unix-like machines):  The load1 of the system is used as a heuristic indicator for adaptive system protection. When the system load1 exceeds the set heuristic value, and the current number of concurrent threads of the system exceeds the estimated system capacity, the system protection (BBR phase) will be triggered. The system capacity is estimated by the system's maxQps * minRt. The setting reference value is generally CPU cores * 2.5.
CPU usage (version 1.5.0+):  When the system CPU usage exceeds the threshold, the system protection is triggered (value range 0.0-1.0), which is more sensitive.
Average RT:  When the average RT of all ingress traffic on a single machine reaches the threshold, system protection is triggered, in milliseconds.
Number of concurrent threads:  When the number of concurrent threads for all ingress traffic on a single machine reaches the threshold, system protection is triggered.
Ingress QPS:  When the QPS of all ingress traffic on a single machine reaches the threshold, the system protection is triggered.

The QPS threshold is set to 2. No matter which interface is accessed, if the number of accesses reaches 2 times within 1 second, the flow will be restricted.

Limit flow by resource name

Project address cloudalibaba-sentinel-service8401

@RestController
public class RateLimitController {
    @GetMapping("/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl() {
        return new CommonResult(200, "按url限流测试ok", new Payment(2020L, "serial002"));
    }

    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerHandler.class, blockHandler = "handlerException2")
    public CommonResult customerBlockHandler() {
        return new CommonResult(200, "按客户自定义限流测试ok", new Payment(2020L, "serial002"));
    }
}

The above explains the current limit according to the service name, the following is the current limit for the interface

There will be code redundancy in the above, and customer-defined processing current-limiting logic is required

User-defined service current limit

1. Provide a unified exception handling class

package org.xwb.springcloud.myhandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.xwb.springcloud.entities.CommonResult;
public class CustomerHandler {
    public static CommonResult handlerException1(BlockException blockException) {
        return new CommonResult(4444, "按照客户自定义, global handlerException-------------1");
    }
    public static CommonResult handlerException2(BlockException blockException) {
        return new CommonResult(4444, "按照客户自定义, global handlerException-------------2");
    }
}

2. @SentinelResource specifies the class and method of current limiting in the called service name

@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler", blockHandlerClass = CustomerHandler.class, blockHandler = "handlerException2")
public CommonResult customerBlockHandler() {
    return new CommonResult(200, "按客户自定义限流测试ok", new Payment(2020L, "serial002"));
}

Note:  blockHandlerClass specifies the class class, and blockHandler specifies the specific method in the class
3. Configure the current limiting service name

test

访问http://localhost:8401/rateLimit/customerBlockHandler

Guess you like

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