Copying current request information into Feign interceptor with Hystrix enabled

Not so long ago I came across a problem which was related to localization in a microservices architecture which was using Feign for internal service communication.

The idea was that if an incoming request has an Accept-Language  header, I should pass it over with every Feign request in order to have the locale information in every single service throughout the system. For this, I wanted to use RequestInterceptor , but luckily I was also using Hystrix enabled in the services.

The code I had in my interceptor is similar to the following:

Now what I experienced is that localization was simply not working. When I debugged this piece of code, it turned out that the I cannot access the current request bound to the thread. After a little bit of investigation I realized that Hystrix was playing me. By default, Hystrix is executing the Feign requests in a separate thread, meaning that the original request information will not be available as it’s stored in a ThreadLocal  variable. Unless you have some custom code to copy it.

So this is what I did:

The important method is the  wrapCallable which is basically overridden to propagate the original request attributes to the new thread. This is how the RequestContextHolderCallable  looks like:

After starting the application, the locale will be propagated correctly.

The example project can be found on my GitHub page.

3 Replies to “Copying current request information into Feign interceptor with Hystrix enabled”

    1. Hey, that will definitely solve the issue but bring in others. Semaphore isolation means that the same thread will be reused and Hystrix will use a semaphore to separate contexts which implies that in a system which needs high throughput, most probably it will not be as effective as it should be.

Leave a Reply

Your email address will not be published. Required fields are marked *