Commit fe956b71f64cd75aef1f791c68d0f7f4bbf600b9

Authored by dark
1 parent fe0018b3

拦截器从spring mvc 改为servlet

src/main/java/cn/iocoder/dashboard/framework/tracer/filter/SpringMvcTraceFilter.java renamed to src/main/java/cn/iocoder/dashboard/framework/tracer/filter/ServletTraceFilter.java
@@ -3,16 +3,10 @@ package cn.iocoder.dashboard.framework.tracer.filter; @@ -3,16 +3,10 @@ package cn.iocoder.dashboard.framework.tracer.filter;
3 import cn.hutool.core.util.StrUtil; 3 import cn.hutool.core.util.StrUtil;
4 import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils; 4 import cn.iocoder.dashboard.framework.tracer.core.util.TracerUtils;
5 import lombok.extern.slf4j.Slf4j; 5 import lombok.extern.slf4j.Slf4j;
6 -import org.aspectj.lang.annotation.Aspect;  
7 -import org.aspectj.lang.annotation.Before;  
8 -import org.aspectj.lang.annotation.Pointcut;  
9 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.beans.factory.annotation.Value;
10 -import org.springframework.context.annotation.Configuration;  
11 import org.springframework.lang.Nullable; 7 import org.springframework.lang.Nullable;
12 import org.springframework.stereotype.Component; 8 import org.springframework.stereotype.Component;
13 -import org.springframework.web.servlet.AsyncHandlerInterceptor;  
14 import org.springframework.web.servlet.HandlerInterceptor; 9 import org.springframework.web.servlet.HandlerInterceptor;
15 -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;  
16 10
17 import javax.servlet.*; 11 import javax.servlet.*;
18 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletRequest;
@@ -27,42 +21,44 @@ import java.io.IOException; @@ -27,42 +21,44 @@ import java.io.IOException;
27 21
28 @Slf4j 22 @Slf4j
29 @Component 23 @Component
30 -public class SpringMvcTraceFilter implements HandlerInterceptor { 24 +public class ServletTraceFilter implements Filter {
31 25
32 @Value("${cn.iocoder.tracer.name:global-trace-id}") 26 @Value("${cn.iocoder.tracer.name:global-trace-id}")
33 private String traceIdName; 27 private String traceIdName;
34 28
35 - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
36 - // 请求中traceId  
37 - String reqTraceId = (String)request.getAttribute(traceIdName);  
38 - // skywalking中的traceId  
39 - String skywalkingTraceId = TracerUtils.getSkywalkingTraceId();  
40 - String traceId ;  
41 - if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) {  
42 - // 两者皆空,添加默认的.  
43 - traceId = TracerUtils.getTraceId();  
44 - request.setAttribute(traceIdName, traceId);  
45 - } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){  
46 - // 若请求空,则添加,为没有skywalking的系统添加一个TraceId  
47 - traceId = skywalkingTraceId;  
48 - request.setAttribute(traceIdName, traceId);  
49 - } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) {  
50 - // 请求非空, skywalking为空  
51 - traceId = reqTraceId;  
52 - } else {  
53 - // 两者皆非空,不动请求头  
54 - traceId = skywalkingTraceId;  
55 - }  
56 - TracerUtils.saveThreadTraceId(traceId);  
57 - log.debug("请求进入,添加traceId[{}]", traceId);  
58 - return true;  
59 - }  
60 -  
61 - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {  
62 - // 请求结束,删除本地的链路流水号  
63 - log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId());  
64 - TracerUtils.deleteThreadTraceId(); 29 + @Override
  30 + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  31 + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
  32 + HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
  33 + try {
  34 + // 请求中traceId
  35 + String reqTraceId = (String)httpServletRequest.getHeader(traceIdName);
  36 + // skywalking中的traceId
  37 + String skywalkingTraceId = TracerUtils.getSkywalkingTraceId();
  38 + String traceId ;
  39 + if (null == reqTraceId && StrUtil.isBlank(skywalkingTraceId)) {
  40 + // 两者皆空,添加默认的.
  41 + traceId = TracerUtils.getTraceId();
  42 + httpServletResponse.setHeader(traceIdName, traceId);
  43 + } else if (null == reqTraceId && StrUtil.isNotBlank(skywalkingTraceId)){
  44 + // 若请求空,则添加,为没有skywalking的系统添加一个TraceId
  45 + traceId = skywalkingTraceId;
  46 + httpServletResponse.setHeader(traceIdName, traceId);
  47 + } else if (null != reqTraceId && StrUtil.isBlank(skywalkingTraceId)) {
  48 + // 请求非空, skywalking为空
  49 + traceId = reqTraceId;
  50 + } else {
  51 + // 两者皆非空,不动请求头
  52 + traceId = skywalkingTraceId;
  53 + }
  54 + TracerUtils.saveThreadTraceId(traceId);
  55 + log.debug("请求进入,添加traceId[{}]", traceId);
65 56
  57 + filterChain.doFilter(httpServletRequest, httpServletResponse);
  58 + } finally {
  59 + // 请求结束,删除本地的链路流水号
  60 + log.debug("请求结束,删除traceId[{}]", TracerUtils.getTraceId());
  61 + TracerUtils.deleteThreadTraceId();
  62 + }
66 } 63 }
67 -  
68 } 64 }
src/main/java/cn/iocoder/dashboard/framework/tracer/filter/WebConfig.java deleted
1 -package cn.iocoder.dashboard.framework.tracer.filter;  
2 -  
3 -import org.springframework.beans.factory.annotation.Autowired;  
4 -import org.springframework.context.annotation.Configuration;  
5 -import org.springframework.stereotype.Component;  
6 -import org.springframework.web.servlet.config.annotation.InterceptorRegistry;  
7 -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;  
8 -  
9 -import javax.annotation.Resource;  
10 -  
11 -@Configuration  
12 -@Component  
13 -public class WebConfig implements WebMvcConfigurer {  
14 -  
15 - @Resource  
16 - private SpringMvcTraceFilter springMvcTraceFilter;  
17 -  
18 - @Override  
19 - public void addInterceptors(InterceptorRegistry registry) {  
20 - registry.addInterceptor(this.springMvcTraceFilter).addPathPatterns("/**");  
21 - }  
22 -  
23 -}  
24 \ No newline at end of file 0 \ No newline at end of file