MDC(Mapped Diagnostic Context,映射调试上下文)是 log4j 、logback及log4j2 提供的一种方便在多线程条件下记录日志的功能。MDC 可以看成是一个与当前线程绑定的哈希表,可以往其中添加键值对。MDC 中包含的内容可以被同一线程中执行的代码所访问。当前线程的子线程会继承其父线程中的 MDC 的内容。当需要记录日志时,只需要从 MDC 中获取所需的信息即可。MDC 的内容则由程序在适当的时候保存进去。对于一个 Web 应用来说,通常是在请求被处理的最开始保存这些数据
MDC 全称是 Mapped Diagnostic Context,可以粗略的理解成是一个线程安全的存放诊断日志的容器。
一般是结合log4j一起使用,为我们的日志根据线程链路加一个表示traceId,在微服务盛行的当下,链路跟踪是个难题,而借助 MDC 去埋点,巧妙实现链路跟踪应该不是问题
1.logback.xml
<configuration debug="false">
<property name="log" value="D://Xiangmu//TrackMeta//src//main//resources//log" />
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%X{TRACE_ID}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
encoder>
appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log}/%d{yyyy-MM-dd}.logFileNamePattern>
<MaxHistory>30MaxHistory>
rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%X{TRACE_ID}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
encoder>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MBMaxFileSize>
triggeringPolicy>
appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="file" />
root>
configuration>
2.所需依赖
<dependencies>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-coreartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-accessartifactId>
<version>1.2.3version>
dependency>
<dependency>
<groupId>ch.qos.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
dependencies>
3.yml
这里需要注意Swagger2和SpringBoot2的依赖问题有版本冲突,需要指定匹配策略
server:
port: 8826
logging:
config: classpath:logback-spring.xml
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher # 解决SpringBoot 和Swagger2版本冲突
4.自定义的日志拦截器
根据用户请求进行前置拦截,判断用户的请求头中是否含有TRACE_ID,如果有的话进行(当然不可能有),没有的话我们进行设置并且赋值(用UUID生成一个唯一序列放到TRACE_ID中),然后放到MDC中
后置处理器中,我们从MDC移除TRACE_ID
package com.wyh.trackmeta.interceptor;
import org.slf4j.MDC;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
//自定义日志拦截器:每一次链路,线程维度,添加最终的链路ID :Trace_ID
public class LogInterceptor implements HandlerInterceptor {
private static final String TRACE_ID = "TRACE_ID";
/**
* 1.前置拦截器
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String tid = UUID.randomUUID().toString().replace("-", "");
//1.这里我们是让客户端传入链路ID,然后进行前置拦截捕获
if(!StringUtils.isEmpty(request.getHeader("TRACE_ID"))){
tid=request.getHeader("TRACE_ID");
}
//2.利用MDC将请求的上下文信息存储到当前线程的上下文映射中
MDC.put(TRACE_ID,tid);
return true;
}
/**
* 2.后置处理器
* @param request
* @param response
* @param handler
* @param ex
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
MDC.remove(TRACE_ID);
}
}
注册拦截器
package com.wyh.trackmeta.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfigurerAdapter implements WebMvcConfigurer {
//1.注册日志拦截器
@Bean
public LogInterceptor logInterceptor() {
return new LogInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor());
}
}
4.多线程下MDC工具类
1.定义方法,将父线程向线程池提交任务的时候,将自身MDC中的数据复制给子线程——>存放上下文判断是否为null,不为空就将数据context放到MDC中,然后设置TraceID——>然后执行任务
总的来说就是对任务进行了一次封装
package com.wyh.trackmeta.config;
import org.slf4j.MDC;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
/**
* @Author: JCccc
* @Date: 2022-5-30 11:14
* @Description:
*/
public final class ThreadMdcUtil {
private static final String TRACE_ID = "TRACE_ID";
// 获取唯一性标识
public static String generateTraceId() {
return UUID.randomUUID().toString();
}
public static void setTraceIdIfAbsent() {
if (MDC.get(TRACE_ID) == null) {
MDC.put(TRACE_ID, generateTraceId());
}
}
/**
* 用于父线程向线程池中提交任务时,将自身MDC中的数据复制给子线程
*
* @param callable
* @param context
* @param
* @return
*/
public static <T> Callable<T> wrap(final Callable<T> callable, final Map<String, String> context) {
return new Callable<T>() {
@Override
public T call() throws Exception {
if (context == null) {
MDC.clear();
} else {
MDC.setContextMap(context);
}
setTraceIdIfAbsent();
try {
return callable.call();
} finally {
MDC.clear();
}
}
};
}
/**
* 用于父线程向线程池中提交任务时,将自身MDC中的数据复制给子线程
*
* @param runnable
* @param context
* @return
*/
public static Runnable wrap(final Runnable runnable, final Map<String, String> context) {
return () -> {
if (context == null) {
MDC.clear();
} else {
MDC.setContextMap(context);
}
setTraceIdIfAbsent();
try {
runnable.run();
} finally {
MDC.clear();
}
};
}
}
5.自定义线程池
**为什么要自定义线程池呢?**因为子线程在打印日志的过程会造成traceId丢失,解决方式就是重写线程池
package com.wyh.trackmeta.config;
import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
/**
* @Author: Fairy
* @Description:
*/
public class MyThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
public MyThreadPoolTaskExecutor() {
super();
}
@Override
public void execute(Runnable task) {
super.execute(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
}
@Override
public <T> Future<T> submit(Callable<T> task) {
return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
}
@Override
public Future<?> submit(Runnable task) {
return super.submit(ThreadMdcUtil.wrap(task, MDC.getCopyOfContextMap()));
}
}
6.线程池配置类
package com.wyh.trackmeta.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import java.util.concurrent.Executor;
@Configuration
@EnableAsync
public class ThreadPoolConfig {
public static void main(String[] args) {
Thread thread = new Thread();
}
/**
* 声明一个线程池
* @return
*/
@Bean("MyExecutor")
public Executor asyncExecutor() {
MyThreadPoolTaskExecutor executor = new MyThreadPoolTaskExecutor();
//核心线程数5:线程池创建时候初始化的线程数
executor.setCorePoolSize(5);
//最大线程数5:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
executor.setMaxPoolSize(10);
//缓冲队列500:用来缓冲执行任务的队列
executor.setQueueCapacity(500);
//允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
executor.setKeepAliveSeconds(60);
//线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
executor.setThreadNamePrefix("asyncJCccc");
executor.initialize();
return executor;
}
}
7.测试接口
package com.wyh.trackmeta.controller;
import com.wyh.trackmeta.service.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "链路日志测试")
@RestController
@Slf4j
public class TestController {
@Autowired
private UserServiceImpl userService;
@SneakyThrows
@ApiOperation("日志测试Test接口")
@RequestMapping("doTest")
public String doTest(@RequestParam("name")String name){
log.info("入参 name={}",name);
testTrace();
userService.insertUser();
log.info("调用结束 name={}",name);
return "Hello,"+name;
}
/**
* 2.日志方法
*/
private void testTrace(){
log.info("这是一行info日志");
log.info("这是一行error日志");
testTrace2();
}
private void testTrace2() {
log.info("这也是一行日志");
}
}
8.异步的业务类
在执行的任务方法上标注注解@Async(“线程池”)
package com.wyh.trackmeta.service;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import static java.lang.Thread.sleep;
@Slf4j
@Service
public class UserServiceImpl {
@SneakyThrows
@Async("MyExecutor")
public void insertUser() throws InterruptedException {
sleep(2000);
log.info("正在插入数据...");
}
}