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的默认处理

相关推荐
不能再留遗憾了3 天前
【SpringCloud】Sentinel
spring·spring cloud·sentinel
Chan164 天前
流量安全优化:基于 Sentinel 实现网站流量控制和熔断
java·spring boot·安全·sentinel·intellij-idea·进程
@HNUSTer5 天前
基于 GEE 的 Sentinel-2 光谱、指数、纹理特征提取与 Sentinel-1 SAR 数据处理
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
@HNUSTer8 天前
基于 GEE 平台用 Sentinel-1 SAR 数据实现山区潜在滑坡检测
云计算·sentinel·数据集·遥感大数据·gee·云平台·sar
还是鼠鼠10 天前
《黑马商城》微服务保护-详细介绍【简单易懂注释版】
java·spring boot·spring·spring cloud·sentinel·maven
月夕·花晨13 天前
Gateway-过滤器
java·分布式·spring·spring cloud·微服务·gateway·sentinel
非凡ghost15 天前
Hard Disk Sentinel(固态硬盘监控) 多语便携版
windows·sentinel·软件需求
柳贯一(逆流河版)18 天前
Sentinel 深度解析:限流与熔断降级的微服务稳定性保障实践
微服务·架构·sentinel
耳东哇18 天前
sentinel docker gateway k8s 集群 主从
docker·gateway·sentinel
遥感之家19 天前
AWS下载sentinel-2原始影像
云计算·sentinel·aws