Spring Cloud Feign--解决服务有多个FeignClient的报错:The bean ‘xxx‘ could not be registered. A bean with tha

原文网址:Spring Cloud Feign--解决服务有多个FeignClient的报错:The bean ‘xxx‘ could not be registered. A bean with tha_IT利刃出鞘的博客-CSDN博客

简介

        本文介绍feign对一个服务定义多个FeignClient时的报错:The bean 'xxx' could not be registered. A bean with that name has already been defined and overriding is disabled.

问题复现

@FeignClient("user")
public interface TestLongFeignService {
    @PostMapping("/user/long")
    public ResultWrapper testLong(String param1);
}
@FeignClient("user")
public interface UserFeignService {
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Long id);
}

错误日志: 

The bean 'user.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.

解决方案

方案1:使用contextId属性

@FeignClient(value = "user", contextId = "user1")
public interface TestLongFeignService {
    @PostMapping("/user/long")
    public ResultWrapper testLong(String param1);
}
@FeignClient(value = "user", contextId = "user2")
public interface UserFeignService {
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Long id);
}

方案2:方法都放到同一个类里边

@FeignClient("user")
public interface UserFeignService {
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Long id);

    @PostMapping("/user/long")
    public ResultWrapper testLong(String param1);
}

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/126607219