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进行抛出。

相关推荐
毕设源码-赖学姐2 小时前
【开题答辩全过程】以 基于Android的校园快递互助APP为例,包含答辩的问题和答案
java·eclipse
damo012 小时前
stripe 支付对接
java·stripe
麦麦鸡腿堡3 小时前
Java的单例设计模式-饿汉式
java·开发语言·设计模式
假客套3 小时前
Request method ‘POST‘ not supported,问题分析和解决
java
傻童:CPU3 小时前
C语言需要掌握的基础知识点之前缀和
java·c语言·算法
爱吃山竹的大肚肚3 小时前
@Valid校验 -(Spring 默认不支持直接校验 List<@Valid Entity>,需用包装类或手动校验。)
java·开发语言
雨夜之寂3 小时前
mcp java实战 第一章-第一节-MCP协议简介.md
java·后端
摇滚侠4 小时前
Spring Boot 3零基础教程,WEB 开发 Thymeleaf 核心语法 笔记39
spring boot·笔记·后端·thymeleaf
皮皮林5514 小时前
蚂蚁又开源了一个顶级 Java 项目!
java
吹晚风吧4 小时前
spring是如何解决循环依赖的(二级缓存不行吗)?
java·spring·循环依赖·三级缓存