當網關和服務在實施全鏈路分佈式灰度發佈和路由時候,咱們須要一款追蹤系統來監控網關和服務走的是哪一個灰度組,哪一個灰度版本,哪一個灰度區域,甚至監控從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
微信、公衆號和文檔
轉載,請保留原文地址,謝謝 ~