Search code examples
springspring-bootspring-cloud-feignfeignopenfeign

How to make multiple FeignClient-s to use same serviceId/name?


assume 2 endpoints:

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

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

On backend side are both running in same server which is registered as serviceId="MY-SERVER" in Eureka or Consul or ...

@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"
    }
}

now my 2 FeignClients have to share same name/serviceId to be able to discover the service in Eureka/Consul but serviceId should be unique per feign client! ...how to deal with this?

@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.

Please consider there could be much more endpoints on single server/backend before advicing me to join it into single interface...


Solution

  • oooh, I see now! What should be unique is contextId not value/name

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