sentinel异常处理机制

1、自动识别出来的web接口

会走BlockExceptionHandler,可自定义,例如:

java 复制代码
@Component
public class SentinelBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s,
        BlockException e) throws Exception {
        tooFrequencyResponse(httpServletResponse);
    }

    public void tooFrequencyResponse(HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value()); // 429状态码

        CommonResult<?> result = CommonResult.failResult(8001, "服务器繁忙,请稍后重试");
        String jsonString = JSONUtil.toJsonStr(result);
        response.getWriter().write(jsonString);
    }
}

在项目中加入此类即可生效

2、@SentinelResource标记的资源

会依次去找@SentinelResource注解里面配置的blockHandler,fallback,defaultFallback,根据先后顺序找到即对应处理,如果都未配置,则返回Springboot的默认处理

统一处理:

java 复制代码
@ExceptionHandler({UndeclaredThrowableException.class})
public CommonResult<?> flowExceptionHandler(UndeclaredThrowableException e) {
    log.error("UndeclaredThrowableException: ", e);
    return CommonResult.failResult(CommonErrorInfo.code_8001,"服务器繁忙,请稍后重试");
}

3、openfeign调用的资源

会根据设置的fallback来返回异常,如果未设置则返回Springboot的默认处理

相关推荐
Owen__z5 天前
GEE统计特定区域特定时间上的Landsat/Sentinel的影像信息
python·sentinel·gee·geemap·landsat
我要去腾讯5 天前
Springcloud核心组件之Sentinel详解
java·spring cloud·sentinel
哦你看看10 天前
Redis Sentinel哨兵集群
linux·redis·bootstrap·sentinel
2501_9387900711 天前
Spring Cloud Alibaba 2023 版:Nacos 服务发现与 Sentinel 限流的整合方案
sentinel·服务发现
青鱼入云11 天前
Sentinel介绍
微服务·sentinel
青鱼入云11 天前
Feign如何集成Sentinel
spring cloud·微服务·sentinel
一周困⁸天.13 天前
Redis Sentinel哨兵集群
redis·bootstrap·sentinel
一条懒鱼66614 天前
Redis Sentinel哨兵集群
数据库·redis·sentinel
2301_7976042414 天前
d44:Sentinel 微服务流量控制与 Seata 分布式事务
分布式·微服务·sentinel
新手小白*14 天前
Redis Sentinel哨兵集群
数据库·redis·sentinel