当网关和服务在实施全链路分布式灰度发布和路由时候,咱们须要一款追踪系统来监控网关和服务走的是哪一个灰度组,哪一个灰度版本,哪一个灰度区域,甚至监控从Http Header头部全程传递的灰度规则和路由策略。这个功能意义在于:java
笔者尝试调研了一系列分布式追踪系统和中间件,包括Opentracing、Uber Jaeger、Twitter Zipkin、Apache Skywalking、Pinpoint、CAT等,最后决定采用Opentracing + Uber Jaeger方式来实现,重要缘由除了易用性和可扩展性外,Opentracing支持WebMvc和WebFlux两种方式,业界的追踪系统能支持WebFlux相对较少git
[OpenTracing] OpenTracing已进入CNCF,正在为全球的分布式追踪系统提供统一的概念、规范、架构和数据标准。它经过提供平台无关、厂商无关的API,使得开发人员可以方便的添加(或更换)追踪系统的实现。对于存在多样化的技术栈共存的调用链中,Opentracing适配Java、C、Go和.Net等技术栈,实现全链路分布式追踪功能。迄今为止,Uber Jaeger、Twitter Zipkin和Apache Skywalking已经适配了Opentracing规范github
笔者以Nepxion社区的Discovery开源框架(对该开源框架感兴趣的同窗,请访问以下连接)为例子展开整合spring
源码主页,请访问
https://github.com/Nepxion/Discovery微信
指南主页,请访问
https://github.com/Nepxion/DiscoveryGuide架构
文档主页,请访问
https://pan.baidu.com/s/1i57rXaNKPuhGRqZ2MONZOA#list/path=%2FNepxionapp
整合的效果图框架
灰度调用链主要包括以下11个参数。使用者能够自行定义要传递的调用链参数,例如:traceId, spanId等;也能够自行定义要传递的业务调用链参数,例如:mobile, user等分布式
1. n-d-service-group - 服务所属组或者应用 2. n-d-service-type - 服务类型,分为“网关”和“服务” 3. n-d-service-id - 服务ID 4. n-d-service-address - 服务地址,包括Host和Port 5. n-d-service-version - 服务版本 6. n-d-service-region - 服务所属区域 7. n-d-version - 版本路由值 8. n-d-region - 区域路由值 9. n-d-address - 地址路由值 10. n-d-version-weight - 版本权重路由值 11. n-d-region-weight - 区域权重路由值
源码参考
https://github.com/Nepxion/Discovery/tree/master/discovery-plugin-strategy-opentracingide
因为OpenTracing扩展须要兼顾到Spring Cloud Gateway、Zuul和服务,它的核心逻辑存在着必定的可封装性,因此笔者抽取出一个公共模块discovery-plugin-strategy-opentracing,包含configuration、operation、context等模块,着重阐述operation模块,其它比较简单,不一一赘述了
在阐述前,笔者须要解释一个配置,该配置将决定核心实现以及终端界面的显示
# 启动和关闭调用链的灰度信息在Opentracing中以独立的Span节点输出,若是关闭,则灰度信息输出到原生的Span节点中。缺失则默认为true spring.application.strategy.trace.opentracing.separate.span.enabled=true
Opentracing公共操做类 - StrategyOpentracingOperation.java
public class StrategyOpentracingOperation { private static final Logger LOG = LoggerFactory.getLogger(StrategyOpentracingOperation.class); @Autowired protected PluginAdapter pluginAdapter; @Autowired protected StrategyContextHolder strategyContextHolder; @Autowired private Tracer tracer; @Value("${" + StrategyOpentracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_ENABLED + ":false}") protected Boolean traceOpentracingEnabled; @Value("${" + StrategyOpentracingConstant.SPRING_APPLICATION_STRATEGY_TRACE_OPENTRACING_SEPARATE_SPAN_ENABLED + ":true}") protected Boolean traceOpentracingSeparateSpanEnabled; public void opentracingInitialize() { if (!traceOpentracingEnabled) { return; } if (!traceOpentracingSeparateSpanEnabled) { return; } Span span = tracer.buildSpan(DiscoveryConstant.SPAN_VALUE).start(); StrategyOpentracingContext.getCurrentContext().setSpan(span); LOG.debug("Trace chain for Opentracing initialized..."); } public void opentracingHeader(Map<String, String> customizationMap) { if (!traceOpentracingEnabled) { return; } Span span = getCurrentSpan(); if (span == null) { LOG.error("Span not found in context to opentracing header"); return; } if (MapUtils.isNotEmpty(customizationMap)) { for (Map.Entry<String, String> entry : customizationMap.entrySet()) { span.setTag(entry.getKey(), entry.getValue()); } } if (traceOpentracingSeparateSpanEnabled) { span.setTag(Tags.COMPONENT.getKey(), DiscoveryConstant.TAG_COMPONENT_VALUE); } span.setTag(DiscoveryConstant.PLUGIN, DiscoveryConstant.PLUGIN_VALUE); span.setTag(DiscoveryConstant.TRACE_ID, span.context().toTraceId()); span.setTag(DiscoveryConstant.SPAN_ID, span.context().toSpanId()); span.setTag(DiscoveryConstant.N_D_SERVICE_GROUP, strategyContextHolder.getHeader(DiscoveryConstant.N_D_SERVICE_GROUP)); ... String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION); if (StringUtils.isNotEmpty(routeVersion)) { span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion); } ... LOG.debug("Trace chain information outputs to Opentracing..."); } public void opentracingLocal(String className, String methodName, Map<String, String> customizationMap) { if (!traceOpentracingEnabled) { return; } Span span = getCurrentSpan(); if (span == null) { LOG.error("Span not found in context to opentracing local"); return; } if (MapUtils.isNotEmpty(customizationMap)) { for (Map.Entry<String, String> entry : customizationMap.entrySet()) { span.setTag(entry.getKey(), entry.getValue()); } } if (traceOpentracingSeparateSpanEnabled) { span.setTag(Tags.COMPONENT.getKey(), DiscoveryConstant.TAG_COMPONENT_VALUE); } span.setTag(DiscoveryConstant.PLUGIN, DiscoveryConstant.PLUGIN_VALUE); span.setTag(DiscoveryConstant.CLASS, className); span.setTag(DiscoveryConstant.METHOD, methodName); span.setTag(DiscoveryConstant.TRACE_ID, span.context().toTraceId()); span.setTag(DiscoveryConstant.SPAN_ID, span.context().toSpanId()); span.setTag(DiscoveryConstant.N_D_SERVICE_GROUP, pluginAdapter.getGroup()); ... String routeVersion = strategyContextHolder.getHeader(DiscoveryConstant.N_D_VERSION); if (StringUtils.isNotEmpty(routeVersion)) { span.setTag(DiscoveryConstant.N_D_VERSION, routeVersion); } ... LOG.debug("Trace chain information outputs to Opentracing..."); } public void opentracingError(String className, String methodName, Throwable e) { if (!traceOpentracingEnabled) { return; } if (!traceOpentracingSeparateSpanEnabled) { return; } Span span = getCurrentSpan(); if (span == null) { LOG.error("Span not found in context to opentracing error"); return; } span.log(new ImmutableMap.Builder<String, Object>() .put(DiscoveryConstant.CLASS, className) .put(DiscoveryConstant.METHOD, methodName) .put(DiscoveryConstant.EVENT, Tags.ERROR.getKey()) .put(DiscoveryConstant.ERROR_OBJECT, e) .build()); LOG.debug("Trace chain error outputs to Opentracing..."); } public void opentracingClear() { if (!traceOpentracingEnabled) { return; } if (!traceOpentracingSeparateSpanEnabled) { return; } Span span = getCurrentSpan(); if (span != null) { span.finish(); } else { LOG.error("Span not found in context to opentracing clear"); } StrategyOpentracingContext.clearCurrentContext(); LOG.debug("Trace chain context of Opentracing cleared..."); } public Span getCurrentSpan() { return traceOpentracingSeparateSpanEnabled ? StrategyOpentracingContext.getCurrentContext().getSpan() : tracer.activeSpan(); } public String getTraceId() { if (!traceOpentracingEnabled) { return null; } Span span = getCurrentSpan(); if (span != null) { return span.context().toTraceId(); } return null; } public String getSpanId() { if (!traceOpentracingEnabled) { return null; } Span span = getCurrentSpan(); if (span != null) { return span.context().toSpanId(); } return null; } }
实现OpenTracing对服务的扩展,包含configuration、tracer等模块,着重阐述tracer模块,其它比较简单,不一一赘述了
Opentracing的服务追踪类 - DefaultServiceStrategyOpentracingTracer.java
public class DefaultServiceStrategyOpentracingTracer extends DefaultServiceStrategyTracer { @Autowired private StrategyOpentracingOperation strategyOpentracingOperation; @Override public void trace(ServiceStrategyTracerInterceptor interceptor, MethodInvocation invocation) { strategyOpentracingOperation.opentracingInitialize(); super.trace(interceptor, invocation); strategyOpentracingOperation.opentracingLocal(interceptor.getMethod(invocation).getDeclaringClass().getName(), interceptor.getMethodName(invocation), getCustomizationMap()); } @Override public void error(ServiceStrategyTracerInterceptor interceptor, MethodInvocation invocation, Throwable e) { super.error(interceptor, invocation, e); strategyOpentracingOperation.opentracingError(interceptor.getMethod(invocation).getDeclaringClass().getName(), interceptor.getMethodName(invocation), e); } @Override public void release(ServiceStrategyTracerInterceptor interceptor, MethodInvocation invocation) { super.release(interceptor, invocation); strategyOpentracingOperation.opentracingClear(); } @Override public String getTraceId() { return strategyOpentracingOperation.getTraceId(); } @Override public String getSpanId() { return strategyOpentracingOperation.getSpanId(); } }
实现OpenTracing对Spring Cloud Gateway的扩展,跟discovery-plugin-strategy-starter-service-opentracing模块相似,不一一赘述了
源码参考
https://github.com/Nepxion/Discovery/tree/master/discovery-plugin-strategy-starter-zuul-opentracing
实现OpenTracing对Zuul的扩展,跟discovery-plugin-strategy-starter-service-opentracing模块相似,不一一赘述了
示例参考
https://github.com/Nepxion/DiscoveryGuide
Opentracing输出方式以Uber Jaeger为例来讲明,步骤很是简单
对于Opentracing调用链功能的开启和关闭,须要经过以下开关作控制:
# 启动和关闭调用链。缺失则默认为false spring.application.strategy.trace.enabled=true # 启动和关闭调用链的Opentracing输出,支持F版或更高版本的配置,其它版本不须要该行配置。缺失则默认为false spring.application.strategy.trace.opentracing.enabled=true # 启动和关闭调用链的灰度信息在Opentracing中以独立的Span节点输出,若是关闭,则灰度信息输出到原生的Span节点中。缺失则默认为true spring.application.strategy.trace.opentracing.separate.span.enabled=true
自定义调用链上下文参数的建立(该类不是必须的),继承DefaultStrategyTracerAdapter
// 自定义调用链上下文参数的建立 // 对于getTraceId和getSpanId方法,在Opentracing等调用链中间件引入的状况下,由调用链中间件决定,在这里定义不会起做用;在Opentracing等调用链中间件未引入的状况下,在这里定义才有效,下面代码中表示从Http Header中获取,并全链路传递 // 对于getCustomizationMap方法,表示输出到调用链中的定制化业务参数,能够同时输出到日志和Opentracing等调用链中间件,下面代码中表示从Http Header中获取,并全链路传递 public class MyStrategyTracerAdapter extends DefaultStrategyTracerAdapter { @Override public String getTraceId() { return StringUtils.isNotEmpty(strategyContextHolder.getHeader(DiscoveryConstant.TRACE_ID)) ? strategyContextHolder.getHeader(DiscoveryConstant.TRACE_ID) : StringUtils.EMPTY; } @Override public String getSpanId() { return StringUtils.isNotEmpty(strategyContextHolder.getHeader(DiscoveryConstant.SPAN_ID)) ? strategyContextHolder.getHeader(DiscoveryConstant.SPAN_ID) : StringUtils.EMPTY; } @Override public Map<String, String> getCustomizationMap() { return new ImmutableMap.Builder<String, String>() .put("mobile", StringUtils.isNotEmpty(strategyContextHolder.getHeader("mobile")) ? strategyContextHolder.getHeader("mobile") : StringUtils.EMPTY) .put("user", StringUtils.isNotEmpty(strategyContextHolder.getHeader("user")) ? strategyContextHolder.getHeader("user") : StringUtils.EMPTY) .build(); } }
在配置类里@Bean方式进行调用链类建立,覆盖框架内置的调用链类
@Bean public StrategyTracerAdapter strategyTracerAdapter() { return new MyStrategyTracerAdapter(); }
任浩军, 10 多年开源经历,Github ID:@HaojunRen,Nepxion 开源社区创始人,Nacos Group Member,Spring Cloud Alibaba & Nacos & Sentinel Committer
微信、公众号和文档
转载,请保留原文地址,谢谢 ~