From fe956b71f64cd75aef1f791c68d0f7f4bbf600b9 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 22:46:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E4=BB=8Espring=20mv?= =?UTF-8?q?c=20=E6=94=B9=E4=B8=BAservlet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tracer/filter/ServletTraceFilter.java | 64 +++++++++++++++++ .../tracer/filter/SpringMvcTraceFilter.java | 68 ------------------- .../framework/tracer/filter/WebConfig.java | 23 ------- 3 files changed, 64 insertions(+), 91 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java delete mode 100644 src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java new file mode 100644 index 000000000..dc3c1b03b --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java @@ -0,0 +1,64 @@ +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(); + } + } +} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java deleted file mode 100644 index 76fc93eb4..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java +++ /dev/null @@ -1,68 +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.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Before; -import org.aspectj.lang.annotation.Pointcut; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.lang.Nullable; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.AsyncHandlerInterceptor; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; - -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 SpringMvcTraceFilter implements HandlerInterceptor { - - @Value("${cn.iocoder.tracer.name:global-trace-id}") - private String traceIdName; - - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - // 请求中traceId - String reqTraceId = (String)request.getAttribute(traceIdName); - // skywalking中的traceId - String skywalkingTraceId = TracerUtils.getSkywalkingTraceId(); - String traceId ; - if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 两者皆空,添加默认的. - traceId = TracerUtils.getTraceId(); - request.setAttribute(traceIdName, traceId); - } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){ - // 若请求空,则添加,为没有skywalking的系统添加一个TraceId - traceId = skywalkingTraceId; - request.setAttribute(traceIdName, traceId); - } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) { - // 请求非空, skywalking为空 - traceId = reqTraceId; - } else { - // 两者皆非空,不动请求头 - traceId = skywalkingTraceId; - } - TracerUtils.saveThreadTraceId(traceId); - log.debug("请求进入,添加traceId[{}]", traceId); - return true; - } - - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { - // 请求结束,删除本地的链路流水号 - log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId()); - TracerUtils.deleteThreadTraceId(); - - } - -} diff --git a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java b/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java deleted file mode 100644 index 4dddc0803..000000000 --- a/src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java +++ /dev/null @@ -1,23 +0,0 @@ -package cn.iocoder.dashboard.framework.tracer.filter; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import javax.annotation.Resource; - -@Configuration -@Component -public class WebConfig implements WebMvcConfigurer { - - @Resource - private SpringMvcTraceFilter springMvcTraceFilter; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(this.springMvcTraceFilter).addPathPatterns("/**"); - } - -} \ No newline at end of file