Hystrix 跨线程共享变量
1.在 Servlet 容器线程与 Hystrix 线程中共享变量的问题 今天遇到一个问题,在使用 zuul 的过程中我想要在 Hystrix 执行的线程中获取到 com.netflix.zuul.context.RequestContext 中的数据。不过 RequestContext 是基于 ThreadLocal 的,所以在 Hystrix 执行的线程中调用 com.netflix.zuul.context.RequestContext#getCurrentContext 函数获取到的只能是一个空的没有任何数据的 RequestContext 对象。因为 Hystrix 默认是在另一个线程中执行的 , 而 zuul filter 则是在 servlet 容器的线程中执行的,基于 ThreadLocal 的 RequestContext 自然无法起作用了。 2.在 Servlet 容器线程与 Hystrix 线程中共享变量的实现方式 2.1 关键类介绍 Hystrix 的设计者们早就考虑到了这个问题,并且提供了解决方案。关键类是 : com.netflix.hystrix.strategy.concurrency.HystrixRequestContext ,com.netflix.hystrix.strategy.concurrency