1、错误日志

在 feign 开启熔断,配置 fallback 类,实现当前接口的实现类时,报错信息如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
ERROR --- [ 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] [::22.410][JdkDynamicAopProxy][]:
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

No fallback instance of type class found for feign client user-service(转)的更多相关文章

  1. Feign 报错:No fallback instance of type class xxx found for feign client xxx

    通常需要确认配置内容: 开启 Hystrix:feign.hystrix.enabled=true Fallback类需要注解@Component 出处:https://www.jianshu.com ...

  2. 【转】Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一个内部类.结果编译时出现:No enclosing instance of type E is accessible ...

  3. Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing   ...

  4. Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing--转

    原文:http://blog.csdn.net/sunny2038/article/details/6926079 最近在看Java,在编译写书上一个例子时,由于书上的代码只有一部分,于是就自己补了一 ...

  5. No enclosing instance of type test8 is accessible. Must qualify the allocation with an enclosing instance of type test8 (e.g. x.new A() where x is an

    在编译一个例子时,结果编译时出现: No enclosing instance of type test8 is accessible. Must qualify the allocation wit ...

  6. No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclosing instance of type Outer (e.g. x.new A() where x is an instance of Outer)

    之前看内部类的时候没发现这个问题,今天写代码的时候遇到,写个最简单的例子: 下面这一段代码 红色的部分就是编译报错: No enclosing instance of type Outer is ac ...

  7. No enclosing instance of type Hello is accessible

    1.static 关键字 修饰的成员被所有对象共享(包括成员变量和方法). 修饰的成员优先于对象存在. 存储于方法区(共享数据区)的静态区中. 静态方法只能访问静态成员. 静态方法中不可以使用this ...

  8. Java编译时出现No enclosing instance of type XXX is accessible.

    今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...

  9. 解决No enclosing instance of type * is accessible

    写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...

随机推荐

  1. 在python中使用print()时,raw write()返回无效的长度:OSError: raw write() returned invalid length 254 (should have been between 0 and 127)

    写出一个不是code的bug,很烦恼,解决了挺长时间,都翻到外文来看,不过还是解决了,只尝试了一种简单可观的方法,希望对大家有用 我正在使用Django与Keras(tensorflow)来训练一个模 ...

  2. python 10大算法之一 LinearRegression 笔记

    简单的线性回归预测房价 #!/usr/bin/env python # encoding: utf-8 """ @version: @author: --*--. @fi ...

  3. 反转链表II(根据指定的索引,逆转链表)

    反转从位置 m 到 n 的链表.请使用一趟扫描完成反转 思想:首先判断链表是否合法,并且判断输入的m和n是否合法 定义dummyHead结点,让它指向头结点的前驱,再定义cur,让它指向dummyHe ...

  4. 易忘&有用 的冷门Anaconda命令

    Python接触有将近2年了,但是在倒腾anaconda环境的时候,总有一些命令忘记需要查半天,今天趁着配置pytorch,特此总结一下,留待后用. 1. 创建和删除环境 创建环境 conda cre ...

  5. [Sublime]Sublime安装以及插件使用

    安装直接去官网下载安装了 安装Package Control关于安装Package Control,有两种方法. 一.自动安装 自动安装很方便,网上代码很多.我用的是Sublime Text3,通过V ...

  6. rabbitmq-cluster搭建

    一.前期准备: 准备3台linux系统,配置好公司源,或者官网下载相应的网络源 1.条件:准备3台linux系统,确保能连到download.yunwei.edu 2.编写yum源下载脚本: #vim ...

  7. 清除电脑缓存的bat文件

    电脑在使用了之后,会产生垃圾缓存,若不及时清理会降低电脑的运行速度. 1.步骤: 2.新建一个记事本文件,命名“系统清理”;(或其他名字) 3.原封不动复制下面的文字到该记事本中 @echo off ...

  8. java实现单链表反转(倒置)

    据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; ...

  9. 使用gitflow流程完成功能时报错

    报错 fatal: could not read Username for 'https://github.com': ······ 原因 使用https方式的时候 在https url 里面没有用户 ...

  10. 归并排序&&归并排序求逆序对

    归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...