Sentine自定义限流异常

本文介绍了Sentinel在1.7及1.8版本中的异常处理方法,包括通过注解定义blockHandler和fallback,以及使用全局异常处理。Sentinel异常处理的优先级为:注解内定义的处理>UrlBlockHandler和BlockExceptionHandler>全局异常处理。提供了针对FlowException、DegradeException等不同异常类型的处理示例,确保服务稳定性和用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. sentinel 1.7版本(含)一下

2. sentinel 1.8 

3. 一共有3中方法

第一种:在springboot项目中,直接使用统一异常处理    

4. 参考链接


1. sentinel 1.7版本(含)一下

https://cloud.tencent.com/developer/article/1645215

 

2. sentinel 1.8 

https://blog.youkuaiyun.com/u011684553/article/details/108789968

 

3. 一共有3中方法


sentinel异常处理优先级 :@SentinelResource中定义了 blockHandler 和 faidback 属性  > UrlBlockHandler 和  BlockExceptionHandler  > 统一异常处理

第一种:在springboot项目中,直接使用统一异常处理    

/**
     * 替换默认流控提示内容:Blocked by sentinel(flow limiting)
     * 流控规则异常处理
     * 如果 @SentinelResource中定义了 blockHandler 和 faidback 属性,则此处不会捕获,以为 blockHandler 和 failback的优先级最高。
     * 限流处理、法一
     */
    @ExceptionHandler({FlowException.class})
    public R handleRRException(FlowException e){
        logger.error(RespConstant.RESP_MSG_FLOW_LIMITING_EXCEPTION, e);
        return RespUtils.commonHandle(RespConstant.RESP_CODE_FLOW_LIMITING_EXCEPTION, RespConstant.RESP_MSG_FLOW_LIMITING_EXCEPTION);
    }

    /**
     * 服务降级异常处理
     */
    @ExceptionHandler({DegradeException.class})
    public R handleRRException(DegradeException e){
        logger.error(RespConstant.RESP_MSG_DEGRADE_EXCEPTION, e);
        return RespUtils.commonHandle(RespConstant.RESP_CODE_DEGRADE_EXCEPTION, RespConstant.RESP_MSG_DEGRADE_EXCEPTION);
    }

    /**
     * 处理流量控制异常处理
     */
    @ExceptionHandler({AuthorityException.class})
    public R handleRRException(AuthorityException e){
        logger.error(RespConstant.RESP_MSG_AUTHORITY_EXCEPTION, e);
        return RespUtils.commonHandle(RespConstant.RESP_CODE_AUTHORITY_EXCEPTION, RespConstant.RESP_MSG_AUTHORITY_EXCEPTION);
    }

    /**
     * 处理流量控制异常处理
     */
    @ExceptionHandler({ParamFlowException.class})
    public R handleRRException(ParamFlowException e){
        logger.error(RespConstant.RESP_MSG_PARAM_FLOW_EXCEPTION, e);
        return RespUtils.commonHandle(RespConstant.RESP_CODE_PARAM_FLOW_EXCEPTION, RespConstant.RESP_MSG_PARAM_FLOW_EXCEPTION);
    }

    /**
     * 处理流量控制异常处理
     */
    @ExceptionHandler({SystemBlockException.class})
    public R handleRRException(SystemBlockException e){
        logger.error(RespConstant.RESP_MSG_SYSTEM_BLOCK_EXCEPTION, e);
        return RespUtils.commonHandle(RespConstant.RESP_CODE_SYSTEM_BLOCK_EXCEPTION, RespConstant.RESP_MSG_SYSTEM_BLOCK_EXCEPTION);
    }


    
    
第2种和第3种


        参考这里
    https://cloud.tencent.com/developer/article/1645215    

 

4. 参考链接

    https://cloud.tencent.com/developer/article/1645215    

https://blog.youkuaiyun.com/u011684553/article/details/108789968

https://cloud.tencent.com/developer/article/1645215

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值