微服务整合Sentinel自定义降级异常数据开发实战

DBC 1.6K 0

异常种类


FlowException //限流异常
DegradeException //降级异常
ParamFlowException //参数限流异常
SystemBlockException //系统负载异常
AuthorityException //授权异常

【新版】实现BlockExceptionHandler并且重写handle方法
@Component
public class XdclassUrlBlockHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
    
    }
}
举个小例子
@Component
public class SentinelBlockHandler implements BlockExceptionHandler {


    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {

        JsonData jsonData = null;

        if(e instanceof FlowException){
            jsonData = JsonData.buildResult(BizCodeEnum.CONTROL_FLOW);

        }else if(e instanceof DegradeException){

            jsonData = JsonData.buildResult(BizCodeEnum.CONTROL_DEGRADE);

        } else if(e instanceof AuthorityException){

            jsonData = JsonData.buildResult(BizCodeEnum.CONTROL_AUTH);
        }

        httpServletResponse.setStatus(200);

        CommonUtil.sendJsonMessage(httpServletResponse,jsonData);

    }
}

发表评论 取消回复
表情 图片 链接 代码

分享