从你贴的完整日志看,Feign 报了 RetryableException: Connection refused
错误,但并没有触发你配置的 fallbackFactory
。
你已经:
- ✅ 使用了
@FeignClient(... fallbackFactory = DemoFallbackFactory.class)
- ✅ 正确实现了
FallbackFactory
- ✅
application.yml
中启用了feign.circuitbreaker.enabled=true
- ✅ 引入了
resilience4j-spring-boot2
但它依然没有进入 fallbackFactory,所以我们可以合理怀疑:
❗ 当前项目没有启用 Resilience4j 的 Feign 支持自动装配
也就是说,即使你引入了依赖,Resilience4j 没接管 Feign 调用,fallbackFactory 就不会生效。
✅ 解决方法(关键一步)
请手动引入如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
🔍 注意:你刚才提到的这个:
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
</dependency>
它是基础依赖,而不是专门为 Spring Cloud OpenFeign 设计的。
你真正需要的是
spring-cloud-starter-circuitbreaker-resilience4j
,它才能让 Feign + Resilience4j + fallbackFactory 联动起来!
📦 完整 Feign+Fallback 正确依赖示例
<dependencies>
<!-- Feign核心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Resilience4j支持Feign熔断 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
<!-- 可选:为了日志更友好 -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
</dependencies>
✅ 添加依赖后步骤
- 清除旧依赖(
mvn clean install
或clean
并重启 IDE) - 确保 application.yml 中
feign.circuitbreaker.enabled=true
- 再次访问接口(让连接失败)
- 看控制台是否打印出你 fallbackFactory 中的日志
System.err.println("Fallback触发了,错误是:" + cause);
✅ 一句话总结:
你写的 fallback 没问题,配置也对,就是 缺了
spring-cloud-starter-circuitbreaker-resilience4j
依赖,导致 fallbackFactory 永远不生效。
我可以根据你项目生成一个完整的依赖列表(pom.xml
部分)给你,需不需要?