Sentinel downgrade strategy

Previous article: Sentinel flow control

Sentinel downgrade will limit the call of this resource when a resource in the call link is in an unstable state (such as call timeout or abnormal ratio increases), so that the request fails quickly and avoids affecting other resources and causing cascading. mistake. When a resource is downgraded, calls to the resource will automatically be disconnected within the next downgrade time window.

RT (average response time)

When >=5 requests continue to enter within 1 second and the average response time exceeds the threshold (in ms), then within the next time window (in s),
calls to this method will automatically be blown. (throws DegradeException). Note that
the default RT upper limit of Sentinel statistics is 4900 ms. Anything exceeding this threshold will be counted as 4900 ms. If you need to change this upper limit, you can configure it
through the startup configuration item .-Dcsp.sentinel.statistic.max.rt=xxx
Insert image description here![Insert image description here](https://img-blog.csdnimg.cn/03d2a1da3515477f895bb6e9685dfd8a.png

  • test
    Insert image description here
    Insert image description here
    Insert image description here

Abnormal ratio

When the resource's requests per second >= 5, and the ratio of the total number of exceptions per second to the throughput exceeds the threshold, the resource enters a degraded
state within the next time window (in s), this resource will Method calls will automatically return. The threshold range of anomaly ratio
is [0.0, 1.0], which represents 0% - 100%.
Insert image description here

  • test
    Insert image description here

Abnormal number

When the number of resource exceptions in the past minute exceeds the threshold, a circuit breaker will be initiated. Note that since the statistical time window is at the minute level, if
the timeWindow is less than 60s, it may still enter the fusing state after ending the fusing state.
Time window >= 60s
Insert image description here

Sentinel custom logic

Insert image description here

  • Code annotations are similar to the annotations
    @SentinelResourcein Hystrix. There are two attributes in the annotations that we need to distinguish. The blockHandler attribute is used to specify the downgrade fallback method that does not meet Sentinel rules. The fallback attribute is used to specify the Java runtime exception fallback method.@HystrixCommand
    @SentinelResource
// Sentinel的降级策略
blockHandlerClass = AutodeliverHandlersClass.class,blockHandler = "handlerException"
//java 代码异常的降级策略
fallbackClass = AutodeliverHandlersClass.class,fallback = "handlerError"
@GetMapping("/checkState/{userId}")
    @SentinelResource(value = "findResumeOpenState",blockHandlerClass = AutodeliverHandlersClass.class,blockHandler = "handlerException",fallbackClass = AutodeliverHandlersClass.class,fallback = "handlerError")
    public Integer findResumeOpenState(@PathVariable Long userId) {
    
    
            //模拟RT
        //        try {
    
    
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
    
    
//            e.printStackTrace();
//        }
//        模拟降级:异常比列
//        int i =1/0;
        Integer defaultResumeState = resumeFeignClient.findDefaultResumeState(userId);
        System.out.println("======>>>调⽤微服务,获取到⽤户" + userId + "的默认     简历当前状态为:" + defaultResumeState);
        return defaultResumeState;
    }
package com.w.edu.config;

import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
 * @author Mrwg
 * @date 2023/5/22 21:16
 * @description
 */
public class AutodeliverHandlersClass {
    
    
    public static  Integer handlerException(Long userId, BlockException blockException) {
    
    
        return  -100;
    }

    public static  Integer handlerError(Long userId) {
    
    
        return  -500;
    }
}

  • Flow control rules
    Insert image description here

Insert image description here

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/u014535922/article/details/130816226