sentinel限流授权异常信息处理
实现BlockExceptionHandler接口,根据不同的子类型设置不同的异常msg
@Component public class SentinelExceptionHandler implements BlockExceptionHandler { @Override public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception { String msg = "未知异常"; int status = 429; if(e instanceof FlowException){ msg = "请求被限流了"; } else if(e instanceof ParamFlowException){ msg = "请求被热点参数限流了"; } else if(e instanceof DegradeException){ msg = "请求被降级了"; } else if(e instanceof AuthorityException){ msg = "没有权限访问"; status = 401; } httpServletResponse.setContentType("application/json;charset=utf-8"); httpServletResponse.setStatus(status); httpServletResponse.getWriter().println("{msg:" + msg + ",status:" + status + "}"); } }

浙公网安备 33010602011771号