出错分析
小编在一个微服务项目中,进行feign的远程调用时,由于是跨服务之间的feign接口调用
,出现了扫描不到接口的错误
,也就是出错的原因!
==写在前面:==
赶时间的小伙伴直接提高扫描的范围即可,在启动类上添加:
@ComponentScan(basePackages = "com.pig4cloud.pig.*.*")
小编给出我们的服务架构图,小伙伴对比看一下是否一样的问题:
报错信息:
代码语言:javascript复制Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.pig4cloud.pig.wms.feign.RemoteProductService': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No fallbackFactory instance of type class com.pig4cloud.pig.wms.feign.factory.RemoteProductServiceFallbackFactory found for feign client remoteProductService
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 34 common frames omitted
Caused by: java.lang.IllegalStateException: No fallbackFactory instance of type class com.pig4cloud.pig.wms.feign.factory.RemoteProductServiceFallbackFactory found for feign client remoteProductService
at com.pig4cloud.pig.common.feign.ext.PigSentinelFeign$Builder$1.getFromContext(PigSentinelFeign.java:109)
at com.pig4cloud.pig.common.feign.ext.PigSentinelFeign$Builder$1.create(PigSentinelFeign.java:99)
at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:64)
at feign.Feign$Builder.target(Feign.java:269)
at org.springframework.cloud.openfeign.DefaultTargeter.target(DefaultTargeter.java:30)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:330)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:360)
at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:339)
at org.springframework.cloud.openfeign.FeignClientsRegistrar.lambda$registerFeignClient$0(FeignClientsRegistrar.java:230)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1231)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1173)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
... 43 common frames omitted
==根本原因:==
我们可以看到feign的扫描是扫描不到我们另外一个服务的,这时需要我们使用spring的注解@ComponentScan
来扩大搜索范围。我们需要扩大扫描的范围:com.pig4cloud.pig.ns
和com.pig4cloud.pig.wms
,我们可以可以合并一下:com.pig4cloud.pig.*.*
。第一个*
为两个服务下的ns和wms
,我们需要扫描下面的所有包我们直接使用第二个*
。
解决方案
在启动类上添加:
@ComponentScan(basePackages = "com.pig4cloud.pig.*.*")
重启完美解决!!
Q.E.D.