在我们的项目中,比较广泛地使用了ThreadLocal,比如,在filter层,根据token,取到用户信息后,就会放到一个ThreadLocal变量中;在后续的业务处理中,就会直接从当前线程,来获取该ThreadLocal变量,然后获取到其中的用户信息,非常的方便。

但是,hystrix 这个组件一旦引入的话,如果使用线程隔离的方式,我们的业务逻辑就被分成了两部分,如下:

  1. public class SimpleHystrixCommand extends HystrixCommand<String> {
  2. private TestService testService;
  3. public SimpleHystrixCommand(TestService testService) {
  4. super(setter());
  5. this.testService = testService;
  6. }
  7. @Override
  8. protected String run() throws Exception {
  9. ....
  10. }
  11. ...
  12. }

首先,我们定义了一个Command,这个Command,最终就会丢给hystrix的线程池中去运行。那,我们的controller层,会怎么写呢?

  1. @RequestMapping("/")
  2. public String hystrixOrder () {
  3. SessionUtils.getSessionVOFromRedisAndPut2ThreadLocal();
  4. // 1
  5. SimpleHystrixCommand simpleHystrixCommand = new SimpleHystrixCommand(testService);
  6. // 2
  7. String res = simpleHystrixCommand.execute();
  8. return res;
  9. }
  • 上面的1处,new了一个HystrixCommand,这一步,还是在当前线程执行的;
  • 2处,在执行execute的过程中,最终就会把这个command,丢到线程池中,然后,command中的业务逻辑,就在线程池的线程中执行了。

所以,这中间,是有线程切换的,执行1时,当前线程里的ThreadLocal数据,在执行业务方法的时候,线程变了,也就取不到ThreadLocal数据了。

如果没时间,可以直接看源码:

https://gitee.com/ckl111/all-simple-demo-in-work-1/tree/master/hystrix-thread-local-demo

一开始,我的思路是,看看能不能把hystrix的默认线程池给换掉,因为构建HystrixCommand时,支持使用Setter的方式去配置。

如下:

  1. com.netflix.hystrix.HystrixCommand.Setter
  2. final public static class Setter {
  3. // 1
  4. protected final HystrixCommandGroupKey groupKey;
  5. // 2
  6. protected HystrixCommandKey commandKey;
  7. // 3
  8. protected HystrixThreadPoolKey threadPoolKey;
  9. // 4
  10. protected HystrixCommandProperties.Setter commandPropertiesDefaults;
  11. // 5
  12. protected HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults;
  13. }
  • 1处,设置命令组

  • 2处,设置命令的key

  • 3处,设置线程池的key;hystrix会根据这个key,在一个map中,来查找对应的线程池,如果找不到,则创建一个,并放到map中。

    1. com.netflix.hystrix.HystrixThreadPool.Factory
    2. final static ConcurrentHashMap<String, HystrixThreadPool> threadPools = new ConcurrentHashMap<String, HystrixThreadPool>();
  • 4处,命令的相关属性,包括是否降级,是否熔断,是否允许请求合并,命令执行的最大超时时长,以及metric等实时统计信息

  • 5处,线程池的相关属性,比如核心线程数,最大线程数,队列长度等

怎么样,可以设置的属性很多,是吧,但是,并没有让我们控制线程池的创建相关的,也没办法替换其默认线程池。

ok,那不用setter的方式,行不行呢?

HystrixCommand 的构造函数,看看能不能传入自定义的线程池呢?

经过我一开始不仔细的观察,发现有一个构造函数可以传入HystrixThreadPool,ok,就是它了。但是,后面仔细一看,竟然是 package权限,我的子类,和HystrixCommand当然不是一个package下的,所以,访问不了这个构造器。

虽然,可以使用反射,但是,咱们还是守规矩点好了,再看看有没有其他入口。

仔细观察下,看看线程池什么时候创建的?

入口在下图,每次new一个HystrixCommand,最终都会调用父类的构造函数:

上图所示处,initThreadPool里面,会去创建线程池,需要注意的是,这里的第一个实参,threadPool,是构造函数的第5个形参,目前来看,传进来的都是null。为啥说这个,我们接着看:

  1. private static HystrixThreadPool initThreadPool(HystrixThreadPool fromConstructor, HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults) {
  2. if (fromConstructor == null) {
  3. //1 get the default implementation of HystrixThreadPool
  4. return HystrixThreadPool.Factory.getInstance(threadPoolKey, threadPoolPropertiesDefaults);
  5. } else {
  6. return fromConstructor;
  7. }
  8. }

