随笔 - 119  文章 - 0  评论 - 22  阅读 - 56万

openFeign调用问题:required a bean of type 'xxx' that could not be found.

场景:

服务B是一个公共的服务,打包成jar后给其他服务使用

package com.testB.seviceB.remote;
//服务B中定义的feign接口
@FeignClient(value = "service-c", path = "/service-c")
public interface ServiceBClient {
    xxxx
}

 

服务A中引用服务B中定义的Feign接口

package com.testA.serviceA.service;
@Service
public class CallServcieB {
    //引用服务B中的Feign接口
    @Autowired
    private ServiceBClient serviceBClient;
​
}
 

启动服务A 是报错:声明的 ServiceBClient 找不到

复制代码
2024-07-19 17:39:58.008 ERROR 47544 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
​
***************************
​
APPLICATION FAILED TO START
​
***************************
​
Description:
​
Field serviceBClient in com.testA.serviceA.service.CallServcieB required a bean of type 'com.testB.seviceB.remote.ServiceBClient' that could not be found.
​
The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
​
​
Action:
​
Consider defining a bean of type 'com.testB.seviceB.remote.ServiceBClient' in your configuration.
复制代码

报错原因: 

上面的报错是因为,启动类中Feign默认扫描的是当前服务A的包,需要将引用的服务B的类所在包的路径,也加入Feign的扫描路径中

 

解决:

修改启动类上Feign的扫描路径,将服务B的中的Feign路径加进来,这样就可以识别到引入的服务B的接口了。

com.testA.serviceA 是服务A的路径
com.testB.seviceB.remote  是服务B的Feign接口路径
复制代码
package com.testA.serviceA;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
​
@EnableDiscoveryClient
//加入引用的包路径
@EnableFeignClients(basePackages = {"com.testA.serviceA", "com.testB.seviceB.remote"})
@SpringBootApplication
public class ServiceAApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(ServiceAApplication.class, args);
    }
​
}
复制代码

 

 
posted on   欢跳的心  阅读(314)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示