项目场景:
有时候一个业务调用链场景,很长,调了各种各样的方法,看日志的时候,各个接口的日志穿插,确实让人头大。为了解决这个痛点,就使用了TraceId,根据TraceId关键字进入服务器查询日志中是否有这个TraceId,这样就把同一次的业务调用链上的日志串起来了。
实现步骤
1、pom.xml 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!--lombok配置-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
2、整合logback,打印日志,logback-spring.xml (简单配置下)
关键代码:[traceId:%X{traceId}],traceId是通过拦截器里MDC.put(traceId, tid)添加
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!--日志存储路径-->
<property name="log" value="D:/test/log" />
<!-- 控制台输出 -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--输出格式化-->
<pattern>[traceId:%X{traceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</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}.log</FileNamePattern>
<!--保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[traceId:%X{traceId}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
<!--日志文件最大的大小-->
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="file" />
</root>
</configuration>
3、application.yml
server:
port: 8826
logging:
config: classpath:logback-spring.xml
4、自定义日志拦截器 LogInterceptor.java
用途:每一次链路,线程维度,添加最终的链路ID traceId。
MDC(Mapped Diagnostic Context)诊断上下文映射,是@Slf4j提供的一个支持动态打印日志信息的工具。
import org.slf4j.MDC;
import org.springframework.lang.Nullable;
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;
/**
* 日志拦截器
*/
public class LogInterceptor implements HandlerInterceptor {
private static final String traceId = "traceId";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String tid = UUID.randomUUID().toString().replace("-", "");
//可以考虑让客户端传入链路ID,但需保证一定的复杂度唯一性;如果没使用默认UUID自动生成
if (!StringUtils.isEmpty(request.getHeader("traceId"))){
tid=request.getHeader("traceId");
}
MDC.put(traceId, tid);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
@Nullable Exception ex) {
// 请求处理完成后,清除MDC中的traceId,以免造成内存泄漏
MDC.remove(traceId);
}
}
5、WebConfigurerAdapter.java 添加拦截器
ps: 其实这个拦截的部分改为使用自定义注解+aop也是很灵活的。
import javax.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebConfigurerAdapter extends WebMvcConfigurationSupport {
@Resource
private LogInterceptor logInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor);
//可以具体制定哪些需要拦截,哪些不拦截,其实也可以使用自定义注解更灵活完成
// .addPathPatterns("/**")
// .excludePathPatterns("/testxx.html");
}
}
6、测试接口
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Api(tags = "测试接口")
@RequestMapping("/test")
@Slf4j
public class TestController {
@RequestMapping(value = "/log", method = RequestMethod.GET)
@ApiOperation(value = "测试日志")
public String sign() {
log.info("这是一行info日志");
log.error("这是一行error日志");
return "success";
}
}
结果:
异步场景:
使用线程的场景,写一个异步线程,加入这个调用里面。再次执行看开效果,我们会发现显然子线程丢失了trackId。 所以我们需要针对子线程使用情形,做调整,思路:将父线程的trackId传递下去给子线程即可。
1、 ThreadMdcUtil.java
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 traceId = "traceId";
// 获取唯一性标识
public static String generateTraceId() {
return UUID.randomUUID().toString().replace("-", "");
}
public static void setTraceIdIfAbsent() {
if (MDC.get(traceId) == null) {
MDC.put(traceId, generateTraceId());
}
}
/**
* 用于父线程向线程池中提交任务时,将自身MDC中的数据复制给子线程
*
* @param callable
* @param context
* @param <T>
* @return
*/
public static <T> Callable<T> wrap(final Callable<T> callable, final Map<String, String> context) {
return () -> {
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();
}
};
}
}
2、 MyThreadPoolTaskExecutor.java 是我们自己写的,重写了一些方法
import org.slf4j.MDC;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
public final 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()));
}
}
3、 ThreadPoolConfig.java 定义线程池,交给spring管理
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import java.util.concurrent.Executor;
@EnableAsync
@Configuration
public class ThreadPoolConfig {
/**
* 声明一个线程池
*/
@Bean("taskExecutor")
public Executor taskExecutor() {
MyThreadPoolTaskExecutor executor = new MyThreadPoolTaskExecutor();
//核心线程数5:线程池创建时候初始化的线程数
executor.setCorePoolSize(5);
//最大线程数5:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
executor.setMaxPoolSize(5);
//缓冲队列500:用来缓冲执行任务的队列
executor.setQueueCapacity(500);
//允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
executor.setKeepAliveSeconds(60);
//线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
executor.setThreadNamePrefix("taskExecutor-");
executor.initialize();
return executor;
}
}
4、 Service
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* 测试Service
*/
@Service("testService")
@Slf4j
public class TestService {
/**
* 异步操作测试
*/
@Async("taskExecutor")
public void asyncTest() {
try {
log.info("模拟异步开始......");
Thread.sleep(3000);
log.info("模拟异步结束......");
} catch (InterruptedException e) {
log.error("异步操作出错:"+e);
}
}
}
5、测试接口
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Api(tags = "测试接口")
@RequestMapping("/test")
@Slf4j
public class TestController {
@Resource
private TestService testService;
@RequestMapping(value = "/log", method = RequestMethod.GET)
@ApiOperation(value = "测试日志")
public String sign() {
log.info("这是一行info日志");
log.error("这是一行error日志");
//异步操作测试
testService.asyncTest();
return "success";
}
}
结果:
我们可以看到,子线程的日志也被串起来了。
定时任务:
如果使用了定时任务@Scheduled,这时候执行定时任务,不会走上面的拦截器逻辑,所以定时任务需要单独创建个AOP切面。
1、创建个定时任务线程池
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import java.util.concurrent.Executors;
/**
* 定时任务线程池
*/
@EnableScheduling
@Configuration
public class SeheduleConfig implements SchedulingConfigurer{
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
}
}
2、创建个AOP切面
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
import java.util.UUID;
@Aspect //定义一个切面
@Configuration
public class SeheduleTaskAspect {
// 定义定时任务切点Pointcut
@Pointcut("@annotation(org.springframework.scheduling.annotation.Scheduled)")
public void seheduleTask() {
}
@Around("seheduleTask()")
public void doAround(ProceedingJoinPoint joinPoint) throws Throwable {
try {
String traceId = UUID.randomUUID().toString().replace("-", "");
//用于日志链路追踪,logback配置:%X{traceId}
MDC.put("traceId", traceId);
//执行定时任务方法
joinPoint.proceed();
} finally {
//请求处理完成后,清除MDC中的traceId,以免造成内存泄漏
MDC.remove("traceId");
}
}
}
3、创建定时任务测试
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class SeheduleTasks {
private Logger logger = LoggerFactory.getLogger(SeheduleTasks.class);
/**
* 1分钟执行一次
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void testTask() {
logger.info("执行定时任务>"+new Date());
}
}
总结:
服务启动的时候traceId是空的,这是正常的,因为还没到拦截器这一层。
源码:https://download.csdn.net/download/u011974797/89981672
API 说明
clear()
=> 移除所有 MDCget (String key)
=> 获取当前线程 MDC 中指定 key 的值getContext()
=> 获取当前线程 MDC 的 MDCput(String key, Object o)
=> 往当前线程的 MDC 中存入指定的键值对remove(String key)
=> 删除当前线程 MDC 中指定的键值对
版权归原作者 涛哥是个大帅比 所有, 如有侵权,请联系我们删除。