fallbackfactory未生效

从你贴的完整日志看,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>

✅ 添加依赖后步骤

  1. 清除旧依赖mvn clean installclean 并重启 IDE)
  2. 确保 application.yml 中 feign.circuitbreaker.enabled=true
  3. 再次访问接口(让连接失败)
  4. 看控制台是否打印出你 fallbackFactory 中的日志
    System.err.println("Fallback触发了,错误是:" + cause);
    

✅ 一句话总结:

你写的 fallback 没问题,配置也对,就是 缺了 spring-cloud-starter-circuitbreaker-resilience4j 依赖,导致 fallbackFactory 永远不生效。


我可以根据你项目生成一个完整的依赖列表(pom.xml 部分)给你,需不需要?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值