Sentinel

Rules of the system

Source location: com. Alibaba. CSP. Sentinel. Slots. System. SystemRuleManager# checkSystem

Load

In Linux, you can run the uptime command to view the system load averages: The first parameter indicates the average system load within one minute, the second parameter indicates the average system load within five minutes, and the third parameter indicates the average system load within 15 minutes. These parameters are also called load1 load5 Load 15

  • This parameter is triggered when load1(1 minute load) exceeds the threshold and the number of concurrent threads exceeds the system capacity. You are advised to set this parameter to number of CPU cores x 2.5. Note: This parameter is valid only for Linux or UNIX-like machines. System capacity = maxQps x minRt, where maxQps indicates the maximum QPS calculated in seconds and minRt indicates the minimum response time calculated in seconds.

RT

The average RT of all incoming traffic reaches the threshold

The number of threads

The number of concurrent threads for all incoming traffic reached the threshold. Procedure

Entrance QPS

The QPS of all inbound traffic reaches the threshold

Use code for configuration

www.imooc.com/article/289…

Sentinel console configuration item

Sentinel API

Spring. Cloud. Sentinel. Filter. Enabled = false shut off for spring MVC endpoint protection, can be understood as to false, The scan interface URL is not displayed on the Sentinel console, but can be displayed on the console by means of the API

    @GetMapping("/test-sentinel")
    public String testSentinel(String a) {
        Entry entry = null;

        String resourceName = "test-sentinel-api";
        ContextUtil.enter(resourceName, "testService");
        try {
            entry = SphU.entry(resourceName);
            if (StringUtils.isBlank(a)) {
                throw newIllegalArgumentException(); }}catch (BlockException e) {
            return "Restricted or degraded.";
        } catch (IllegalArgumentException e) {
            Tracer.trace(e);
            return "Invalid parameter";
        } finally {
            if(entry ! =null) {
                entry.close();
            }
            ContextUtil.exit();
        }

        return "success";
    }
Copy the code

After calling the above interface, the console will display only this data

It should be noted that when the exception ratio is used and the number of exceptions is degraded, the exception referred to is by default BlockException. Other exceptions need to be specified and tracer.trace (e) needs to be called.

ContextUtil’s Enter and exit control the source of the flow control rule.

@ SentinelResource,

Properties, a www.imooc.com/article/289…

Using annotations versus using the Sentinel API above:

  • Api way to achieve, capture in additionBlockExceptionExternal exceptions need to be usedTracer.trace(), and the default way to use annotations is captureThrowable.Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};
  • Annotation mode is not supported ContextUtil.enter()This function
    @GetMapping("/test-sentinel-resource")
    @SentinelResource(value = "test-sentinel-resource", blockHandler = "blockHandler", fallback = "fallbackHandler")
    public String testSentinelResource(String param) {
        if (StringUtils.isBlank(param)) {
            throw new IllegalArgumentException("param is blank");
        }

        return "success";
    }

    public String blockHandler(String param) {
        log.error("Limit or degrade");
        return "Restricted or degraded.";
    }

    public String fallbackHandler(String param, Throwable ex) {
        log.error("Demoted: {}", ex.getMessage());
        return "Downgraded.";
    }
Copy the code

It is worth noting that after configuring the reversion rules

Fallback in the callback function abnormality for Java. Lang. IllegalArgumentException: param is blank

BlockHandler callback function com. Alibaba. CSP. Sentinel. Slots. Block. Degrade. DegradeException

RestTemplate integration Sentinel

Comments: @ SentinelRestTemplate

Integrated switch: resttemplate. Sentinel. Enabled by default is true

Relevant source com. Alibaba. Cloud. Sentinel. Custom. SentinelBeanPostProcessor

Feign integration Sentinel

The integrated switch: feign.sentinel.enabled Defaults to false

@FeignClient(name = "userCenter", path = "user", configuration = UserCenterFeignClientConfig.class, fallbackFactory = UserCenterFeignClientFallBackFactory.class)
Copy the code

The fallback Factory must bea valid spring bean.

@Component
public class UserCenterFeignClientFallBackFactory implements FallbackFactory<UserCenterFeignClient> {
    @Override
    public UserCenterFeignClient create(Throwable cause) {
        cause.printStackTrace();
        return new UserCenterFeignClient() {
            @Override
            public UserDto getByID(Integer id) {
                return null;
            }

            @Override
            public String paramTest(TestGetParam getParams, TestPostParam postParams) {
                return "Standby information"; }}; }}Copy the code

Note that the fallback here is similar to @SentinelResource’s Fallback, which behaves the same without specifying blockHandler. In comparison, @FeignClient lacks a separate class to handle limiting traffic. But can be judged by judging the Throwable FallbackFactory types, if for the com. Alibaba. CSP. The sentinel. Slots. Block. Flow. FlowException current limiting, The global exception handling mechanism of SpringBoot can be used to limit the flow of the unified information and demote it to the customized information

Sentinel extension Api

The source code for spring-Cloud-Alibaba version 2.2.5 is no longer in CommonFilter. In com. Alibaba. CSP. Sentinel. Adapter. Spring. Webmvc. AbstractSentinelInterceptor

Configuration items : www.imooc.com/article/289…