public class SentinelExceptionHandler { public static Response handleBlock(Map<String, Object> params, BlockException e){ log.error("SentinelExceptionHandler HandleBlock",e); //在不是抛出异常的情况下(rt时间长),导致抛出的DegradeException被 限流的方法捕获 if(e instanceof DegradeException){ return Response.error(ResultCode.FALLBACK_ERROR,ExceptionUtils.getStackTrace(e)); }else{ return Response.error(ResultCode.LIMIT_ERROR,ExceptionUtils.getStackTrace(e)); } }
public static Response handleFallback(Map<String, Object> params,Throwable e){
log.error("SentinelExceptionHandler handleFallback",e);
return Response.error(ResultCode.FALLBACK_ERROR, ExceptionUtils.getStackTrace(e));
}
暂时这样解决的,不知道是不是bug,有谁遇到相同的问题吗?
spring-cloud-starter-alibaba-sentinel ,2.2.3-RELEASE sentinel-core, 1.8.0
@SentinelResource(value = "demo-list",blockHandlerClass= SentinelExceptionHandler.class,fallbackClass = SentinelExceptionHandler.class, blockHandler = "handleBlock",fallback = "handleFallback") public Response list(@RequestParam Map<String, Object> params){ log.info("demo/demo/list invoke ------------------------------"); PageResult pageResult = demoService.queryPage(params); return Response.success(pageResult); }
@Slf4j public class SentinelExceptionHandler { public static Response handleBlock(Map<String, Object> params, BlockException e){ log.error("SentinelExceptionHandler HandleBlock",e); //在不是抛出异常的情况下(rt时间长),抛出的DegradeException被 限流的方法捕获 //todo https://github.com/alibaba/Sentinel/issues/1801 if(e instanceof DegradeException){ return Response.error(ResultCode.FALLBACK_ERROR,ExceptionUtils.getStackTrace(e)); }else{ return Response.error(ResultCode.LIMIT_ERROR,ExceptionUtils.getStackTrace(e)); } }
public static Response handleFallback(Map<String, Object> params,Throwable e){
log.error("SentinelExceptionHandler handleFallback",e);
return Response.error(ResultCode.FALLBACK_ERROR, ExceptionUtils.getStackTrace(e));
}
}
原提问者GitHub用户zh69183787
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
blockHandler 用于专门处理被 Sentinel 流控、熔断的情况,而 fallback 主要针对业务异常。
原回答者GitHub用户sczyh30
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。