微服务全链路跟踪:grpc集成zipkin

微服务全链路跟踪:grpc集成jaeger

微服务全链路跟踪:springcloud集成jaeger

微服务全链路跟踪:jaeger集成istio,并兼容uber-trace-id与b3

微服务全链路跟踪:jaeger集成hystrix

微服务全链路跟踪:jaeger增加tag参数

背景

> 当springcloud服务集成hystrix,并且用了hystrixCommend注解到方法上时,jaeger链路会断掉

方案

在网上搜索到了大量jaeger遇到多线程时的处理方式,都是包装线程池来做到ThreadLocal传递,有很多都用到了阿里开源的transmittable-thread-local

下面说一下当集成hystrix时,jaeger链路丢失问题,大家都知道hystrix默认是线程池隔离,所以归根结底还是遇到多线程线程变量没有共享的问题,网上也罗列了几种方案:

方案一:变更隔离方式

  1. hystrix.command.default.execution.isolation.strategy: SEMAPHORE

当并发高时这里设置信号量隔离是有风险的,可以根据情况优化断路器配置来降低风险

方案二:自定义隔离策略

隔离策略官方文档有定义:

原先我就定义了一个feign传递request中header信息的策略,在原有的隔离策略下面参考https://github.com/opentracing-contrib/java-concurrent项目的代码:

将原有feign自定义隔离策略做了响应变动,代码如下

  1. /**
  2. * 自定义Feign的隔离策略:
  3. * 在转发Feign的请求头的时候, 如果开启了Hystrix,
  4. * Hystrix的默认隔离策略是Thread(线程隔离策略), 因此转发拦截器内是无法获取到请求的请求头信息的,
  5. * 可以修改默认隔离策略为信号量模式:hystrix.command.default.execution.isolation.strategy=SEMAPHORE,
  6. * 这样的话转发线程和请求线程实际上是一个线程, 这并不是最好的解决方法, 信号量模式也不是官方最为推荐的隔离策略;
  7. * 另一个解决方法就是自定义Hystrix的隔离策略:
  8. * 思路是将现有的并发策略作为新并发策略的成员变量,在新并发策略中,
  9. * 返回现有并发策略的线程池、Queue;将策略加到Spring容器即可;
  10. */
  11. @Component
  12. @Slf4j
  13. public class FeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
  14. private HystrixConcurrencyStrategy delegate;
  15. @Lazy
  16. @Autowired
  17. private Tracer tracer;
  18. public FeignHystrixConcurrencyStrategy() {
  19. try {
  20. this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
  21. if (this.delegate instanceof FeignHystrixConcurrencyStrategy) {
  22. // Welcome to singleton hell...
  23. return;
  24. }
  25. HystrixCommandExecutionHook commandExecutionHook =
  26. HystrixPlugins.getInstance().getCommandExecutionHook();
  27. HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
  28. HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
  29. HystrixPropertiesStrategy propertiesStrategy =
  30. HystrixPlugins.getInstance().getPropertiesStrategy();
  31. this.logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher, propertiesStrategy);
  32. HystrixPlugins.reset();
  33. HystrixPlugins instance = HystrixPlugins.getInstance();
  34. instance.registerConcurrencyStrategy(this);
  35. instance.registerCommandExecutionHook(commandExecutionHook);
  36. instance.registerEventNotifier(eventNotifier);
  37. instance.registerMetricsPublisher(metricsPublisher);
  38. instance.registerPropertiesStrategy(propertiesStrategy);
  39. } catch (Exception e) {
  40. log.error("Failed to register Sleuth Hystrix Concurrency Strategy", e);
  41. }
  42. }
  43. private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
  44. HystrixMetricsPublisher metricsPublisher,
  45. HystrixPropertiesStrategy propertiesStrategy) {
  46. if (log.isDebugEnabled()) {
  47. log.debug("Current Hystrix plugins configuration is [" + "concurrencyStrategy ["
  48. + this.delegate + "]," + "eventNotifier [" + eventNotifier + "]," + "metricPublisher ["
  49. + metricsPublisher + "]," + "propertiesStrategy [" + propertiesStrategy + "]," + "]");
  50. log.debug("Registering Sleuth Hystrix Concurrency Strategy.");
  51. }
  52. }
  53. @Override
  54. public <t> Callable<t> wrapCallable(Callable<t> callable) {
  55. RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  56. return new WrappedCallable&lt;&gt;(callable, requestAttributes,tracer,tracer.activeSpan());
  57. }
  58. @Override
  59. public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
  60. HystrixProperty<integer> corePoolSize,
  61. HystrixProperty<integer> maximumPoolSize,
  62. HystrixProperty<integer> keepAliveTime,
  63. TimeUnit unit, BlockingQueue<runnable> workQueue) {
  64. return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime,
  65. unit, workQueue);
  66. }
  67. @Override
  68. public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
  69. HystrixThreadPoolProperties threadPoolProperties) {
  70. return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
  71. }
  72. @Override
  73. public BlockingQueue<runnable> getBlockingQueue(int maxQueueSize) {
  74. return this.delegate.getBlockingQueue(maxQueueSize);
  75. }
  76. @Override
  77. public <t> HystrixRequestVariable<t> getRequestVariable(HystrixRequestVariableLifecycle<t> rv) {
  78. return this.delegate.getRequestVariable(rv);
  79. }
  80. static class WrappedCallable<t> implements Callable<t> {
  81. private final Callable<t> target;
  82. private final RequestAttributes requestAttributes;
  83. private final Span span;
  84. private final Tracer tracer;
  85. public WrappedCallable(Callable<t> target, RequestAttributes requestAttributes,Tracer tracer, Span span) {
  86. this.target = target;
  87. this.requestAttributes = requestAttributes;
  88. this.tracer=tracer;
  89. this.span=span;
  90. }
  91. @Override
  92. public T call() throws Exception {
  93. Scope scope = span == null ? null : tracer.scopeManager().activate(span);
  94. try {
  95. RequestContextHolder.setRequestAttributes(requestAttributes);
  96. return target.call();
  97. } finally {
  98. RequestContextHolder.resetRequestAttributes();
  99. if (scope != null) {
  100. scope.close();
  101. }
  102. }
  103. }
  104. }
  105. }

