Hystrix 传递ThreadLocal数据

7 篇文章 0 订阅

Hystrix 底层用了线程池,要传递ThreadLocal 数据,需要对HystrixConcurrencyStrategy 进行扩展


import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.gray.utils.GrayContextUtil;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.Map;
import java.util.concurrent.Callable;

public class RequestHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {

    private HystrixConcurrencyStrategy hystrixConcurrencyStrategy;


    public RequestHystrixConcurrencyStrategy() {
        HystrixPlugins hystrixPlugins = HystrixPlugins.getInstance();
        //当前策略
        HystrixConcurrencyStrategy strategy = hystrixPlugins.getConcurrencyStrategy();
        this.hystrixConcurrencyStrategy = strategy;

        HystrixEventNotifier hystrixEventNotifier = hystrixPlugins.getEventNotifier();
        HystrixCommandExecutionHook hystrixCommandExecutionHook = hystrixPlugins.getCommandExecutionHook();
        HystrixMetricsPublisher hystrixMetricsPublisher = hystrixPlugins.getMetricsPublisher();
        HystrixPropertiesStrategy hystrixPropertiesStrategy = hystrixPlugins.getPropertiesStrategy();

        HystrixPlugins.reset();
        //包装一层,在重新设置
        hystrixPlugins.registerConcurrencyStrategy(this);
        hystrixPlugins.registerEventNotifier(hystrixEventNotifier);
        hystrixPlugins.registerCommandExecutionHook(hystrixCommandExecutionHook);
        hystrixPlugins.registerMetricsPublisher(hystrixMetricsPublisher);
        hystrixPlugins.registerPropertiesStrategy(hystrixPropertiesStrategy);
    }

    @Override
    public <T> Callable<T> wrapCallable(Callable<T> callable) {
        if (hystrixConcurrencyStrategy != null){
            callable = hystrixConcurrencyStrategy.wrapCallable(callable);
        }

        return new WrappedCallable(callable, GrayContextUtil.getValues());
    }

    static class WrappedCallable<T> implements Callable<T> {

        private final Callable<T> target;
        private final Map<String,String> dataMap;

        public WrappedCallable(Callable<T> target,Map<String,String> dataMap) {
            this.target = target;
            this.dataMap = dataMap;
        }

        @Override
        public T call() throws Exception {
            try {
                //threadLocal数据传递
                GrayContextUtil.setValues(dataMap);
                return target.call();
            } finally {
                RequestContextHolder.resetRequestAttributes();
            }
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值