Added support for customization of LoadBalancerFeignClient
i try define myLoadBalancerFeignClient extend of LoadBalancerFeignClient , and override the LBFC.execute,
@Configuration
@Slf4j
public class LoadBalancerConf {
@Bean
public Client myLoadBalancerFeignClient(CachingSpringLoadBalancerFactory lbClientFactory,
SpringClientFactory clientFactory) {
return new MyLoadBalancerFeignClient(new Client.Default(null, null), lbClientFactory, clientFactory);
}
public class MyLoadBalancerFeignClient extends LoadBalancerFeignClient {
public MyLoadBalancerFeignClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory,
SpringClientFactory clientFactory) {
super(delegate, lbClientFactory, clientFactory);
}
@Override
public Response execute(Request request, Options options) throws IOException {
Response resp = super.execute(request, options);
// do addtional logic here
log.info("add some business");
return resp;
}
}
}
when i use spring-cloud-sleuth in project ,i find that TraceFeignObjectWrapper.warp alway warp Client, if the Client instanceof LoadBalancerFeignClient,it will use client.getDelegate() to rebuild TraceLoadBalancerFeignClient,and myLoadBalancerFeignClient is useless and addtional logic not work.
the code of TraceFeignObjectWrapper
Object wrap(Object bean) {
if (bean instanceof Client && !(bean instanceof TraceFeignClient)) {
if (bean instanceof LoadBalancerFeignClient && !(bean instanceof TraceLoadBalancerFeignClient)) {
LoadBalancerFeignClient client = ((LoadBalancerFeignClient) bean);
return new TraceLoadBalancerFeignClient(
client.getDelegate(), factory(),
clientFactory(), this.beanFactory);
} else if (bean instanceof TraceLoadBalancerFeignClient) {
return bean;
}
return new TraceFeignClient(this.beanFactory, (Client) bean);
}
return bean;
}
I think that you should create a bean of Client type and not of LoadBalancerFeignClient. LoadBalancerFeignClient is a very specific feign ribbon related client. Why can't you create the feign.Client bean?
because i need do some logic after feign invoke, make sure do once only. if i create a bean of Feign.Client interface and not of LBFC,it maybe do more that once with happen retry,it is difficult to mark sure do once logic with once feign invoke.
We had a chat with @dsyer and it's extremely difficult to wrap things in such a way that the custom logic of LoadBalancerFeignClient gets executed. Actually we didn't consider anyone extend LoadBalancerFeignClient (maybe we should update the javadocs on this cc @spencergibb).
Up till now nobody has ever requested for this feature. We try to follow the convention of waiting for more people to require a feature before we implement it. Let's wait if someone else wants the feature before we consider adding it.
As a workaround I think you should create your custom logic in some higher abstraction layer. You can create an aspect around your business components that use feign and move your code into that aspect. I'll close this issue until we gather feedback from more interested users. Then, we might consider reopening it.