全链路参数透传

简单分布式架构图示例:
请添加图片描述

背景

现在的系统大多是基于SOA/MSA的分布式应用。跨服务调用的时候,存在线程上线文里的重要信息如traceId,租户id无法传递给下游应用。

要解决的问题

  1. 内部系统通过RocketMQ交互时,traceId如何透传到消息消费应用
  2. 内部系统通过Dubbo调用时, traceId如何透传到服务提供方
  3. http调用这里不做赘述,参数塞入header里面,用filter获取。

解决方案

1 rocketMQ异步透传解决方案

发消息时,通过切面拦截 DefaultMQProducer.send 方法,将traceId从threadLocal写入Message的properties里面

@Before("execution(* org.apache.rocketmq.client.producer.DefaultMQProducer.send*(..))")
public void addTraceId(JoinPoint joinPoint) {
    
    
    Message message = (Message) joinPoint.getArgs()[0];
    message.getProperties().put("traceId", TraceIdContext.getTraceId());
}

收消息时,通过aop拦截 MQMessageHandler.handlerMessage 方法,将traceId从Message的properties里面写入到threadLocal里

@Before("execution(* MQMessageHandler.handlerMessage(..))")
public void addTraceId(JoinPoint joinPoint) {
    
    
    MessageExt message = (MessageExt) joinPoint.getArgs()[0];
    TraceIdContext.put(message.getProperties().get("TraceId"));
}

2 dubbo调用透传解决方案

dubbo调用时,提供了一个隐式参数传递的功能,详见官方文档 https://dubbo.apache.org/zh/docs/v2.7/user/examples/attachment/,但是这个功能有一个问题

setAttachment 设置的 KV 对,在完成下面一次远程调用会被清空,即多次远程调用要多次设置。

这里还需要配合dubbo提供的一个SPI扩展点改造——Filter,每次调用的都时候都从threadLocal取出来进行隐式传递,参见官方文档 https://dubbo.apache.org/zh/docs/v2.7/dev/impls/filter/

消费方调用服务时,从threadLocal中取出traceId,然后进行dubbo隐式参数传递

@Activate(group = Constants.CONSUMER)
public class TraceIdConsumerFilter implements Filter {
    
    
     
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    
    
        RpcContext.getContext().setAttachment("TraceId", TraceIdContext.getTraceId());
        return invoker.invoke(invocation);
    }
}

服务提供方处理请求时,从隐式参数中取出TraceId,写入threadLocal。完成参数透传

@Activate(group = Constants.PROVIDER)
public class TraceIdProviderFilter implements Filter {
    
    
     
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    
    
        String TraceId = RpcContext.getContext().getAttachment("TraceId");
        TraceIdContext.put(TraceId);
        return invoker.invoke(invocation);
    }
}

这两个spi扩展点需要进行配置
在这里插入图片描述

3. 异步线程线程参数传递问题求解

使用jdk自带的InheritableThreadLocal替换常用的ThreadLocal。手动new出来的线程也可以传递参数。InheritableThreadLocal会将父线程的所有值都作为自己的初始值。jdk的文档如下:

This class extends ThreadLocal to provide inheritance of values from parent thread to child thread: when a child thread is created, the child receives initial values for all inheritable thread-local variables for which the parent has values

public class TraceIdContext {
    
    
    
    private static InheritableThreadLocal<String> merchantThreadLocal = new InheritableThreadLocal<>();
    
    public static void put(String TraceId) {
    
    
        merchantThreadLocal.set(TraceId);
    }
    
    public static String get() {
    
    
        return merchantThreadLocal.get();
    }
    
    public static String getTraceId() {
    
    
        return merchantThreadLocal.get();
        
    }
    
    /**
     * 线程终止前要调用这个方法,防止内存泄漏
     */
    public static void remove() {
    
    
        merchantThreadLocal.remove();
    }
}

猜你喜欢

转载自blog.csdn.net/bruce128/article/details/123788596