终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?
继之前我在《https://www.jianshu.com/p/28cc667263bb》一文中抛出的问题 , MDC无法很好地实现多线程之间的值传递问题 , 我自己私下也是抽空去找了 , 目前找到一个初次尝试 , 效果还不错的方案 , 下面给大家介绍一下吧 。 
文章图片
目录
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);}});}});}}}
文章图片
启动成功
文章图片
日志输出
文章图片
两次对比
OK , 是不是很nice , 感兴趣的道友 , 可以去看看它的底层源码哦 。
【终于找到多线程间日志唯一链路ID的解决方案了,你晓得了嘛?】好了 , 若觉得此文章还不错 , 记得点赞评论转发哦!!!
- 户外|“小眼镜”增多 专家支招教你科学用眼
- 苹果|美科技巨头每秒能赚多少钱?苹果11376元
- iqoo|iQOO Z6x终于量产,6000mAh大电池、天玑8100+独显齐上阵
- 微信更新正式版!“清理缓存”功能变强,小程序终于能分享到朋友圈
- 英特尔|两倍变焦自拍能有多大区别?苹果、三星、OPPO自拍样张解析
- 苹果|苹果多款新品已提上日程
- 拼多多|拼多多推出PC网页版商城,或只为缓解增长焦虑
- 宇宙中有多少维度,人类为何想要进入四维空间?科学家给出了解释
- 湖北|巅峰摩擦?i9 12950HX与R9 6900HX专业应用差距有多大?
- 拼多多|4P营销新理论
