OpenFeign 实现熔断降级无效解决办法 日期:2025-05-13 分类:1 人气:0 若依微服务版本使用OpenFeign跨服务调用接口时面临的一个文字就是处理服务异常情况,包括404,502等情况,这时候需要用到熔断降级处理。 首先要明确你的框架使用的是哪一个熔断依赖,比如我使用的框架是ruoyi-plus,框架整合的熔断依赖是Sentinel,此时在OpenFeign模块添加的依赖就要使用spring-cloud-starter-alibaba-sentinel,相应的配置文件也要配置sentinel。 我参考了很多资料,无论是hystrix还是circuit-breaker都无法触发FeignClient的fallback降级,最终[这篇文章](https://blog.csdn.net/xcg340123/article/details/136092590)解决了我的问题。 整理一下ruoyi-plus实现OpenFeign的步骤,如果你使用hystrix或者circuitbreaker,请[查看这篇文章](https://blog.csdn.net/Anenan/article/details/147138754?spm=1001.2014.3001.5501) - 模块pom增加依赖 ``` <!--熔断降级使用--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> ``` - 定义降级类,此类实现FeignClient服务接口 ``` package com.longday.collection.factory; import org.springframework.stereotype.Component; import com.longday.collection.service.CollectionFeigenClient; /** * 藏品服务降级处理 * * @author ruoyi */ @Component public class CollectionFeigenClientFallback implements CollectionFeigenClient { @Override public String hello(String username) { return "服务不可用,请稍后再试。"; } } ``` - FeignClient注解填写callback参数,参数值为上一步定义的类 ``` package com.longday.collection.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import com.longday.collection.factory.CollectionFeigenClientFallback; /** * 藏品服务 * * @author ruoyi */ @FeignClient(contextId = "collectionFeigenClient", value = "ruoyi-sidecar", fallback = CollectionFeigenClientFallback.class) public interface CollectionFeigenClient { /** * test * * @param username 用户名 * @return 结果 */ @GetMapping(value = "/index/index/hello?name={username}") public String hello(@PathVariable("username") String username); } ``` - 模块的nacos配置文件增加配置 ``` feign: sentinel: enabled: true ``` ### 鸣谢 - [csdn**宣晨光**](https://blog.csdn.net/xcg340123/article/details/136092590) - [csdn**抓手**](https://blog.csdn.net/Anenan/article/details/147138754?spm=1001.2014.3001.5501) 标签: 上一篇:vscode使用Community Server Connector运行JDK1.8项目报错 下一篇:ruoyi-plus通过OpenFeign调用非jvm微服务接口 随便看看 2024-02-19 PHP7 运算符“??” 和“?:”的区别 2022-11-30 Linux 后台运行命令 2022-11-25 关于我们 2022-11-30 centos一键系统安装lnmp集成环境 2022-11-30 linux 生成 ssh 公钥 留言