My goal
I want to be able to declare a feign.Client
bean (the http one) for each feign client. Furthermore, I want these feign.Client
beans to be customized for each feign client.
How I achieved this
The feign.Client
is retrieved/produced by FeignClientFactoryBean@L486.
Client client = getOptional(feignClientFactory, Client.class);
If I have a configuration that produces feign.Client
in the context used by FeignClientFactoryBean
then I will be able to control the feign.Client
retrieved by FeignClientFactoryBean
.
EnableFeignClients#defaultConfiguration
allows to do this so I declared this:
@Bean
@Scope("prototype")
public feign.Client feignHttpClient() {
return internalBuildFeignHttpClient("todo");
}
As I need to know the client I am building so I add some injections:
@Bean
@Scope("prototype")
public feign.Client feignHttpClient(
FeignClientFactoryBean feignClientFactoryBean,
FeignClientSpecification feignClientSpecification) {
final String clientName = feignClientFactoryBean.getName();
return internalBuildFeignHttpClient(clientName);
}
The problem
The problem with this code is that clientName
is always equal to the name of the first Feign client instantiated. This is because there is no FeignClientFactoryBean
in the current context (nor any way to know which Feign client is being built).
My suggestion
So, I propose to put in the current context the FeignClientFactoryBean
which uses it.
A temporary solution
For those who have the same use case as me, you can find the name of the client being built with this not very elegant trick:
@Bean
@Scope("prototype")
public feign.Client feignHttpClient(
FeignClientFactoryBean feignClientFactoryBean,
FeignClientSpecification feignClientSpecification,
GenericApplicationContext genericApplicationContext) {
// final String clientName = feignClientFactoryBean.getName();
final String clientName = genericApplicationContext.getDisplayName().substring(FeignClientFactory.class.getSimpleName().length() + 1);
return internalBuildFeignHttpClient(clientName);
}
Comment From: OlgaMaciaszek
Hello @lildadou, thanks for reporting the issue. This looks like a process very different from the usual flow and the base design of Spring Cloud OpenFeign. This is the first time anyone has indicated a scenario where they'd need to know the name of the Feign Client bean while creating the underlying feign.Client
and it's not something we are planning to support, so the workaround you're using is possibly the way to go at this point.
Comment From: spring-cloud-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-cloud-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.