No fallback instance of type class found for feign client user-service(轉)


1、錯誤日志

在 feign 開啟熔斷,配置 fallback 類,實現當前接口的實現類時,報錯信息如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
ERROR 7204 --- [ main] o.s.boot.SpringApplication  : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: 
  Error creating bean with name
'consumerController':
Unsatisfied dependency expressed through field 'mUserClient';
nested exception is org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'club.sscai.consumer.client.UserClient':
FactoryBean threw exception on object creation;
nested exception is java.lang.IllegalStateException:
  No fallback instance of type class club.sscai.consumer.client.UserClientImpl found for feign client user-service

 

2、通常配置

1、開啟 hystrix(默認是關閉的):feign.hystrix.enabled=true

2、Fallback 接口實現類需要注解 @Component

如果到此處還沒有解決的話?請往下看。

 

3、轉載解決方案

跟蹤代碼發現 是因為對FeignClient 這個接口做了AOP切面。

@Pointcut("execution(* com.xx.xx.service.IR*.*(..))")
public void remoteCall() {
}

Trace日志看到這么一行:

[DEBUG] [17:50:22.410][JdkDynamicAopProxy][117]:
Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.xx.xx.service.HystrixClientFallback@32354b00]

然后考慮是不是因為Spring AOP動態代理默認為 JDK動態代理。

切面還是要切的,Fallback也不能放棄。因為調用的是接口,無論如何都要被切。

換成cglib后,問題成功解決。


 

原理
SpringAOP 的動態代理有兩種實現,JDK動態代理,和Cglib。
Spring默認使用 JDK動態代理。
當類至少實現了一個接口時,使用JDK動態代理。上文的Feign的Fallback類正好是這樣。
至於究竟為什么cglib可以成功,就不去深究了,方案就兩個,非此即彼。
至於為什么 找不到 fallback instance?

private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
    List<String> result = new ArrayList<String>();

    // Check all bean definitions.
    for (String beanName : this.beanDefinitionNames) {
        // Only consider bean as eligible if the bean name
        // is not defined as alias for some other bean.
        if (!isAlias(beanName)) {
            try {
                RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                // Only check bean definition if it is complete.
                if (!mbd.isAbstract() && (allowEagerInit ||
                   ((mbd.hasBeanClass() || !mbd.isLazyInit() || isAllowEagerClassLoading())) &&
                   !requiresEagerInitForType(mbd.getFactoryBeanName()))) {
                    // In case of FactoryBean, match object created by FactoryBean.
                    boolean isFactoryBean = isFactoryBean(beanName, mbd);
                    boolean matchFound = (allowEagerInit || !isFactoryBean || containsSingleton(beanName)) &&
                    (includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type);

......

問題出現在isTypeMatch

這里isTypeMatch 返回了false,因為騙不過JVM類型檢查。當使用cglib則是匹配的。

 

原文地址:https://www.jianshu.com/p/c8210d878e96

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM