欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dubbo 全链路追踪日志的实现

程序员文章站 2022-03-05 08:07:59
...

目标

http接口

所有HTTP API响应体中增加traceId字段:

{
    "code": 200,
    "data": "Hello world",
    "msg": "ok",
    "traceId": "bd41aed8b2da4895a9d2b43d1ef12595"
}

dubbo接口

对于服务调用方:每次调用服务时,都需要向服务提供方传递traceId

对于服务提供方:每次服务响应时,都需要从服务调用方获取traceId,并将该traceId传递下去,直至该响应结束。

可对traceId的命名进行规范。

如:

  • req:xxa:xxx 表示 前端请求,xxa模块,序号标识xxx
  • tim:xxc:xxx 表示 定时器触发,xxc模块,序号标识xxx

实现思路

http

项目内部使用org.slf4j.MDC传递traceId。
使用拦截器完成traceId的设置与清除。请求到来时,生成并设置traceId;请求结束时,清除traceId。
拦截器中无法修改HTTP响应体。可通过ControllerAdvice统一向Response Body中写入traceId。

Dubbo

使用Dubbo提供的org.apache.dubbo.rpc.Filter来完成traceId的设置与获取。

实现

  1. 项目中traceId生成、传递、销毁

请求过来之后,生成traceId,并写入org.slf4j.MDC

请求完成之后,将traceIdorg.slf4j.MDC中移除。

import com.example.dubboserviceb.constants.Constants;
import org.slf4j.MDC;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

public class TraceIdInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // generate traceId
        String traceId = UUID.randomUUID().toString().replace("-", "");

        // put traceId
        MDC.put(Constants.TRACE_ID, traceId);

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        // clear traceId
        MDC.remove(Constants.TRACE_ID);
    }
}
import lombok.Data;

@Data
public class RestResponse<T> {
    private Integer code;
    private String msg;
    private T data;
    private String traceId;

    public RestResponse() {
    }

    public RestResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> RestResponse<T> ok(T data) {
        return new RestResponse<>(200, "ok", data);
    }

    public static <T> RestResponse<T> error(T data) {
        return new RestResponse<>(400, "error", data);
    }
}
import com.example.dubboserviceb.constants.Constants;
import com.example.dubboserviceb.utils.RestResponse;
import org.slf4j.MDC;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;


@ControllerAdvice
public class ResponseModifyAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

        // put traceId to response
        ((RestResponse) o).setTraceId(MDC.get(Constants.TRACE_ID));

        return o;
    }
}

Dubbo类传递

traceId的存储使用org.apache.dubbo.rpc.RpcContext(内部使用InternalThreadLocal实现)。

借助Dubbo的过滤器来实现,traceId在Dubbo服务间的读取、写入和清除。

import com.example.dubboservicea.constants.Constants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.MDC;


@Activate(group = {org.apache.dubbo.common.Constants.PROVIDER, org.apache.dubbo.common.Constants.CONSUMER})
public class DubboTraceIdFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        RpcContext rpcContext = RpcContext.getContext();

        // before
        if (rpcContext.isProviderSide()) {
            // get traceId from dubbo consumer,and set traceId to MDC
            String traceId = rpcContext.getAttachment(Constants.TRACE_ID);
            MDC.put(Constants.TRACE_ID, traceId);
        }

        Result result = invoker.invoke(invocation);

        // after
        if (rpcContext.isProviderSide()) {
            // clear traceId from MDC
            MDC.remove(Constants.TRACE_ID);
        }

        return result;
    }

    @Override
    public Result onResponse(Result result, Invoker<?> invoker, Invocation invocation) {
        return result;
    }
}

另,需要在resources/META-INF/dubbo/文件夹下,创建com.alibaba.dubbo.rpc.Filter文本文件。内容为dubboTraceIdFilter=com.example.dubboservicea.filter.DubboTraceIdFilter

而后,spring-boot配置文件中 配置dubbo的过滤器

#dubbo
dubbo:
  scan:
    base-packages: com.example.dubboservicea.provider, com.example.dubboservicea.reference
  protocol:
    name: dubbo
    port: 12101
  registry:
    address: zookeeper://118.190.204.150:20084
  provider:
    filter: dubboTraceIdFilter
  consumer:
    filter: dubboTraceIdFilter

 

相关标签: dubbo springboot