上面我们说了,第一个实参,总是null,所以,会走这里的1处。

  1. com.netflix.hystrix.HystrixThreadPool.Factory#getInstance
  2. static HystrixThreadPool getInstance(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter propertiesBuilder) {
  3. String key = threadPoolKey.name();
  4. //1 this should find it for all but the first time
  5. HystrixThreadPool previouslyCached = threadPools.get(key);
  6. if (previouslyCached != null) {
  7. return previouslyCached;
  8. }
  9. //2 if we get here this is the first time so we need to initialize
  10. synchronized (HystrixThreadPool.class) {
  11. if (!threadPools.containsKey(key)) {
  12. // 3
  13. threadPools.put(key, new HystrixThreadPoolDefault(threadPoolKey, propertiesBuilder));
  14. }
  15. }
  16. return threadPools.get(key);
  17. }
  • 1处,会查找缓存,就是前面说的,去map中,根据线程池的key,查找对应的线程池
  • 2处,没找到,则进行创建
  • 3处,new HystrixThreadPoolDefault,创建线程池

我们接着看3处:

  1. public HystrixThreadPoolDefault(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter propertiesDefaults) {
  2. // 1
  3. this.properties = HystrixPropertiesFactory.getThreadPoolProperties(threadPoolKey, propertiesDefaults);
  4. // 2
  5. HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
  6. // 3
  7. this.metrics = HystrixThreadPoolMetrics.getInstance(threadPoolKey,
  8. concurrencyStrategy.getThreadPool(threadPoolKey, properties),
  9. properties);
  10. // 4
  11. this.threadPool = this.metrics.getThreadPool();
  12. ...
  13. }
  • 1处,获取线程池的默认配置,这个就和我们前面说的那个Setter里的类似

  • 2处,从HystrixPlugins.getInstance()获取一个HystrixConcurrencyStrategy类型的对象,保存到局部变量 concurrencyStrategy

  • 3处,初始化metrics,这里的第二个参数,是concurrencyStrategy.getThreadPool来获取的,这个操作,实际上就会去创建线程池。

    1. com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy#getThreadPool
    2. public ThreadPoolExecutor getThreadPool(final HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) {
    3. final ThreadFactory threadFactory = getThreadFactory(threadPoolKey);
    4. ...
    5. final int keepAliveTime = threadPoolProperties.keepAliveTimeMinutes().get();
    6. final int maxQueueSize = threadPoolProperties.maxQueueSize().get();
    7. ...
    8. // 1
    9. return new ThreadPoolExecutor(dynamicCoreSize, dynamicCoreSize, keepAliveTime, TimeUnit.MINUTES, workQueue, threadFactory);
    10. }
    11. }

    上面的1处,会去创建线程池。但是,这里直接就是要了 jdk 的默认线程池类来创建,这还怎么搞?类型都定死了。没法扩展了。。。

但是,回过头来,又仔细看了看,这个getThreadPool 是 HystrixConcurrencyStrategy类的一个方法,这个方法也是个实例方法。

方法不能改,那,实例能换吗?再看看前面的代码:

ok,那接着分析:

  1. public HystrixConcurrencyStrategy getConcurrencyStrategy() {
  2. if (concurrencyStrategy.get() == null) {
  3. //1 check for an implementation from Archaius first
  4. Object impl = getPluginImplementation(HystrixConcurrencyStrategy.class);
  5. concurrencyStrategy.compareAndSet(null, (HystrixConcurrencyStrategy) impl);
  6. }
  7. return concurrencyStrategy.get();
  8. }

1处,根据这个类,获取实现,感觉有点戏。

  1. private <T> T getPluginImplementation(Class<T> pluginClass) {
  2. // 1
  3. T p = getPluginImplementationViaProperties(pluginClass, dynamicProperties);
  4. if (p != null) return p;
  5. // 2
  6. return findService(pluginClass, classLoader);
  7. }
  • 1处,从一个动态属性中获取,后来经查,发现是如果集成了Netflix Archaius就可以动态获取属性,类似于一个配置中心

  • 2处,如果前面没找到,就是要 JDK 的SPI机制。

    1. private static <T> T findService(
    2. Class<T> spi,
    3. ClassLoader classLoader) throws ServiceConfigurationError {
    4. ServiceLoader<T> sl = ServiceLoader.load(spi,
    5. classLoader);
    6. for (T s : sl) {
    7. if (s != null)
    8. return s;
    9. }
    10. return null;
    11. }

    那就好说了。SPI ,我们自定义一个实现,就可以替换掉默认的了,hystrix做的还是不错,扩展性可以。

