spring - 如何让多个 Fe​​ignClient-s 使用相同的 serviceId/name?

标签 spring spring-boot spring-cloud-feign feign openfeign

假设有 2 个端点:

@RequestMapping("/ep1")
interface Endpoint1 {
    @GetMapping("/echo")
    String echo();
}

@RequestMapping("/ep2")
interface Endpoint2 {
    @GetMapping("/echo")
    String echo();
}

后端都在同一个服务器上运行,该服务器在 Eureka 或 Consul 或...中注册为 serviceId="MY-SERVER"

@RestController
public class Endpoint1Controller implements Endpoint1 {
    public String echo() {
        return "echo from Endpoint1"
    }
}
@RestController
public class Endpoint2Controller implements Endpoint2 {
    public String echo() {
        return "echo from Endpoint2"
    }
}

现在我的 2 个 FeignClients 必须共享相同的名称/serviceId 才能在 Eureka/Consul 中发现服务,但每个 feign 客户端的 serviceId 应该是唯一的! ...如何处理?

@FeignClient("MY-SERVER")
public interface Endpoint1Client extends Endpoint1 {
}

@FeignClient("MY-SERVER")
public interface Endpoint2Client extends Endpoint2 {
}


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

在建议我将其加入单一界面之前,请考虑在单个服务器/后端上可能有更多的端点......

最佳答案

哦,我现在明白了!唯一的是 contextId 而不是 value/name

@FeignClient(name="MY-SERVER", contextId = "THIS-SHOULD-BE-UNIQUE")

关于spring - 如何让多个 Fe​​ignClient-s 使用相同的 serviceId/name?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69950791/

相关文章:

java - 使用 jackson 响应中的指定键反序列化为类

java - 在 Junit 测试中异常 HashMap 返回 null 后

spring - 在其中保存带有缓存对象的实体导致分离实体异常

java - 使用 Mockito 和 jUnit5 的 Spring Boot Controller 测试失败,因为 Spring 无法创建加载属性的 Property Service 类的 bean?

java - Spring Boot 应用程序 java -jar --property 覆盖不起作用

spring-boot - 如何在 Spring Boot 中记录 json 响应?

spring - 如何在 Java Spring 应用程序中缓存使用 Netflix Feign 库发出的 HTTP 请求

Spring Data JPA PageRequest 按 Map 值属性排序

java - 如何处理 "feign.FeignException: [301 Moved Permanently] during [GET]"?

spring-cloud - Spring Cloud |假装 Hytrix |首次调用超时