关于链路追踪,在微服务的趋势下,一次调用的日志信息分布在不一样的机器上或目录下,当须要看一条链路调用全部的日志信息时,这是个比较困难的地方,咱们虽然有ELK , Sentry等日志异常收集分析工具, 可是如何把信息串起来也是一个关键的问题。 咱们通常的作法是在系统调用开始时生成一个traceId , 而且它伴随着一次调用的整个生命周期 。 当一个服务调用另一个服务的时候,traceId 则向下透传,全局使用惟一一个。html
咱们经过分析源码能够知道客户端在调用服务段进行服务消费时,实际上发送的是封装过的Request实体 ,Data为Invocation实体对象(接口签名,参数类型,参数值,及attachment附件)java
final class HeaderExchangeChannel implements ExchangeChannel { ... public ResponseFuture request(Object request, int timeout) throws RemotingException { if (closed) { throw new RemotingException(this.getLocalAddress(), null, "Failed to send request " + request + ", cause: The channel " + this + " is closed!"); } // create request. Request req = new Request(); req.setVersion("2.0.0"); req.setTwoWay(true); req.setData(request); DefaultFuture future = new DefaultFuture(channel, req, timeout); try { channel.send(req); } catch (RemotingException e) { future.cancel(); throw e; } return future; } ... }
经过源码可知 request是入参,其余参数均为固定,因此只能在request中作文章。apache
TraceIdUtil源码以下dom
public class TraceIdUtil { private static final ThreadLocal<String> TRACE_ID = new ThreadLocal<String>(); public static String getTraceId() { if(TRACE_ID.get() == null) { String s = UUID.randomUUID().toString(); setTraceId(s); } return TRACE_ID.get(); } public static void setTraceId(String traceId) { TRACE_ID.set(traceId); } }
由Dubbo线程模型图示可知,Dubbo客户端调用实际上经过JavassistProxyFactory获取的是Proxy代理对象。 代码以下ide
public class JavassistProxyFactory extends AbstractProxyFactory { ... @SuppressWarnings("unchecked") public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) { return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker)); } ... }
其中 InvokerInvocationHandler 调用数据的处理及rpc调用实体的封装过程。因此咱们须要在调用的起始位置添加traceId信息 。 微服务
其次是服务提供者进行请求处理过程: DubboProtocol的requestHandler 请求处理器(不明白请详读服务Dubbo服务暴露过程源码) 工具
将traceId 从RpcInvocation 的attachment属性中取出 ,传给TraceIdUtil 方面后续调用过程使用。this
建立Filter 服务提供者端扩展线程
/** * Created with IntelliJ IDEA. * * @author: bakerZhu * @description: * @time: 2018年09月09日 * @modifytime: */ @Activate(group = {Constants.CONSUMER, Constants.PROVIDER} , order = -9999) public class GlobalTraceFilter implements Filter { @Override public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { String traceId = invocation.getAttachment("traceId"); if(!StringUtils.isBlank(traceId)) { RpcContext.getContext().setAttachment("traceId",traceId); }else { // 第一次发起调用 RpcContext.getContext().setAttachment("traceId", UUID.randomUUID().toString()); } return invoker.invoke(invocation); } }
资源文件夹下建立 META-INF/dubbo 文件夹 建立com.alibaba.dubbo.rpc.Filter 文件,并编辑文件内容 gtrace=com.alibaba.dubbo.rpc.filter.GlobalTraceFilter3d
详见 AbstractInvoker.invoke(Invocation inv) 方法
public abstract class AbstractInvoker<T> implements Invoker<T> { public Result invoke(Invocation inv) throws RpcException { ...... Map<String, String> context = RpcContext.getContext().getAttachments(); if (context != null) { invocation.addAttachmentsIfAbsent(context); } if (getUrl().getMethodParameter(invocation.getMethodName(), Constants.ASYNC_KEY, false)) { invocation.setAttachment(Constants.ASYNC_KEY, Boolean.TRUE.toString()); } RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation); ...... } }
将上下文的“附件信息”拷贝到RpcInvocation中