Project scenario
feign + hystrix fuse for remote calling
Problem description
Bug occurs when starting remote calling class
2023-09-17 17:05:21.017 [main] ERROR o.s.boot.SpringApplication - Application run failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'wmNewsAutoScanServiceImpl': Unsatisfied dependency expressed through field 'articleClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.itheima .apis.article.IArticleClient': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No fallback instance of type class com.itheima.apis.article.fallback.IArticleClientFallback found for feign client leadnews-article at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1425) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) 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.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) at com.heima.wemedia.WemediaApplication.main(WemediaApplication.java:22) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.itheima.apis.article.IArticleClient': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No fallback instance of type class com.itheima.apis.article.fallback.IArticleClientFallback found for feign client leadnews-article at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529) 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ... 21 common frames omitted Caused by: java.lang.IllegalStateException: No fallback instance of type class com.itheima.apis.article.fallback.IArticleClientFallback found for feign client leadnews-article at org.springframework.cloud.openfeign.HystrixTargeter.getFromContext(HystrixTargeter.java:81) at org.springframework.cloud.openfeign.HystrixTargeter.targetWithFallback(HystrixTargeter.java:72) at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:49) at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:338) at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:369) at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:347) at org.springframework.cloud.openfeign.FeignClientsRegistrar.lambda$registerFeignClient$0(FeignClientsRegistrar.java:240) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1235) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ... 29 common frames omitted 2023-09-17 17:05:21.018 [Thread-5] WARN c.a.n.c.http.HttpClientBeanHolder - [HttpClientBeanHolder] Start destroying common HttpClient 2023-09-17 17:05:21.018 [Thread-5] WARN c.a.n.c.http.HttpClientBeanHolder - [HttpClientBeanHolder] Destruction of the end
Cause analysis:
Looking at the error report, there is no instance of the fallback class.
So first you need to add the @component annotation to the fallback class to let spring generate a bean.
If adding @Component is invalid, it is not invalid, but the path of the scanned package is different. If we customize our own business module and name it something other than this, then the fallback instance equipped with the @Component annotation cannot be scanned. Because SpringBoot scans all packages in the same directory as Application by default. But the package names of the business modules are inconsistent.
Solution:
Add the @ComponentScan(“xxx”) annotation to the remote call startup class.
And using idea’s Build > Rebuild Project instead of the usual Build Project (Ctrl + F9) can solve the problem. The reason is that Build will only compile the modified files, but this time, involving the content in the annotations, you need Forcibly regenerate all classes. Only Rebuild Project has this effect.