现在知道可以自定义HystrixConcurrencyStrategy了,那要怎么自定义呢?

这个类,是个抽象类,大体有如下几个方法:

  1. getThreadPool
  2. getBlockingQueue(int maxQueueSize)
  3. Callable<T> wrapCallable(Callable<T> callable)
  4. getRequestVariable(final HystrixRequestVariableLifecycle<T> rv)

说是抽象类,但其实并没有需要我们实现的方法,所有方法都有默认实现,我们只需要重写需要覆盖的方法即可。

我这里,看重了第三个方法:

  1. /**
  2. * Provides an opportunity to wrap/decorate a {@code Callable<T>} before execution.
  3. * <p>
  4. * This can be used to inject additional behavior such as copying of thread state (such as {@link ThreadLocal}).
  5. * <p>
  6. * <b>Default Implementation</b>
  7. * <p>
  8. * Pass-thru that does no wrapping.
  9. *
  10. * @param callable
  11. * {@code Callable<T>} to be executed via a {@link ThreadPoolExecutor}
  12. * @return {@code Callable<T>} either as a pass-thru or wrapping the one given
  13. */
  14. public <T> Callable<T> wrapCallable(Callable<T> callable) {
  15. return callable;
  16. }

方法注释如上,我简单说下,在执行前,提供一个机会,让你去wrap这个callable,即最终要丢到线程池执行的那个callable。

我们可以wrap一下原有的callable,在执行前,把当前线程的threadlocal变量存下来,即为A,然后设置到callable里面去;在callable执行的时候,就可以使用我们的A中的threadlocal来替换掉worker线程中的。

多说无益,这里直接看代码:

  1. // 0
  2. public class MyHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
  3. @Override
  4. public <T> Callable<T> wrapCallable(Callable<T> callable) {
  5. /**
  6. * 1 获取当前线程的threadlocalmap
  7. */
  8. Object currentThreadlocalMap = getCurrentThreadlocalMap();
  9. Callable<T> finalCallable = new Callable<T>() {
  10. // 2
  11. private Object callerThreadlocalMap = currentThreadlocalMap;
  12. // 3
  13. private Callable<T> targetCallable = callable;
  14. @Override
  15. public T call() throws Exception {
  16. /**
  17. * 4 将工作线程的原有线程变量保存起来
  18. */
  19. Object oldThreadlocalMapOfWorkThread = getCurrentThreadlocalMap();
  20. /**
  21. *5 将本线程的线程变量,设置为caller的线程变量
  22. */
  23. setCurrentThreadlocalMap(callerThreadlocalMap);
  24. try {
  25. // 6
  26. return targetCallable.call();
  27. }finally {
  28. // 7
  29. setCurrentThreadlocalMap(oldThreadlocalMapOfWorkThread);
  30. log.info("restore work thread's threadlocal");
  31. }
  32. }
  33. };
  34. return finalCallable;
  35. }
  • 0处,自定义了一个类,继承HystrixConcurrencyStrategy,准备覆盖其默认的wrap方法
  • 1处,获取外部线程的threadlocal
  • 2处,3处,这里已经是处于匿名内部类了,定义了2个field,分别存放1中的外部线程的threadlocal,以及要wrap的callable
  • 4处,此时已经处于run方法的执行逻辑了:保存worker线程的自身的线程局部变量
  • 5处,使用外部线程的threadlocal覆盖自身的
  • 6处,调用真正的业务逻辑
  • 7处,恢复为线程自身的threadlocal

获取线程的threadlocal的代码:

  1. private Object getCurrentThreadlocalMap() {
  2. Thread thread = Thread.currentThread();
  3. try {
  4. Field field = Thread.class.getDeclaredField("threadLocals");
  5. field.setAccessible(true);
  6. Object o = field.get(thread);
  7. return o;
  8. } catch (NoSuchFieldException | IllegalAccessException e) {
  9. log.error("{}",e);
  10. }
  11. return null;
  12. }

