• sentinel熔断报java.lang.reflect.UndeclaredThrowableException


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

    依赖

    1. <dependency>
    2. <groupId>com.alibaba.cloudgroupId>
    3. <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
    4. <version>2022.0.0.0-RC2version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.alibaba.cspgroupId>
    8. <artifactId>sentinel-datasource-nacosartifactId>
    9. <version>1.8.6version>
    10. dependency>

    开启Feign对Sentinel的支持

    sentinel官方文档

    出现错误日志时的代码

    1. import org.springframework.cloud.openfeign.FeignClient;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestParam;
    4. /**
    5. * @Author: WeiXiang
    6. * @Desc:
    7. * @Date: Create in 5:09 PM on 2020/6/23.
    8. */
    9. @FeignClient(value = "messageCenter", path = "/messageCenter/cloudService")
    10. public interface JobApplyRest {
    11. /**
    12. * 获取用户最近的报名兼职list
    13. *
    14. * @desc:
    15. * @author: WeiXiang
    16. * @date: 9:04 PM 2020/6/23
    17. */
    18. @PostMapping("/userLastMonthApplyPartJob")
    19. String userLastMonthApplyPartJob(@RequestParam("userId") Long userId, @RequestParam("size") Integer size);
    20. }

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

    错误被解决后的代码

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

    1. import org.springframework.cloud.openfeign.FeignClient;
    2. import org.springframework.web.bind.annotation.PostMapping;
    3. import org.springframework.web.bind.annotation.RequestParam;
    4. /**
    5. * @Author: WeiXiang
    6. * @Desc:
    7. * @Date: Create in 5:09 PM on 2020/6/23.
    8. */
    9. @FeignClient(value = "messageCenter", path = "/messageCenter/cloudService", fallback = JobApplyRestFallback.class)
    10. public interface JobApplyRest {
    11. /**
    12. * 获取用户最近的报名兼职list
    13. *
    14. * @desc:
    15. * @author: WeiXiang
    16. * @date: 9:04 PM 2020/6/23
    17. */
    18. @PostMapping("/userLastMonthApplyPartJob")
    19. String userLastMonthApplyPartJob(@RequestParam("userId") Long userId, @RequestParam("size") Integer size);
    20. }

    JobApplyRestFallback类

    1. import lombok.extern.slf4j.Slf4j;
    2. import org.springframework.stereotype.Service;
    3. /**
    4. * @description: 报名单服务容错类
    5. * @author: jiusi
    6. * @create: 2023-09-01 10:23:47
    7. */
    8. @Service
    9. @Slf4j
    10. public class JobApplyRestFallback implements JobApplyRest{
    11. @Override
    12. public String userLastMonthApplyPartJob(Long userId, Integer size) {
    13. log.warn("熔断降级开启JobApplyRest#userLastMonthApplyPartJob:userId:{}", userId);
    14. return null;
    15. }
    16. }

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

    问题原因:

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

  • 相关阅读:
    JS逆向实战27——pdd的anti_content 分析与逆向
    【Pytorch深度学习实战】(5)卷积神经网络(CNN)
    华清远见上海中心22071班
    Go 语言切片是如何扩容的?
    “过度炒作”的大模型巨亏,Copilot每月收10刀,倒赔20刀
    Tomcat安装shell脚本
    服务器从上架到上线经验
    Spring源码--Bean的加载
    LINUX笔记温习
    ssm体育课堂管理系统毕业设计源码181626
  • 原文地址:https://blog.csdn.net/YYQ_QYY/article/details/132624628