使用FastThreadLocal替换Map来维护traceId
parent
f0a633373e
commit
5b18172794
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue