Creating a Feign configuration just for one client?
Asked Answered
I

3

7

I have a whole bunch of Feign clients that uses a shared configuration(MyFeignConfiguration.class):

@FeignClient(name = "clientA", url = "http://serviceA.com", fallbackFactory = ServiceAFallbackFactory.class, configuration = MyFeignConfiguration.class)

@FeignClient(name = "clientB", url = "http://serviceB.com", fallbackFactory = ServiceBFallbackFactory.class, configuration = MyFeignConfiguration.class)

@FeignClient(name = "clientC", url = "http://serviceC.com", fallbackFactory = ServiceCFallbackFactory.class, configuration = MyFeignConfiguration.class)

However, for a new client, I want to change the underlying Http Client that is used to OkHttp one. In the MyFeignConfiguration class, I can add the following:

@Configuration
class MyFeignConfiguration {
    @Bean
    public Client getClient() {
        return OkHttpClient() // use the OkHttp client 
    }
   
    @Bean
    public ErrorDecoder getErrorDecoder() {
        //... existing configs
    }

However, now all of the clients are using this OkHttp client. How do configure the new feign client so that only it is using the OkHttp client? Also, I still need to use the existing default configs(like the ErrorDecoder) from my main MyFeignConfiguration class.

Integer answered 24/1, 2022 at 22:50 Comment(0)
K
8

looking at the doc there is a imported Note https://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-overriding-defaults

FooConfiguration does not need to be annotated with @Configuration. However, if it is, then take care to exclude it from any @ComponentScan that would otherwise include this configuration as it will become the default source for feign.Decoder, feign.Encoder, feign.Contract, etc., when specified. This can be avoided by putting it in a separate, non-overlapping package from any @ComponentScan or @SpringBootApplication, or it can be explicitly excluded in @ComponentScan.

Try to remove the annotation: @Configuration

Kataway answered 24/1, 2022 at 22:59 Comment(0)
A
0

You have to create new FeignConfiguration class for your new Feign client, Also you should remove the @Configuration from your Feign configuration classes.

Aliaalias answered 3/2, 2022 at 16:4 Comment(0)
S
0

As other answers mentioned, you'll have to remove @configuration from the Feign configuration so the beans don't get autowired.

Along with this, I also had to remove the defaultConfiguration = FeignConfig.class from my Application.java class. Otherwise, the default configuration set here always gets picked up.

@EnableFeignClients(basePackages = {"com.example"}, defaultConfiguration = FeignConfig.class)

This also means that in all feign clients you'll have to set the configurations manually.

@FeignClient(contextId = "myClient", value = "${api.name}", url = "${api.ip}", configuration = FeignConfig.class)
Slier answered 13/9 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.