其中改动部分就是在原有的WrappedCallable中增加了span、trace的注入。

至于自定义隔离策略以及Callable是可以支持多个链的,这里不做详细描述,大家有兴趣可以参考,下面的链接:

https://blog.csdn.net/songhaifengshuaige/article/details/80345012

https://www.jianshu.com/p/c60fe209a799

https://www.cnblogs.com/duanxz/p/10949816.html

微服务全链路跟踪:jaeger集成hystrix的更多相关文章

  1. Go微服务全链路跟踪详解

    在微服务架构中,调用链是漫长而复杂的,要了解其中的每个环节及其性能,你需要全链路跟踪. 它的原理很简单,你可以在每个请求开始时生成一个唯一的ID,并将其传递到整个调用链. 该ID称为Correlati ...

  2. Spring Cloud 微服务分布式链路跟踪 Sleuth 与 Zipkin

    Zipkin 是一个开放源代码分布式的跟踪系统,由 Twitter 公司开源,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集.存储.查找和展现.它的理论模型来自于Google ...

  3. 微服务, 架构, 服务治理, 链路跟踪, 服务发现, 流量控制, Service Mesh

    微服务, 架构, 服务治理, 链路跟踪, 服务发现, 流量控制, Service Mesh 微服务架构   本文将介绍微服务架构和相关的组件,介绍他们是什么以及为什么要使用微服务架构和这些组件.本文侧 ...

  4. 微服务之分布式跟踪系统(springboot+zipkin+mysql)

    通过上一节<微服务之分布式跟踪系统(springboot+zipkin)>我们简单熟悉了zipkin的使用,但是收集的数据都保存在内存中重启后数据丢失,不过zipkin的Storage除了 ...

  5. SpringCloud初体验:六、利用 Sleuth 和 Zipkin 给微服务加上链路监控追踪查看功能

    首先:装上 Zipkin 服务,收集调用链跟踪数据,体验时装在了本机docker上, 方便快捷 docker run -d -p : openzipkin/zipkin 安装后访问地址也是 9411端 ...

  6. springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  7. SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践

    一. 前言 日志对于一个程序的重要程度不用过多的言语修饰,本篇将以实战的方式讲述开源微服务全栈项目 有来商城 是如何整合当下主流日志解决方案 ELK +Filebeat . 话不多说,先看实现的效果图 ...

  8. SpringBoot之微服务日志链路追踪

    SpringBoot之微服务日志链路追踪 简介 在微服务里,业务出现问题或者程序出的任何问题,都少不了查看日志,一般我们使用 ELK 相关的日志收集工具,服务多的情况下,业务问题也是有些难以排查,只能 ...

  9. IDEA 集成 Docker 插件实现一键远程部署 SpringBoot 应用,无需三方依赖,开源微服务全栈项目有来商城云环境的部署方式

    一. 前言 最近有些童鞋对开源微服务商城项目 youlai-mall 如何部署到线上环境以及项目中 的Dockerfile 文件有疑问,所以写了这篇文章做个答疑以及演示完整的微服务项目发布到线上的流程 ...

  10. 全链路跟踪skywalking简介

    该文章主要包括以下内容: skywalking的简介 skywalking的使用,支持多种调用中间件(httpclent,springmvc,dubbo,mysql等等) skywalking的tra ...

