Feign 回调工厂报错:Incompatible fallbackFactory instance. Fallback/fallbackFactory

使用Feign回调工厂时,有时会报错,报错关键信息如下:

Caused by: java.lang.IllegalStateException: Incompatible fallbackFactory instance. Fallback/fallbackFactory of type class com.zhufeng.web.fallback.UserFallbackFactory is not assignable to interface feign.hystrix.FallbackFactory for feign client zhufeng-web

控制台信息:

Caused by: java.lang.IllegalStateException: Incompatible fallbackFactory instance. Fallback/fallbackFactory of type class com.zhufeng.web.fallback.UserFallbackFactory is not assignable to interface feign.hystrix.FallbackFactory for feign client zhufeng-web-user
	at org.springframework.cloud.openfeign.HystrixTargeter.getFromContext(HystrixTargeter.java:88)
	at org.springframework.cloud.openfeign.HystrixTargeter.targetWithFallbackFactory(HystrixTargeter.java:63)
	at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:53)
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:352)

原因一

多数时候,是因为导包不正确引起:

错误包:

org.springframework.cloud.openfeign.FallbackFactory

修改为:

feign.hystrix.FallbackFactory

重启项目正常:

2022-09-02 13:43:19.146  INFO 19267 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 382 ms
2022-09-02 13:43:19.212  INFO 19267 --- [           main] o.s.c.openfeign.FeignClientFactoryBean   : For 'zhufeng-web-user' URL not provided. Will try picking an instance via load-balancing.
2022-09-02 13:43:19.340  WARN 19267 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-09-02 13:43:19.340  INFO 19267 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-09-02 13:43:19.341  WARN 19267 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2022-09-02 13:43:19.341  INFO 19267 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2022-09-02 13:43:19.394  INFO 19267 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-09-02 13:43:19.445  INFO 19267 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'Nacos-Watch-Task-Scheduler'
2022-09-02 13:43:19.461  WARN 19267 --- [           main] ockingLoadBalancerClientRibbonWarnLogger : You already have RibbonLoadBalancerClient on your classpath. It will be used by default. As Spring Cloud Ribbon is in maintenance mode. We recommend switching to BlockingLoadBalancerClient instead. In order to use it, set the value of `spring.cloud.loadbalancer.ribbon.enabled` to `false` or remove spring-cloud-starter-netflix-ribbon from your project.
2022-09-02 13:43:19.497  INFO 19267 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2022-09-02 13:43:19.497  INFO 19267 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Environment :null
2022-09-02 13:43:19.497  INFO 19267 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2022-09-02 13:43:19.538  INFO 19267 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Starting ProtocolHandler ["http-nio-8081"]
2022-09-02 13:43:19.546  INFO 19267 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''

原因二

出现的原因是使用FallbackFactory 来进行服务熔断时,在@FeignClient注解中配置将fallbackFactory配置为fallback,如下图:

将fallback改为fallbackFactoryd,代码如下:

@FeignClient(value = "zhufeng-web-user", fallbackFactory = UserFallbackFactory.class)
public interface ShowUserApi {

    /**
     * 根据id获取user信息
     * @param id
     * @return
     */
    @GetMapping("/fegin/{id}")
    JSONObject showUserById(@PathVariable("id") int id);

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月夜烛峰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值