0

I need to access different instances of a server sharing the same REST interface.

for one server, or different instances of the same server, I would use Ribbon and a feignClient, but the servers are not interchangeable.

I've got a list of server adresses in my application.yml file, likewise:

servers:
  - id: A
    url: http://url.a
  - id: B
    url: http://url.b

I'd like to be able to request a server regarding input parameter, for example:

ClientA -> /rest/api/request/A/get -> http://url.a/get
ClientB -> /rest/api/request/B/get -> http://url.b/get

The middleware is agnostic regarding the clients, but the backend server is bound to the clients.

many clients -> one middleware -> some clients

Who would you achieve that using Feign? is it even possible?

Alexandre Mélard
  • 12,229
  • 3
  • 36
  • 46

1 Answers1

2

The simplest way is to create two Feign targets using the reusing the interface and builder.

Client clientA = Feign.builder()
                      .target(Client.class, "https://url.a");
Client clientB = Feign.builder()
                      .target(Client.class, "https://url.b");

This will create a new Client for each target url, however, by ensuring that the supporting components such as the Encoder, Decoder, Client, and ErrorDecoder are singleton instances and thread-safe, the cost of the client will be minimal.

If you don't want to create multiple clients, the alternative is to include a URI as a method parameter.

@RequestLine("POST /repos/{owner}/{repo}/issues")
void createIssue(URI host, Issue issue, @Param("owner") String owner, @Param("repo") String repo);

The value host in the example above will replace the base uri provided in the builder. The drawback to this approach you will need to modify your interface to add this URI to the appropriate methods and adjust the callers to supply the target.

Kevin Davis
  • 1,193
  • 8
  • 14