Reputation: 171
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.
Upvotes: 7
Views: 13937
Reputation: 491
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)
Upvotes: 0
Reputation: 33
You have to create new FeignConfiguration class for your new Feign client, Also you should remove the @Configuration from your Feign configuration classes.
Upvotes: 0
Reputation: 119
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
Upvotes: 8