Fescar和spring-cloud集成在使用Feign服务调用时不会出现xid问题

fescar与springcloud集成时,客户端调用微服务接口时,由于hystrix 做了线程隔离 ,

Hystrix会将请求放入Hystrix的线程池中去执行,此时会新启一个子线程处理请求。 而RootContext采用threadlocal传递数据,无法在子线程中传递,造成在子线程中通过RootContext.getXid()获取为空。 解决办法,在ThreadLocalContextCore类中,用InheritableThreadLocal 替代ThreadLocal。如下: public class ThreadLocalContextCore implements ContextCore {

//private ThreadLocal<Map<String, String>> threadLocal = new ThreadLocal<Map<String, String>>() private ThreadLocal<Map<String, String>> threadLocal = new InheritableThreadLocal<Map<String, String>>() { @Override protected Map<String, String> initialValue() { return new HashMap<String, String>(); }

};

原提问者GitHub用户wenchao523

展开
收起
学习娃 2023-06-14 16:46:21 83 分享 版权
1 条回答
写回答
取消 提交回答
  • hystrix 有信号量与线程池两种模式,应该只有使用线程池模式时才会出现这个问题。http://www.itmuch.com/spring-cloud-sum/hystrix-threadlocal/

    原回答者GitHub用户chengyan0

    2023-06-14 17:21:46
    赞同 展开评论

阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。

还有其他疑问?
咨询AI助理