解决:Sentinel设置blockHandler不生效,只有fallback生效

解决:Sentinel设置blockHandler不生效,只有fallback生效

一·个人经验总结:

在这里插入图片描述

二·官方文档:

在这里插入图片描述

三·@SentinelResource注解:用法示例

public class TestService {
    
    

    // 对应的 `handleException` 函数需要位于 `ExceptionUtil` 类中,并且必须为 static 函数.
    @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {
    
    ExceptionUtil.class})
    public void test() {
    
    
        System.out.println("Test");
    }

    // 原函数
    @SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
    public String hello(long s) {
    
    
        return String.format("Hello at %d", s);
    }
    
    // Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String helloFallback(long s) {
    
    
        return String.format("Halooooo %d", s);
    }

    // Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
    public String exceptionHandler(long s, BlockException ex) {
    
    
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at " + s;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_48033662/article/details/140741779