使用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);
}