首页  

dubbo 分布式追踪 traceId 传递要点     所属分类 dubbo 浏览量 113
traceId 透传

业务请求对象里 增加 traceId 字段  (侵入 不推荐)


filter RpcContext 
RpcContext  ThreadLocal对象  维护一次rpc交互的上下文信息

RpcContext 里的 attachments 信息会填充到 RpcInvocation 中


public class RpcInvocation implements Invocation, Serializable {
    private String               methodName;
    private Class<?>[]           parameterTypes;
    private Object[]             arguments;
    private Map<String, String>  attachments;
    private transient Invoker<?> invoker;
 
 
public interface Invoker<T> extends Node



Invocation  
Invoker

注意!!!
RpcContext attachment 在一次rpc交互后会被清空
因此需要引入 TraceIdUtils 保存并重新设置 traceId
每次rpc调用前, 重新设置traceId

ConsumerContextFilter

RpcContext clearAttachments 断点  调用堆栈

java.lang.Thread.State: RUNNABLE
      at com.alibaba.dubbo.rpc.RpcContext.clearAttachments(RpcContext.java:438)
      at com.alibaba.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:50)
      at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
      at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
      at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:77)
      at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:227)
      at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:72)
      at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
      at com.alibaba.dubbo.common.bytecode.proxy0.echo(proxy0.java:-1)
      at com.test.dubbo.EchoServiceConsumer.main(EchoConsumer.java:20)
      
      

public class TraceIdUtils {

    private static final ThreadLocal<String> traceIdCache
            = new ThreadLocal<String>();

    public static String getTraceId() {
        return traceIdCache.get();
    }

    public static void setTraceId(String traceId) {
        traceIdCache.set(traceId);
    }

    public static void clear() {
        traceIdCache.remove();
    }
}


package com.dyyx;

public class TraceIdFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String traceId = RpcContext.getContext().getAttachment("traceId");
        if ( !StringUtils.isEmpty(traceId) ) {
            // 从RpcContext里获取traceId并保存
            TraceIdUtils.setTraceId(traceId);
        } else {
            // 交互前重新设置traceId 
            RpcContext.getContext().setAttachment("traceId", TraceIdUtils.getTraceId());
        }
        // 实际的rpc调用
        return invoker.invoke(invocation);
    }

}
resource/META-INF/dubbo目录
com.alibaba.dubbo.rpc.Filter

traceIdFilter=com.dyyx.TraceIdFilter

dubbo producer和consumer 配置filter

<dubbo:service interface="com.dyyx.EchoService" ref="echoService" version="1.0.0"
            filter="traceIdFilter"/>


<dubbo:reference interface="com.dyyx.EchoService" id="echoService" version="1.0.0" 
                     filter="traceIdFilter"/>
                     
                    


上一篇     下一篇
Java 位运算实例

K8S简介

十六条实用的人生经验

dubbo之RpcContext

dubbo RpcContexta attachments 实例

Dubbo Filter 顺序