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

摘要:
@Pointcutpublicvoid remoteCall(){}跟踪日志显示以下行:[DEBUG][17:50:22410][JdkDynamicAopProxy][117]:创建JDKdynamicProxy:targetsourceisSingletonTargetSourcefortargetobject[com.xx.xx.service.HystrixClientFallback@32354b00]然后考虑Spring AOP动态代理是否默认为JDK动态代理。切割平面仍需切割,回退无法放弃。更改为cglib后,问题成功解决。默认情况下,Spring使用JDK动态代理。上面的费恩撤退班就是这样。至于cglib能够成功的原因,我们不必赘述。有两种解决方案,一种或另一种。至于为什么我们找不到fallback实例?isFactoryBean | | containsSingleton)&&&&isTypeMatch;。。。。。。问题出现在isTypeMatch中,其中isTypeMatch返回false,因为JVM类型检查无法被欺骗。当使用cglib时,它是匹配的。

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

No fallback instance of type class found for feign client user-service(转)第1张

这里isTypeMatch 返回了false,因为骗不过JVM类型检查。当使用cglib则是匹配的。

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

免责声明:文章转载自《No fallback instance of type class found for feign client user-service(转)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇nginx基于TCP的反向代理贷款减值准备和折现回拨下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Springboot2.x 启动报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Proxy82'

Springboot 2.0.5 搭建一个新项目启动后报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Proxy82' C:Javajdk1.8.0_144injava.exe -XX:TieredStopAtLevel=1 -noverify -Dspring....

【转】java异常报错大全

算术异常类:ArithmeticExecption 空指针异常类:NullPointerException 类型强制转换异常:ClassCastException 数组负下标异常:NegativeArrayException 数组下标越界异常:ArrayIndexOutOfBoundsException 违背安全原则异常:SecturityExceptio...

[JavaWeb]SpringSecurity-OAuth2.0 统一认证、资源分离的配置,用于分布式架构、模块化开发的认证体系

前言 关于 OAuth2.0的认证体系,翻阅了好多资料,RCF 文档太多,看了一半就看不下去了,毕竟全英文的文档看起来,是有一点让我烦躁,但也对 OAuth2.0的认证流程有了一个基本的概念,之前用 SpringSecurity 做了一个基于 RBAC 的权限管理系统的基础配置,所以对 SpringSecurity 算是比较了解了,于是 OAuth2.0...

获取Spring容器中Bean实例的工具类(Java泛型方法实现)

在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean。 因此,实现一个获得bean实例的工具类,就很有必要。 以前,写了一个根据bean的名称和类型获取bean实例的2个工具方法,发现每次调用后,都需要强制转换成目标结果类型。 这样很不方便,突然想到可以使用Java泛型方法,实现1个新的工具方法,避免了类型转换...

Java bean 转为xml

Java bean 转为xml可以采用XStream类来完成 pom.xml <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>...

多数据源切换-Druid

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。本文链接:https://blog.csdn.net/qq_37279783/article/details/82013702 这是实际应用场景中的多数据源切换案例 逻辑思路如下: 1.系统初始化,加载所有数据库中配置的数据源,加载进去spring容器...