使用MDC实现日志链路跟踪

这篇文章主要介绍了使用MDC实现日志链路跟踪,在微服务环境中,我们经常使用Skywalking、CAT等去实现整体请求链路的追踪,但是这个整体运维成本高,架构复杂,我们来使用MDC通过Log来实现一个轻量级的会话事务跟踪功能,下面就来看看具体的过程吧,需要的朋友可以参考一下

前言:

在微服务环境中,我们经常使用Skywalking、CAT等去实现整体请求链路的追踪,但是这个整体运维成本高,架构复杂,我们来使用MDC通过Log来实现一个轻量级的会话事务跟踪功能。

1.原理

MDC org.sl4j.MDC其实内部就是ThreadLocal,MDC提供了put/get/clear等几个核心接口,用于操作ThreadLocal中的数据;ThreadLocal中的K-V,可以在logback.xml中声明,最终将会打印在日志中。

// java代码 MDC.put("userId","laker");   // logback.xml %X{userId}

例如:

2.实现

整体流程如下:

  • 用户登录系统,我们日志中记录userId:laker
  • 用户发起请求,一个请求中可能实际产生多个http请求,这里可以前端生成一个requestId
  • 在返回体中,返回requestId
  • 研发运维人员,可以根据 userIdrequestId去日志中捞请求链路。

3.过滤器

@Order(value = Ordered.HIGHEST_PRECEDENCE + 100) @Component @WebFilter(filterName = "MDCFilter", urlPatterns = "/*") public class MDCFilter extends OncePerRequestFilter {     @Override     protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {         try {             MDC.put("userId", "laker");             MDC.put("requestId", IdUtil.fastUUID());         } catch (Exception e) {             //         }         try {             filterChain.doFilter(httpServletRequest, httpServletResponse);         } finally {             MDC.clear();         }     } }

4.logback.xml

                         ${LOG_HOME}/test.log         true         false                      %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50} %line - %m%n                                                     ${LOG_HOME}/test.log.%d{yyyy-MM-dd}                          15                                                        ${log.pattern}                                         %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50} %line - %m%n                                                                  

5.返回体

public class Response {     @ApiModelProperty(notes = "响应码,非200 即为异常", example = "200")     private final int code;     @ApiModelProperty(notes = "响应消息", example = "提交成功")     private final String msg;     @ApiModelProperty(notes = "响应数据")     private final T data;     @ApiModelProperty(notes = "请求id")     private final String requestId;     public Response(int code, String msg, T data) {         this.code = code;         this.msg = msg;         this.data = data;         this.requestId = MDC.get("requestId");     }

6.效果日志

响应:

{     code: 200,     msg: "",     requestId: "74a269a8-3cb4-417e-853c-b968b77cce23" }

日志:

18:37:15.997 [http-nio-8080-exec-1] INFO  [laker|90717490-5ef4-4e46-bc2c-605952fc3803] c.l.m.c.InfoController - [v2Map,17] - null 18:37:38.980 [http-nio-8080-exec-2] INFO  [laker|82bde351-f86e-466f-97a0-c857a0c4c1c9] c.l.m.c.InfoController - [v2Map,17] - null 18:37:39.992 [http-nio-8080-exec-3] INFO  [laker|74a269a8-3cb4-417e-853c-b968b77cce23] c.l.m 

到此这篇关于使用MDC实现日志链路跟踪的文章就介绍到这了,更多相关MDC实现日志链路跟踪内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是使用MDC实现日志链路跟踪的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java