Commit 5b181727948707fd8bc6cedf33cd1a24b8a31722

Authored by dark
1 parent f0a63337

使用FastThreadLocal替换Map来维护traceId

src/main/java/cn/iocoder/dashboard/framework/tracer/core/util/TracerUtils.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util;
3 3 import cn.hutool.core.util.StrUtil;
4 4 import cn.hutool.extra.spring.SpringUtil;
5 5 import cn.iocoder.dashboard.framework.tracer.core.ITrace;
  6 +import io.netty.util.concurrent.FastThreadLocal;
6 7 import org.apache.skywalking.apm.toolkit.trace.TraceContext;
7 8  
8 9 import java.util.Map;
... ... @@ -19,7 +20,7 @@ public class TracerUtils {
19 20 /**
20 21 * 维护请求线程对应的TraceId
21 22 */
22   - private final static Map<Thread, String> threadTraceIdMap = new ConcurrentHashMap<>();
  23 + private static FastThreadLocal<String> traceIdMap = new FastThreadLocal<>();
23 24  
24 25 /**
25 26 * 保存链路流水号
... ... @@ -27,7 +28,7 @@ public class TracerUtils {
27 28 * @param traceId 链路流水号
28 29 */
29 30 public static void saveThreadTraceId(String traceId) {
30   - threadTraceIdMap.put(Thread.currentThread(), traceId);
  31 + traceIdMap.set(traceId);
31 32 }
32 33  
33 34 /**
... ... @@ -36,14 +37,14 @@ public class TracerUtils {
36 37 * @return 链路流水号
37 38 */
38 39 public static String getThreadTraceId() {
39   - return threadTraceIdMap.get(Thread.currentThread());
  40 + return traceIdMap.get();
40 41 }
41 42  
42 43 /**
43 44 * 根据线程删除链路流水
44 45 */
45 46 public static void deleteThreadTraceId() {
46   - threadTraceIdMap.remove(Thread.currentThread());
  47 + traceIdMap.remove();
47 48 }
48 49  
49 50 /**
... ... @@ -75,7 +76,7 @@ public class TracerUtils {
75 76 } catch (Throwable ignore) {
76 77 }
77 78 // 尝试从map中获取
78   - traceId = threadTraceIdMap.get(Thread.currentThread());
  79 + traceId = traceIdMap.get();
79 80 if (StrUtil.isNotBlank(traceId)) {
80 81 return traceId;
81 82 }
... ...