sentinel限流授權異常資訊處理
阿新 • • 發佈:2022-05-17
實現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 + "}"); } }