1

I have 2 services S1 and S2. Calling S2 using annotated Feign client(@FeignClient) from S1. The issue is, I am unable to get traceId in S2.

But when I try to call S2 using RestTemplate it works.

Any help will be appreciated

Edited:

I have find out the cause actually I am using Feign.Builder below is sample code which builds fiegn client.

@ConditionalOnProperty(name = "feign.hystrix.enabled")
    public Feign.Builder feignHystrixBuilder() {
        SetterFactory setterFactory = new SetterFactory() {
            @Override
            public HystrixCommand.Setter create(Target<?> target, Method method) {
                String groupKey = target.name();
                String commandKey = target.name();
                return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
            }
        };
        return HystrixFeign.builder().setterFactory(setterFactory);
    }

Actually due to above config.. SleuthFeignHystrixBuilder is not invoked. I need to set HysterixCommandKey in my format.. thats why need above config.

How can it work with spring-sleuth ?

Zeeshan
  • 1,173
  • 5
  • 19
  • 26

1 Answers1

1

I have implemented Spring's BeanPostProcessor interface and then set 'setterFactory'. Refer to below sample code

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

    if(bean instanceof HystrixFeign.Builder) {

        ((HystrixFeign.Builder)bean).setterFactory(new SetterFactory() {
            @Override
            public HystrixCommand.Setter create(Target<?> target, Method method) {
                String groupKey = target.name();
                String commandKey = target.name();
                return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
                    .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
            }
        });

    }
    return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
Zeeshan
  • 1,173
  • 5
  • 19
  • 26
  • Shouldn't the last line simply be `return bean;` ? I know the interface has a default method that does exactly that, this seems a bit cumbersome. – john16384 Oct 16 '20 at 09:56