终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?

继之前我在《https://www.jianshu.com/p/28cc667263bb》一文中抛出的问题 , MDC无法很好地实现多线程之间的值传递问题 , 我自己私下也是抽空去找了 , 目前找到一个初次尝试 , 效果还不错的方案 , 下面给大家介绍一下吧 。
终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?
文章图片
目录
2、引入依赖org.springframework.bootspring-boot-starter-webcom.yomahubtlog-all-spring-boot-starter1.3.1
%d{yyyy-MM-ddHH:mm:ss.SSS}[%thread]%-5level%logger{50}-%msg%n
importlombok.extern.slf4j.Slf4j;importorg.springframework.stereotype.Service;@Slf4j@ServicepublicclassTestService{publicvoidtest(intn){log.info("service输出内容:{}",n);}}
importcom.example.spring_hibernate.service.TestService;importlombok.extern.slf4j.Slf4j;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.ScheduledThreadPoolExecutor;@RestController@Slf4jpublicclassTestThread{@AutowiredprivateTestServicetestService;@GetMapping(value="https://pcff.toutiao.jxnews.com.cn/hello")publicvoidget{//构建两个线程池ExecutorServiceexecutorService=newScheduledThreadPoolExecutor(4);ExecutorServiceexecutorService2=newScheduledThreadPoolExecutor(2);for(inti=0;i<10;i++){executorService.submit(newRunnable{@Overridepublicvoidrun{try{Thread.sleep(200);}catch(InterruptedExceptione){e.printStackTrace;}log.info(Thread.currentThread.getName+">>线程名>>"+System.currentTimeMillis);//启动线程池2executorService2.submit(newRunnable{@Overridepublicvoidrun{log.info("线程池2》》"+Thread.currentThread.getName);//调用service方法testService.test(4);}});}});}}}
终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?
文章图片
启动成功
终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?
文章图片
日志输出
终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?
文章图片
两次对比
OK , 是不是很nice , 感兴趣的道友 , 可以去看看它的底层源码哦 。
终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?】好了 , 若觉得此文章还不错 , 记得点赞评论转发哦!!!