Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
24.09k
stars
4.7k
forks
source link
How to get MDC after sending it from Tomcat thread to hystrix thread? #2030
I have a class A which runs on tomcat thread. Class B is inside a jar which is a dependency of the project which has class A. Class B is running on hystrix thread. I have implemented a CustomConcurrency strategy and a custom Hystrix hook which puts the MDC from class A in class B.
The logs which are getting printed on class B show the MDC which I put in HystrixHook but when I print MDC.get("id") from Class B, it shows nothing.
I have implemented the concurrency strategy and hystrixhook using this link.
CustomConcurrency.java
public class ThreadLocalUtil {
private static ThreadLocal<String> idTL = new ThreadLocal<>();
public static void setId(String id) {
idTL.set(id);
}
public static void getId() {
return idTL.get();
}
public static void clear() {
idTL.remove();
}
}
public class ConcurrencyStrategy extends HystrixConcurrencyStrategy {
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
String id = ThreadLocalUtil.getId();
return () -> {
try{
ThreadLocalUtil.setId(id);
return callable.call();
}
finally {
ThreadLocalUtil.clear()
}
};
}
}
// Register the concurrency strategy
HystrixPlugins.getInstance().registerConcurrencyStrategy(new ConcurrencyStrategy());
I have a class A which runs on tomcat thread. Class B is inside a jar which is a dependency of the project which has class A. Class B is running on hystrix thread. I have implemented a CustomConcurrency strategy and a custom Hystrix hook which puts the MDC from class A in class B.
The logs which are getting printed on class B show the MDC which I put in HystrixHook but when I print MDC.get("id") from Class B, it shows nothing.
I have implemented the concurrency strategy and hystrixhook using this link.
CustomConcurrency.java
HystrixHook.java