Hystrix实现ThreadLocal传递

总体上是 自定义扩展类实现Callable接口,并传入当前Callable变量delegate,在delegate执行call方法前后进行线程上线文的操作即可实现线程状态在父线程与子线程间的传播

public HystrixThreadPoolDefault(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties.Setter propertiesDefaults) {
	this.properties = HystrixPropertiesFactory.getThreadPoolProperties(threadPoolKey, propertiesDefaults);
	HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
	this.queueSize = properties.maxQueueSize().get();

	this.metrics = HystrixThreadPoolMetrics.getInstance(threadPoolKey,
			concurrencyStrategy.getThreadPool(threadPoolKey, properties),
			properties);
	this.threadPool = this.metrics.getThreadPool();
	this.queue = this.threadPool.getQueue();

	/* strategy: HystrixMetricsPublisherThreadPool */
	HystrixMetricsPublisherFactory.createOrRetrievePublisherForThreadPool(threadPoolKey, this.metrics, this.properties);
}

创建线程池的地方,可以进行定制,换掉 HystrixPlugins.getInstance().getConcurrencyStrategy();

public HystrixConcurrencyStrategy getConcurrencyStrategy() {
	if (concurrencyStrategy.get() == null) {
		// check for an implementation from Archaius first
		Object impl = getPluginImplementation(HystrixConcurrencyStrategy.class);
		if (impl == null) {
			// nothing set via Archaius so initialize with default
			concurrencyStrategy.compareAndSet(null, HystrixConcurrencyStrategyDefault.getInstance());
			// we don't return from here but call get() again in case of thread-race so the winner will always get returned
		} else {
			// we received an implementation from Archaius so use it
			concurrencyStrategy.compareAndSet(null, (HystrixConcurrencyStrategy) impl);
		}
	}
	return concurrencyStrategy.get();
}




private <T> T getPluginImplementation(Class<T> pluginClass) {
	T p = getPluginImplementationViaProperties(pluginClass, dynamicProperties);
	if (p != null) return p;        
	return findService(pluginClass, classLoader);
}

这里可以spi 进行定制 MyHystrixConcurrencyStrategy  wrapCallable方法如下 :

@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
	
	 // 获取当前线程的threadlocalmap
	

	Callable<T> finalCallable = new Callable<T>() {
		
		private Object callerThreadlocalMap = currentThreadlocalMap;
		
		private Callable<T> targetCallable = callable;

		@Override
		public T call() throws Exception {
			
			 //TODO 将工作线程的原有线程变量保存起来
			
			// 将本线程的线程变量,设置为caller的线程变量
			 
			try {
				return targetCallable.call();
			}finally {
				// 恢复原来的线程变量
				log.info("restore work thread's threadlocal");
			}

		}
	};

	return finalCallable;
}

第二种实现方式如下

public HystrixContexSchedulerAction(final HystrixConcurrencyStrategy concurrencyStrategy, Action0 action) {
	this.actual = action;
	this.parentThreadState = HystrixRequestContext.getContextForCurrentThread();

	this.c = concurrencyStrategy.wrapCallable(new Callable<Void>() {

		@Override
		public Void call() throws Exception {
			HystrixRequestContext existingState = HystrixRequestContext.getContextForCurrentThread();
			try {
				// set the state of this thread to that of its parent
				HystrixRequestContext.setContextOnCurrentThread(parentThreadState);
				// execute actual Action0 with the state of the parent
				actual.call();
				return null;
			} finally {
				// restore this thread back to its original state
				HystrixRequestContext.setContextOnCurrentThread(existingState);
			}
		}
	});
}

猜你喜欢

转载自blog.csdn.net/ma_ru_long/article/details/106792099