question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support different custom configurations for multiple feign clients with the same name

See original GitHub issue

using @FeignClient in spring currently has one fatal issue, which is demonstrated with this repository. If I got two feign clients, each having it’s own configuration (excluded from component scan), and one of them has an RequestInterceptor, both (and all others, if present) get this interceptor also applied. The repository shows the following experiment to show this:

experiment

We assume some foreign service, serving “foo” entities. You have to be authenticated to access this the foo resource and have a role “USER”. If you have “ADMIN”, you will see different data, as when “USER”.

This behavior is implemented using plain spring boot with spring security in the “producer” service. Additionally it registers to an eureka instance, so some “consumer” service is able to fetch this.

So the consumer service should fetch data from consumer, automatically passing basic auth of “users he know”. While this may not clearly makes sense for real world, something very similar is happening consuming OAuth2 secured resource servers with client credentials grant! So to get the data correctly, we define 2 clients, with individually set up basic auth credentials using feign client configuration.

Since the main application does not use @SpringBootApplication, but @EnableAutoConfiguration, and an exclude filter on @ComponentScan, the two different configuration do not declare the internal beans.

So the expected result of these 2 clients is: the UserFooClient retrieving at least a foo entity with value “user1”, while the AdminFooClients result should contain a “admin1” foo.

To verify, which result is intended, the same test is done using plain old RestTemplate.

consequences

This issue leads towards we can’t really use multiple feign clients in spring boot using the @FeignClient annotation, because as soon one client is using some authorization flow (basic, oauth2), this is applied to all other clients.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:37 (26 by maintainers)

github_iconTop GitHub Comments

4reactions
ryanjbaxtercommented, Aug 11, 2016

@xetys Based on @spencergibb’s suggestion you can do something like this to accomplish what you want

        @Autowired
        public FooController(
                ResponseEntityDecoder decoder, SpringEncoder encoder, EurekaClient discoveryClient) {
            InstanceInfo prodSvcInfo =  discoveryClient.getNextServerFromEureka("PROD-SVC", false);
            this.fooClient = Feign.builder()
                    .encoder(encoder)
                    .decoder(decoder)
                     .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
                     .target(FooClient.class, prodSvcInfo.getHomePageUrl());
            this.adminClient = Feign.builder()
                    .encoder(encoder)
                    .decoder(decoder)
                     .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
                     .target(FooClient.class, prodSvcInfo.getHomePageUrl());
        }

You could probably also integrate Ribbon into the situation as well.

2reactions
dsyercommented, Jul 26, 2016

You don’t need all that infrastructure to reproduce this problem. All you need is 2 feign clients with the same name and different configurations (they can’t actually have different configuration because the configuration is keyed on the name).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support different custom configurations for multiple feign ...
Now, it is possible to declare multiple feign clients with different names, but the same serviceId. When serviceId is present then it will...
Read more >
Setting Up Multiple Configurations for Feign Clients [A Step-by ...
Consider a Feign client that must be used with different configurations at different places in the code, or multiple Feign clients that each ......
Read more >
How to make multiple FeignClient-s to use same serviceId ...
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 ......
Read more >
Multiple Configurations for Feign Clients - Amir Shokri - Medium
We have two Feign clients for two services, FooClient and BarClient. These Feign clients need to adopt different authentication configuration.
Read more >
1. Declarative REST Client: Feign - Spring Cloud
If we want to create multiple feign clients with the same name or url so that they would point to the same server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found