标题:Copying Current Request Information Into Feign Interceptor With Hystrix Enabled
原文地址:
https://arnoldgalovics.com/feign-interceptor-hystrix/
问题
平时我们打开了Hystrix的时候,Interceptor无效了。
怎么搞?
怎么搞呢?
这个作者给出了他的修改:
@Component
public class RequestContextHolderHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
private HystrixConcurrencyStrategy delegate;
public RequestContextHolderHystrixConcurrencyStrategy() {
this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook();
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy();
HystrixPlugins.reset();
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
}
@Override
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
return delegate != null ? delegate.getBlockingQueue(maxQueueSize) : super.getBlockingQueue(maxQueueSize);
}
@Override
public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) {
return delegate != null ? delegate.getRequestVariable(rv) : super.getRequestVariable(rv);
}
@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixProperty<Integer> corePoolSize,
HystrixProperty<Integer> maximumPoolSize, HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
return delegate != null ? delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue)
: super.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolProperties threadPoolProperties) {
return delegate != null ? delegate.getThreadPool(threadPoolKey, threadPoolProperties) : super.getThreadPool(threadPoolKey, threadPoolProperties);
}
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
RequestContextHolderCallable<T> delegatingCallable = new RequestContextHolderCallable<>(callable, requestAttributes);
return delegate != null ? delegate.wrapCallable(delegatingCallable) : super.wrapCallable(delegatingCallable);
}
}
RequestContextHolder.java
public class RequestContextHolderCallable<V> implements Callable<V> {
private final Callable<V> delegate;
private final RequestAttributes originalRequestAttributes;
public RequestContextHolderCallable(Callable<V> delegate, RequestAttributes originalRequestAttributes) {
this.delegate = delegate;
this.originalRequestAttributes = originalRequestAttributes;
}
@Override
public V call() throws Exception {
RequestAttributes currentRequestAttributes = RequestContextHolder.getRequestAttributes();
try {
RequestContextHolder.setRequestAttributes(originalRequestAttributes);
return delegate.call();
} finally {
RequestContextHolder.setRequestAttributes(currentRequestAttributes);
}
}
}
原文请参考上方地址