diff --git a/rxlib/pom.xml b/rxlib/pom.xml index 11421130..aea9243f 100644 --- a/rxlib/pom.xml +++ b/rxlib/pom.xml @@ -31,7 +31,7 @@ 0.1.55 2.5.15 - 5.3.33 + 5.3.27 1.9.7 @@ -57,7 +57,7 @@ org.springframework - spring-web + spring-webmvc ${spring.version} diff --git a/rxlib/src/main/java/org/rx/annotation/EnableTrace.java b/rxlib/src/main/java/org/rx/annotation/EnableLog.java similarity index 88% rename from rxlib/src/main/java/org/rx/annotation/EnableTrace.java rename to rxlib/src/main/java/org/rx/annotation/EnableLog.java index e5574301..bb088b9a 100644 --- a/rxlib/src/main/java/org/rx/annotation/EnableTrace.java +++ b/rxlib/src/main/java/org/rx/annotation/EnableLog.java @@ -8,6 +8,6 @@ @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited -public @interface EnableTrace { +public @interface EnableLog { boolean doValidate() default false; } diff --git a/rxlib/src/main/java/org/rx/spring/BaseInterceptor.java b/rxlib/src/main/java/org/rx/spring/BaseInterceptor.java index 46a06c6d..b84108b8 100644 --- a/rxlib/src/main/java/org/rx/spring/BaseInterceptor.java +++ b/rxlib/src/main/java/org/rx/spring/BaseInterceptor.java @@ -2,11 +2,11 @@ import io.netty.util.concurrent.FastThreadLocal; import org.apache.commons.lang3.BooleanUtils; -import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.reflect.MethodSignature; -import org.rx.core.*; +import org.rx.core.Reflects; +import org.rx.core.Sys; import static org.rx.core.Extends.as; import static org.rx.core.Sys.*; @@ -15,28 +15,11 @@ public abstract class BaseInterceptor { static final FastThreadLocal idempotent = new FastThreadLocal<>(); protected CallLogBuilder logBuilder = Sys.DEFAULT_LOG_BUILDER; - protected final void enableTrace(String traceName) { - if (traceName == null) { - traceName = Constants.DEFAULT_TRACE_NAME; - } - RxConfig.ThreadPoolConfig conf = RxConfig.INSTANCE.getThreadPool(); - conf.setTraceName(traceName); - ThreadPool.onTraceIdChanged.first((s, e) -> logCtx(conf.getTraceName(), e.getValue())); - } - - protected String startTrace(JoinPoint joinPoint, String parentTraceId) { - return ThreadPool.startTrace(parentTraceId); - } - public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { if (BooleanUtils.isTrue(idempotent.get())) { return joinPoint.proceed(); } idempotent.set(Boolean.TRUE); - String tn = RxConfig.INSTANCE.getThreadPool().getTraceName(); - if (tn != null) { - logCtxIfAbsent(tn, startTrace(joinPoint, null)); - } try { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = as(signature, MethodSignature.class); @@ -53,7 +36,6 @@ public Object invoke() throws Throwable { } }, logBuilder, null); } finally { - ThreadPool.endTrace(); clearLogCtx(); idempotent.remove(); } diff --git a/rxlib/src/main/java/org/rx/spring/Interceptors.java b/rxlib/src/main/java/org/rx/spring/Interceptors.java index ab3282be..3e16f631 100644 --- a/rxlib/src/main/java/org/rx/spring/Interceptors.java +++ b/rxlib/src/main/java/org/rx/spring/Interceptors.java @@ -1,14 +1,13 @@ package org.rx.spring; import lombok.SneakyThrows; -import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.ConstructorSignature; import org.aspectj.lang.reflect.MethodSignature; -import org.rx.annotation.EnableTrace; +import org.rx.annotation.EnableLog; import org.rx.bean.Tuple; import org.rx.core.*; import org.rx.exception.ApplicationException; @@ -63,22 +62,6 @@ protected Object methodAround(ProceedingJoinPoint joinPoint, Tuple httpEnv = httpEnv(); - if (httpEnv == null) { - return super.startTrace(joinPoint, parentTraceId); - } - - String tn = RxConfig.INSTANCE.getThreadPool().getTraceName(); - if (parentTraceId == null) { - parentTraceId = httpEnv.left.getHeader(tn); - } - String tid = super.startTrace(joinPoint, parentTraceId); - httpEnv.right.setHeader(tn, tid); - return tid; - } - @Override public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { Tuple httpEnv = httpEnv(); @@ -149,12 +132,8 @@ public Object onException(Throwable e, HttpServletRequest request) { @Aspect @Component - public static class TraceInterceptor extends BaseInterceptor { - public void setTraceName(String traceName) { - super.enableTrace(traceName); - } - - @Around("@annotation(org.rx.annotation.EnableTrace) || @within(org.rx.annotation.EnableTrace)") + public static class LogInterceptor extends BaseInterceptor { + @Around("@annotation(org.rx.annotation.EnableLog) || @within(org.rx.annotation.EnableLog)") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { Signature signature = joinPoint.getSignature(); if (signature instanceof ConstructorSignature) { @@ -173,9 +152,9 @@ public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { } protected boolean doValidate(Executable r) { - EnableTrace a = r.getAnnotation(EnableTrace.class); + EnableLog a = r.getAnnotation(EnableLog.class); if (a == null) { - a = r.getDeclaringClass().getAnnotation(EnableTrace.class); + a = r.getDeclaringClass().getAnnotation(EnableLog.class); } return a != null && a.doValidate(); } diff --git a/rxlib/src/main/java/org/rx/spring/MxController.java b/rxlib/src/main/java/org/rx/spring/MxController.java index cf66349c..fa53c4ef 100644 --- a/rxlib/src/main/java/org/rx/spring/MxController.java +++ b/rxlib/src/main/java/org/rx/spring/MxController.java @@ -105,20 +105,6 @@ public Object health(HttpServletRequest request) { } } return target; -// case 4: -// Class ft = Class.forName(request.getParameter("ft")); -// String fn = request.getParameter("fn"); -// String fu = request.getParameter("fu"); -// Map, Map> fms = RxConfig.INSTANCE.getMxHttpForwards(); -// if (fu == null) { -// Map fts = fms.get(ft); -// if (fts != null) { -// fts.remove(fn); -// } -// } else { -// fms.computeIfAbsent(ft, k -> new ConcurrentHashMap<>(8)).put(fn, fu); -// } -// return fms; case 5: return Linq.from(InetAddress.getAllByName(request.getParameter("host"))).select(p -> p.getHostAddress()).toArray(); case 6: diff --git a/rxlib/src/main/java/org/rx/spring/MxWebConfig.java b/rxlib/src/main/java/org/rx/spring/MxWebConfig.java new file mode 100644 index 00000000..e1dfc9eb --- /dev/null +++ b/rxlib/src/main/java/org/rx/spring/MxWebConfig.java @@ -0,0 +1,59 @@ +package org.rx.spring; + +import lombok.RequiredArgsConstructor; +import org.rx.core.Constants; +import org.rx.core.RxConfig; +import org.rx.core.Strings; +import org.rx.core.ThreadPool; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import static org.rx.core.Sys.logCtx; + +@RequiredArgsConstructor +@Configuration +public class MxWebConfig implements WebMvcConfigurer { + @Component + public static class WebTracer implements HandlerInterceptor { + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + RxConfig.ThreadPoolConfig conf = RxConfig.INSTANCE.getThreadPool(); + String traceName = conf.getTraceName(); + if (Strings.isEmpty(traceName)) { + return true; + } + + String parentTraceId = request.getHeader(traceName); + String traceId = ThreadPool.startTrace(parentTraceId); + response.setHeader(traceName, traceId); + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + ThreadPool.endTrace(); + } + } + + public static void enableTrace(String traceName) { + if (traceName == null) { + traceName = Constants.DEFAULT_TRACE_NAME; + } + RxConfig.ThreadPoolConfig conf = RxConfig.INSTANCE.getThreadPool(); + conf.setTraceName(traceName); + ThreadPool.onTraceIdChanged.first((s, e) -> logCtx(conf.getTraceName(), e.getValue())); + } + + final WebTracer tracer; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(tracer).addPathPatterns("/**"); + } +} diff --git a/rxlib/src/main/resources/applicationContext.xml b/rxlib/src/main/resources/applicationContext.xml index 2823a7cb..79efa825 100644 --- a/rxlib/src/main/resources/applicationContext.xml +++ b/rxlib/src/main/resources/applicationContext.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> - +