随机推荐

  1. 什么是spring,它能够做什么?

    1.什么是SpringSpring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的. Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情. ...

  2. 为什么有些IP无法PING通但又能访问

    背景 在调试板子的网络,突然发现板子无法ping通开发机(出现request timed out,),而电脑却可以ping通板子. 而scp.ssh以及nfs等工具却可以正常使用. 原理 找了一些资料 ...

  3. python爬虫-request模块

    1. requests 中的请求方法 HTTP 请求方法: requests.get(url, params=None, **kwargs) # GET 请求 requests.post(url, d ...

  4. 全志科技T3国产工业评估板规格书(四核ARM Cortex-A7,主频1.2GHz)

    1 评估板简介 创龙科技TLT3-EVM是一款基于全志科技T3处理器设计的4核ARM Cortex-A7高性能低功耗国产评估板,每核主频高达1.2GHz,由核心板和评估底板组成. 评估板接口资源丰富, ...

  5. vulnhub - w1r3s.v1.0.1

    vulnhub - w1r3s.v1.0.1 高质量视频教程 - b站红队笔记 靶机下载 本地环境 本机ip:192.168.157.131 w1r3s虚拟机设置NAT模式 信息收集 扫描网段得到攻击 ...

  6. java生成word的解决方案比较

    1.Jacob Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.通过Jacob实现了在Java平台上对微软Office的COM接口进行调用. 优点:调 ...

  7. 洛谷P1432

    水一道绿题,整体思路和八数码很像,哈希表存解,然后常规模拟即可 #include<iostream> #include<utility> #include<queue&g ...

  8. FairMOT复现报错存档

    FairMOT复现 使用pip命令单独安装Cython包即可 修改下载的cython-bbox包里的setup.py里的代码 将#extra_compile_args=['-Wno-cpp'], 修改 ...

  9. [rCore学习笔记 016]实现应用程序

    写在前面 本随笔是非常菜的菜鸡写的.如有问题请及时提出. 可以联系:1160712160@qq.com GitHhub:https://github.com/WindDevil (目前啥也没有 设计方 ...

  10. Java maven构建命令使用总结

    实践环境 Apache Maven 3.0.5 (Red Hat 3.0.5-17) maven构建生命周期 学习Maven构建命令之前,我们不烦先简单了解下Maven构建生命周期. Maven基于构 ...