com.alibaba.csp.sentinel.slots.block.flow.FlowException: null--记录一次报错

阿丹:

记录一次开发中遇到的错误。

报错信息描述:

复制代码
2023-10-01 09:47:57.263 ERROR 39572 --- [nio-9100-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException] with root cause

com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

问题出现场景:

因为我是开发后端嘛,使用Sentinel的注解来给我的接口做了一下限流,限流的效果是正常的,但是发现我的回调函数并不能正常被触发。

问题出现原因:

根据错误信息来看,这是一个com.alibaba.csp.sentinel.slots.block.flow.FlowException异常。该异常通常由流控规则引起,表示请求被阻塞或限流了。

要解决这个问题,你可以考虑以下几个方面:

  1. 检查流控规则:检查Sentinel的流控规则是否正确配置并能够满足你的需求。确保规则没有过于严格的限制导致请求被拒绝。

  2. 检查资源使用情况:查看应用程序的资源使用情况,特别是在流控发生时。如果资源使用过高,可以考虑优化代码或增加资源容量来避免流控。

  3. 检查Sentinel配置:确保Sentinel的相关配置正确无误。例如,检查Sentinel的配置文件是否正确加载,规则是否生效等。

解决方式:

1、回调函数的参数要与被限流的参数保持一致,并且接口的后面添加一个参数。

2、要保证限流的回调函数的返回值与被限流的接口保持一致

正确的代码

java 复制代码
@SentinelResource(value = "findByUsername" ,blockHandler = "blockHandlerForGetUser")
    @GetMapping("/findByUsername/{username}")
    public User findByUsername(@PathVariable String username){
        return service.findByUsername(username);
    }

    public User blockHandlerForGetUser(@PathVariable String username, BlockException blockException){
        User user = new User();
        user.setUsername("限流统一返回类");
        return user;
    }
相关推荐
廋到被风吹走5 天前
稳定性保障:限流降级深度解析 —— Sentinel滑动窗口算法与令牌桶实现
运维·算法·sentinel
笨蛋不要掉眼泪9 天前
Sentinel 热点参数限流实战:精准控制秒杀接口的流量洪峰
java·前端·分布式·spring·sentinel
笨蛋不要掉眼泪10 天前
Sentinel 流控规则详解:三种模式与三种效果实战指南
java·jvm·数据库·后端·sentinel
Jinkxs11 天前
Sentinel - 在 Dubbo 微服务中使用:Alibaba 生态无缝集成
微服务·sentinel·dubbo
笨蛋不要掉眼泪11 天前
Spring Cloud Alibaba Sentinel 从入门到实战:微服务稳定性的守护者
分布式·微服务·云原生·架构·sentinel
tod11311 天前
Redis Sentinel 高可用架构:从原理到 Docker 部署全解析
数据库·redis·docker·架构·sentinel
递归尽头是星辰13 天前
Sentinel + Spring Cloud Gateway 联动限流实战
系统架构·sentinel·限流·微服务治理·限流架构设计
万象.13 天前
redis哨兵sentinel的部署及作用
redis·sentinel
七夜zippoe14 天前
分布式系统弹性设计实战:Hystrix与Sentinel熔断降级深度解析
java·hystrix·sentinel·aws·分布式系统
程序员敲代码吗15 天前
微服务熔断降级配置详解与实践:使用Sentinel和Nacos
java·微服务·sentinel