使用FastThreadLocal替换Map来维护traceId

pull/2/head
dark 2021-03-18 21:06:36 +08:00
parent f0a633373e
commit 5b18172794
1 changed files with 6 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.tracer.core.util;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.dashboard.framework.tracer.core.ITrace; import cn.iocoder.dashboard.framework.tracer.core.ITrace;
import io.netty.util.concurrent.FastThreadLocal;
import org.apache.skywalking.apm.toolkit.trace.TraceContext; import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import java.util.Map; import java.util.Map;
@ -19,7 +20,7 @@ public class TracerUtils {
/** /**
* 线TraceId * 线TraceId
*/ */
private final static Map<Thread, String> threadTraceIdMap = new ConcurrentHashMap<>(); private static FastThreadLocal<String> traceIdMap = new FastThreadLocal<>();
/** /**
* *
@ -27,7 +28,7 @@ public class TracerUtils {
* @param traceId * @param traceId
*/ */
public static void saveThreadTraceId(String traceId) { public static void saveThreadTraceId(String traceId) {
threadTraceIdMap.put(Thread.currentThread(), traceId); traceIdMap.set(traceId);
} }
/** /**
@ -36,14 +37,14 @@ public class TracerUtils {
* @return * @return
*/ */
public static String getThreadTraceId() { public static String getThreadTraceId() {
return threadTraceIdMap.get(Thread.currentThread()); return traceIdMap.get();
} }
/** /**
* 线 * 线
*/ */
public static void deleteThreadTraceId() { public static void deleteThreadTraceId() {
threadTraceIdMap.remove(Thread.currentThread()); traceIdMap.remove();
} }
/** /**
@ -75,7 +76,7 @@ public class TracerUtils {
} catch (Throwable ignore) { } catch (Throwable ignore) {
} }
// 尝试从map中获取 // 尝试从map中获取
traceId = threadTraceIdMap.get(Thread.currentThread()); traceId = traceIdMap.get();
if (StrUtil.isNotBlank(traceId)) { if (StrUtil.isNotBlank(traceId)) {
return traceId; return traceId;
} }