设置线程的threadlocal的代码:

  1. private void setCurrentThreadlocalMap(Object newThreadLocalMap) {
  2. Thread thread = Thread.currentThread();
  3. try {
  4. Field field = Thread.class.getDeclaredField("threadLocals");
  5. field.setAccessible(true);
  6. field.set(thread,newThreadLocalMap);
  7. } catch (NoSuchFieldException | IllegalAccessException e) {
  8. log.error("{}",e);
  9. }
  10. }

https://github.com/Netflix/Hystrix/wiki/Plugins

  1. @RequestMapping("/")
  2. public String hystrixOrder () {
  3. // 1
  4. SessionUtils.getSessionVOFromRedisAndPut2ThreadLocal();
  5. // 2
  6. SimpleHystrixCommand simpleHystrixCommand = new SimpleHystrixCommand(testService);
  7. String res = simpleHystrixCommand.execute();
  8. return res;
  9. }
  • 1处,设置ThreadLocal变量

    1. public static UserVO getSessionVOFromRedisAndPut2ThreadLocal() {
    2. UserVO userVO = new UserVO();
    3. userVO.setUserName("test user");
    4. RequestContextHolder.set(userVO);
    5. log.info("set thread local:{} to context",userVO);
    6. return userVO;
    7. }
  • 2处,new了一个HystrixCommand,然后execute执行

  1. public class SimpleHystrixCommand extends HystrixCommand<String> {
  2. private TestService testService;
  3. public SimpleHystrixCommand(TestService testService) {
  4. super(setter());
  5. this.testService = testService;
  6. }
  7. @Override
  8. protected String run() throws Exception {
  9. // 1
  10. String s = testService.getResult();
  11. log.info("get thread local:{}",s);
  12. /**
  13. * 如果睡眠时间,超过2s,会降级
  14. * {@link #getFallback()}
  15. */
  16. int millis = new Random().nextInt(3000);
  17. log.info("will sleep {} millis",millis);
  18. Thread.sleep(millis);
  19. return s;
  20. }

重点看1处代码:

  1. public String getResult() {
  2. UserVO userVO = RequestContextHolder.get();
  3. log.info("I am hystrix pool thread,try to get threadlocal:{}",userVO);
  4. return userVO.toString();
  5. }

如上所示,会去获取ThreadLocal变量,并打印。

在resources\META-INF\services目录下,创建文件:

com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy

内容为下面一行:

com.learn.hystrix.utils.MyHystrixConcurrencyStrategy

访问:http://localhost:8080/

  1. 2020-05-09 17:26:11.134 INFO 7452 --- [nio-8080-exec-2] com.learn.hystrix.utils.SessionUtils : set thread local:UserVO(userName=test user) to context
  2. 2020-05-09 17:26:11.143 INFO 7452 --- [x-member-pool-2] com.learn.hystrix.service.TestService : I am hystrix pool thread,try to get threadlocal:UserVO(userName=test user)
  3. 2020-05-09 17:26:11.143 INFO 7452 --- [x-member-pool-2] c.l.h.command.SimpleHystrixCommand : get thread local:UserVO(userName=test user)
  4. 2020-05-09 17:26:11.144 INFO 7452 --- [x-member-pool-2] c.l.h.command.SimpleHystrixCommand : will sleep 126 millis
  5. 2020-05-09 17:26:11.281 INFO 7452 --- [x-member-pool-2] c.l.h.u.MyHystrixConcurrencyStrategy : restore work thread's threadlocal

可以看到,已经发生了线程切换,在worker线程也取到了。

大家如果发现日志中出现了[ HystrixTimer-1] 线程的身影,不用担心,那只是因为我们的线程超时了,所以timer线程检测到了之后,去执行一个callable任务,那个runnable就是前面被我们包装过的那个callable。(这块超时的机制,todo吧,下次再讲)

hystrix的插件机制,不止可以扩展上面这一个类,还有几个别的类也是可以的。大家直接参考:

https://github.com/Netflix/Hystrix/wiki/Plugins

代码demo,我放在了:

https://gitee.com/ckl111/all-simple-demo-in-work-1/tree/master/hystrix-thread-local-demo

版权声明:本文为grey-wolf原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/grey-wolf/p/12859084.html