When using Feign to call back the factory, sometimes an error will be reported. The key information of the error is as follows:
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
Console info:
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)
Reason one
Most of the time, it is caused by incorrect package guide:
Error package:
org.springframework.cloud.openfeign.FallbackFactory
change into:
feign.hystrix.FallbackFactory
Restarting the project works fine:
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 ''
Reason 2
The reason for this is that when using FallbackFactory to fuse the service, configure the fallbackFactory as fallback in the @FeignClient annotation, as shown below:
Change fallback to fallbackFactoryd, the code is as follows:
@FeignClient(value = "zhufeng-web-user", fallbackFactory = UserFallbackFactory.class) public interface ShowUserApi { /** * Get user information based on id * @param id * @return */ @GetMapping("/fegin/{id}") JSONObject showUserById(@PathVariable("id") int id); }