不再产生默认流水号,以skywalking为准,删除多余的拦截器

pull/2/head
tinyWolf 2021-04-09 20:41:54 +08:00
parent 3d2961fabf
commit d16ca426b5
3 changed files with 1 additions and 173 deletions

View File

@ -1,16 +0,0 @@
package cn.iocoder.dashboard.framework.tracer.core;
/**
* traceId,Spring bean.
*
* @author
*/
public interface ITrace {
/**
* traceId
*
* @return traceId
*/
String getTraceId();
}

View File

@ -1,9 +1,5 @@
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.UUID;
@ -15,36 +11,6 @@ import java.util.UUID;
*/
public class TracerUtils {
/**
* 线TraceId
*/
private final static FastThreadLocal<String> traceIdMap = new FastThreadLocal<>();
/**
*
*
* @param traceId
*/
public static void saveThreadTraceId(String traceId) {
traceIdMap.set(traceId);
}
/**
* 线
*
* @return
*/
public static String getThreadTraceId() {
return traceIdMap.get();
}
/**
* 线
*/
public static void deleteThreadTraceId() {
traceIdMap.remove();
}
/**
*
*/
@ -54,71 +20,13 @@ public class TracerUtils {
/**
*
* <p>
* 访logger
* skywalking TraceId ""
* <p>
* 使 Apache SkyWalking traceId Skywalking 使 UUID
* : skywalking - > map -> -> default
* traceId skywalking > >
* maptraceId .
*
* @return
*/
public static String getTraceId() {
String traceId;
// 通过 SkyWalking 获取链路编号
try {
traceId = TraceContext.traceId();
if (StrUtil.isNotBlank(traceId)) {
return traceId;
}
} catch (Throwable ignore) {
}
// 尝试从map中获取
traceId = traceIdMap.get();
if (StrUtil.isNotBlank(traceId)) {
return traceId;
}
// 通过自定义扩展的tracer产生traceId, 在Spring容器加载完成前会获取不到对应的Bean
ITrace tracer = null;
try {
tracer = getTracer();
} catch (Throwable ignore) {
}
if (null != tracer) {
try {
return tracer.getTraceId();
} catch (Throwable ignored) {
}
}
// TODO 芋艿 多次调用会问题
return defaultTraceId();
}
/**
* Skywalking traceId, skywalking,""
*
* @return skywalking traceId
*/
public static String getSkywalkingTraceId() {
return TraceContext.traceId();
}
/**
* Spring ITrace ,null
*
* @return ITrace
*/
private static ITrace getTracer() {
return SpringUtil.getBean(ITrace.class);
}
/**
* TraceIdUUID
*
* @return UUID
*/
private static String defaultTraceId() {
return "UUID:" + UUID.randomUUID().toString();
}
}

View File

@ -1,64 +0,0 @@
package cn.iocoder.dashboard.framework.tracer.filter;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Spring Mvc , traceId.
*
* @author mashu
*/
@Slf4j
@Component
public class ServletTraceFilter implements Filter {
@Value("${cn.iocoder.tracer.name:global-trace-id}")
private String traceIdName;
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
try {
// 请求中traceId
String reqTraceId = (String)httpServletRequest.getHeader(traceIdName);
// skywalking中的traceId
String skywalkingTraceId = TracerUtils.getSkywalkingTraceId();
String traceId ;
if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) {
// 两者皆空,添加默认的.
traceId = TracerUtils.getTraceId();
httpServletResponse.setHeader(traceIdName, traceId);
} else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){
// 若请求空,则添加,为没有skywalking的系统添加一个TraceId
traceId = skywalkingTraceId;
httpServletResponse.setHeader(traceIdName, traceId);
} else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) {
// 请求非空, skywalking为空
traceId = reqTraceId;
} else {
// 两者皆非空,不动请求头
traceId = skywalkingTraceId;
}
TracerUtils.saveThreadTraceId(traceId);
log.debug("请求进入,添加traceId[{}]", traceId);
filterChain.doFilter(httpServletRequest, httpServletResponse);
} finally {
// 请求结束,删除本地的链路流水号
log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId());
TracerUtils.deleteThreadTraceId();
}
}
}