From 5b181727948707fd8bc6cedf33cd1a24b8a31722 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 21:06:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8FastThreadLocal=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2Map=E6=9D=A5=E7=BB=B4=E6=8A=A4traceId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/tracer/core/util/TracerUtils.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java index a7cd841da..2d3b47068 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.dashboard.framework.tracer.core.ITrace; +import io.netty.util.concurrent.FastThreadLocal; import org.apache.skywalking.apm.toolkit.trace.TraceContext; import java.util.Map; @@ -19,7 +20,7 @@ public class TracerUtils { /** * 维护请求线程对应的TraceId */ - private final static Map threadTraceIdMap = new ConcurrentHashMap<>(); + private static FastThreadLocal traceIdMap = new FastThreadLocal<>(); /** * 保存链路流水号 @@ -27,7 +28,7 @@ public class TracerUtils { * @param traceId 链路流水号 */ public static void saveThreadTraceId(String traceId) { - threadTraceIdMap.put(Thread.currentThread(), traceId); + traceIdMap.set(traceId); } /** @@ -36,14 +37,14 @@ public class TracerUtils { * @return 链路流水号 */ public static String getThreadTraceId() { - return threadTraceIdMap.get(Thread.currentThread()); + return traceIdMap.get(); } /** * 根据线程删除链路流水 */ public static void deleteThreadTraceId() { - threadTraceIdMap.remove(Thread.currentThread()); + traceIdMap.remove(); } /** @@ -75,7 +76,7 @@ public class TracerUtils { } catch (Throwable ignore) { } // 尝试从map中获取 - traceId = threadTraceIdMap.get(Thread.currentThread()); + traceId = traceIdMap.get(); if (StrUtil.isNotBlank(traceId)) { return traceId; }