sentinel熔断报java.lang.reflect.UndeclaredThrowableException

背景:内部要进行应用jdk&springboot升级,因此也需要将Spring Cloud Hystrix 替换成alibaba sentinel。

依赖

XML 复制代码
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
   <version>2022.0.0.0-RC2</version>
</dependency>

<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-datasource-nacos</artifactId>
  <version>1.8.6</version>
</dependency>

开启Feign对Sentinel的支持

sentinel官方文档

出现错误日志时的代码

java 复制代码
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author: WeiXiang
 * @Desc:
 * @Date: Create in 5:09 PM on 2020/6/23.
 */
@FeignClient(value = "messageCenter", path = "/messageCenter/cloudService")
public interface JobApplyRest {

    /**
     * 获取用户最近的报名兼职list
     *
     * @desc:
     * @author: WeiXiang
     * @date: 9:04 PM 2020/6/23
     */
    @PostMapping("/userLastMonthApplyPartJob")
    String userLastMonthApplyPartJob(@RequestParam("userId") Long userId, @RequestParam("size") Integer size);
}

当达到熔断条件时,则抛出以下错误信息

错误被解决后的代码

解决方案:在FeignClient上,配置自定义fallback熔断降级处理方法

java 复制代码
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @Author: WeiXiang
 * @Desc:
 * @Date: Create in 5:09 PM on 2020/6/23.
 */
@FeignClient(value = "messageCenter", path = "/messageCenter/cloudService", fallback = JobApplyRestFallback.class)
public interface JobApplyRest {

    /**
     * 获取用户最近的报名兼职list
     *
     * @desc:
     * @author: WeiXiang
     * @date: 9:04 PM 2020/6/23
     */
    @PostMapping("/userLastMonthApplyPartJob")
    String userLastMonthApplyPartJob(@RequestParam("userId") Long userId, @RequestParam("size") Integer size);
}

JobApplyRestFallback类

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @description: 报名单服务容错类
 * @author: jiusi
 * @create: 2023-09-01 10:23:47
 */
@Service
@Slf4j
public class JobApplyRestFallback implements JobApplyRest{
    @Override
    public String userLastMonthApplyPartJob(Long userId, Integer size) {
        log.warn("熔断降级开启JobApplyRest#userLastMonthApplyPartJob:userId:{}", userId);
        return null;
    }
}

至此,java.lang.reflect.UndeclaredThrowableException 错误被解决。

问题原因:

sentinel通过代理实现熔断降级,当达到设置的阈值条件时,内部就抛出的自定义受检异常 。但该受检异常并未在被代理对象接口定义中进行声明(即:Rest feignClient为被代理对象 ),那么这个异常就会被JVM包装成UndeclaredThrowableException进行抛出。

相关推荐
handsome_sai2 小时前
【Java 线程池】记录
java
大学生资源网3 小时前
基于springboot的唐史文化管理系统的设计与实现源码(java毕业设计源码+文档)
java·spring boot·课程设计
guslegend3 小时前
SpringSecurity源码剖析
java
roman_日积跬步-终至千里4 小时前
【人工智能导论】02-搜索-高级搜索策略探索篇:从约束满足到博弈搜索
java·前端·人工智能
大学生资源网4 小时前
java毕业设计之儿童福利院管理系统的设计与实现(源码+)
java·开发语言·spring boot·mysql·毕业设计·源码·课程设计
JasmineWr4 小时前
JVM栈空间的使用和优化
java·开发语言
Hello.Reader4 小时前
Flink SQL DELETE 语句批模式行级删除、连接器能力要求与实战避坑(含 Java 示例)
java·sql·flink
爱笑的眼睛114 小时前
从 Seq2Seq 到 Transformer++:深度解构与自构建现代机器翻译核心组件
java·人工智能·python·ai
Spring AI学习4 小时前
Spring AI深度解析(10/50):多模态应用开发实战
java·spring·ai
老华带你飞5 小时前
物流信息管理|基于springboot 物流信息管理系统(源码+数据库+文档)
数据库·vue.js·spring boot