openfeign+hystrix降级类 注入失败问题

feign远程调用类:

@FeignClient(value = "third-service", fallback = ThirdpartyFallbackService.class)
public interface ThirdpartyService {
        ......
}

fallback降级类:

@Component
public class ThirdpartyFallbackService implements ThirdpartyService {
        ......
}

controller中注入的service类:

 @Qualifier("third-service") ThirdpartyService thirdpartyTencentOcrService

启动类:

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker
@EntityScan("com.xiaopang.entity")
@EnableJpaRepositories(basePackages ={"com.xiaopang.repo"})
public class Application {

   public static void main(String[] args) {
      SpringApplication.run(Application.class, args);
   }

}

 

结果启动报错:

........

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'driverInfoService' defined in file [C:\Users\xp\IdeaProjects\car-hail\server\driver\target\classes\com\cloudease\driver\service\DriverInfoService.class]: Unsatisfied dependency expressed through constructor parameter 7; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cloudease.driver.service.ThirdpartyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="third-service")}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:799)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1208)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:886)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:790)
    ... 20 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.cloudease.driver.service.ThirdpartyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="third-service")}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:886)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:790)
    ... 34 common frames omitted
百度了半天没有解决,然后点进去这个

@FeignClient  注解,结果看到一个 String qualifier 属性(已弃用,现在用 String[] qualifiers 替代了)

果断在feign接口上添加

qualifiers = "third-service"

然后再启动,启动成功!

修改后的feign接口:

@FeignClient(value = "third-service", qualifiers = "third-service", fallback = ThirdpartyFallbackService.class)
public interface ThirdpartyService {
        ......
}
上一篇:解决 Tasks support was removed in SonarQube 7.6. 的问题


下一篇:MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommend