MDC-链路追踪

文章目录

  • 1. 不引入MDC的日志打印
  • 2. 引入MDC的简单例子
  • 3. 使用AOP进行日志打印
  • MDC相关API

1. 不引入MDC的日志打印

public class MDCTestController {

    private static final Logger logger = LoggerFactory.getLogger(MDCTestController.class);

    private static final String TRACE_Id = "traceId";

    public static void main(String[] args) {
        MDC.put(TRACE_Id, UUID.randomUUID().toString());
        logger.info("开始调用服务,进行业务处理");
        logger.info("业务处理完毕,可以释放空间了,避免内存泄露");
        MDC.remove(TRACE_Id);
        logger.info("traceId 还存在吗?{}", MDC.get(TRACE_Id) != null);
    }
}

MDC-链路追踪_第1张图片

2. 引入MDC的简单例子

public class MDCTestController {

    private static final Logger logger = LoggerFactory.getLogger(MDCTestController.class);

    private static final String TRACE_Id = "traceId";

    public static void main(String[] args) {
        MDC.put(TRACE_Id, UUID.randomUUID().toString());
        logger.info("开始调用服务,进行业务处理");
        logger.info("业务处理完毕,可以释放空间了,避免内存泄露");
        MDC.remove(TRACE_Id);
        logger.info("traceId 还存在吗?{}", MDC.get(TRACE_Id) != null);
    }
}

logback.xml


<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>[%t] [%X{traceId}] - %m%nPattern>
        layout>
    appender>
    <root level="debug">
        <appender-ref ref="CONSOLE"/>
    root>
configuration>

MDC-链路追踪_第2张图片

3. 使用AOP进行日志打印

  1. logback.xml

<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>[%t] [%X{traceId}] - %m%nPattern>
        layout>
    appender>
    <root level="debug">
        <appender-ref ref="CONSOLE"/>
    root>
configuration>

  1. 自定义注解(用于切入点)
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MDCInterface {
}
  1. 切面类
@Aspect
@Component
@Slf4j
public class MDCAspect {

    private static final String TRACE_ID = "traceId";

    /**
     * 切入点
     */
    @Pointcut("@annotation(com.example.tooldemo.aspect.MDCInterface)")
    public void mdcPointCut(){}

    @Around("mdcPointCut()")
    public void process(ProceedingJoinPoint joinPoint){
        /**
         * 解析Log注解
         */
        try {
            MDC.put(TRACE_ID, UUID.randomUUID().toString());
            joinPoint.proceed();
        } catch (Throwable e) {
            log.error("MDC标识添加异常 ", e);
            throw new RuntimeException("MDC标识添加异常");
        } finally {
            MDC.clear();
        }
    }
}
  1. 在需要追踪的方法上添加注解
@Api(tags = "MDC测试")
@RestController
@Slf4j
public class MDCTestController {

    @Autowired
    MDCService mdcService;

    @ApiOperation(value="MDC-traceId测试")
    @GetMapping("/testMdc")
    @MDCInterface  //添加注解
    public void testMdc(){
        log.info("MDC测试——Controller开始执行");
        mdcService.mdcTest();
        log.info("MDC测试——Controller执行结束");
    }
}
public interface MDCService {

    void mdcTest();

}
@Slf4j
@Service
public class MDCServiceImpl implements MDCService {

    @Override
    public void mdcTest() {
        log.info("MDC测试——service");
    }
}

在这里插入图片描述

MDC相关API

  • clear(): 移除所有MDC
  • get (String key) : 获取当前线程MDC中指定key的值
  • getContext() : 获取当前线程MDC的MDC
  • put(String key, Object o) : 往当前线程的MDC中存入指定的键值对
  • remove(String key) : 删除当前线程MDC中指定的键值对

你可能感兴趣的:(java,开发语言)