开发者社区 > 云原生 > 正文

自定义限流处理逻辑实现不生效

public final class ExceptionUtil {

public static void handleException(BlockException ex) {
    System.out.println("Oops: " + ex.getClass().getCanonicalName());
}

} public class TestService {

// blockHandler 是位于 ExceptionUtil 类下的 handleException 静态方法,需符合对应的类型限制. @SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class}) public void test() { System.out.println("Test"); }

// blockHandler 是位于当前类下的 exceptionHandler 方法,需符合对应的类型限制.
@SentinelResource(value = "hello", blockHandler = "exceptionHandler")
public String hello(long s) {
    return String.format("Hello at %d", s);
}

public String exceptionHandler(long s, BlockException ex) {
    // Do some log here.
    ex.printStackTrace();
    return "Oops, error occurred at " + s;
}

}

示例项目中也并不生效,只是提示:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException] with root cause

com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

原提问者GitHub用户wzlee

展开
收起
白夜行fighting 2023-06-11 10:54:35 52 0
1 条回答
写回答
取消 提交回答
  • 我找到了解决方法,在你自定义的方法上面加上 static

    原回答者GitHub用户duanqingxu

    2023-06-11 11:37:34
    赞同 展开评论 打赏
问答地址:

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载