在Spring Cloud框架中,使用Feign进行远程调用时,若遇到No bean found错误,通常由依赖版本不匹配引起。以下步骤可帮助排查解决。
No bean found of type interface org.springframework.cloud.openfeign.Targeter for 服务名.
想要使用springcloud组件,一般在父工程pom文件里加入:
org.springframework.cloud
spring-cloud-dependencies
${springcloud.version}
pom
import
而后在要使用的子pom中引入openFeign依赖,最好不要添加版本,就是因为手欠,加了version,导致报错
org.springframework.cloud
spring-cloud-starter-openfeign
手动加入3.1.6版本,但是和springcloud版本不符合,然后删除version后,Maven自动帮我们下载了对应的版本2.2.7

@SpringBootApplication @EnableFeignClients(basePackages = "com.example.client")
@FeignClient("UserService")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
细心哪!
总结以上经验,通过查看依赖版本、添加注解和正确编写接口,能有效解决Feign远程调用中的Bean创建问题。
您可能感兴趣的文章: