From 7846812266602c596ac74a3897e908a6b0f2e493 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Mon, 27 Dec 2021 13:45:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BF=E9=97=AE=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/logging/aop/AccessLoggerParser.java | 2 +- .../aop/DefaultAccessLoggerParser.java | 2 +- .../aop/ReactiveAopAccessLoggerSupport.java | 59 +++++++++++++------ .../aop/ResourceAccessLoggerParser.java | 2 +- .../aop/Swagger3AccessLoggerParser.java | 2 +- .../aop/SwaggerAccessLoggerParser.java | 2 +- 6 files changed, 47 insertions(+), 22 deletions(-) diff --git a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/AccessLoggerParser.java b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/AccessLoggerParser.java index e51573e94..584ba6f85 100644 --- a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/AccessLoggerParser.java +++ b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/AccessLoggerParser.java @@ -7,7 +7,7 @@ import java.lang.reflect.Method; public interface AccessLoggerParser { - boolean support(Class clazz, Method method); + boolean support(Class clazz, Method method); LoggerDefine parse(MethodInterceptorHolder holder); } diff --git a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/DefaultAccessLoggerParser.java b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/DefaultAccessLoggerParser.java index 3c9f742ba..a0fc433fc 100644 --- a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/DefaultAccessLoggerParser.java +++ b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/DefaultAccessLoggerParser.java @@ -13,7 +13,7 @@ public class DefaultAccessLoggerParser implements AccessLoggerParser { @Override - public boolean support(Class clazz, Method method) { + public boolean support(Class clazz, Method method) { AccessLogger ann = AnnotationUtils.findAnnotation(method, AccessLogger.class); //注解了并且未取消 return null != ann && !ann.ignore(); diff --git a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ReactiveAopAccessLoggerSupport.java b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ReactiveAopAccessLoggerSupport.java index 9dd11cf35..7afb1a897 100644 --- a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ReactiveAopAccessLoggerSupport.java +++ b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ReactiveAopAccessLoggerSupport.java @@ -16,6 +16,7 @@ import org.springframework.core.Ordered; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.ClassUtils; +import org.springframework.util.ConcurrentReferenceHashMap; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebFilter; import org.springframework.web.server.WebFilterChain; @@ -41,6 +42,11 @@ public class ReactiveAopAccessLoggerSupport extends StaticMethodMatcherPointcutA @Autowired private ApplicationEventPublisher eventPublisher; + private final Map defineCache = new ConcurrentReferenceHashMap<>(); + + private static final LoggerDefine UNSUPPORTED = new LoggerDefine(); + + @SuppressWarnings("all") public ReactiveAopAccessLoggerSupport() { setAdvice((MethodInterceptor) methodInvocation -> { MethodInterceptorHolder methodInterceptorHolder = MethodInterceptorHolder.create(methodInvocation); @@ -55,23 +61,34 @@ public ReactiveAopAccessLoggerSupport() { }); } + private Mono currentRequestInfo() { + return Mono + .subscriberContext() + .handle((context, sink) -> { + if (context.hasKey(RequestInfo.class)) { + RequestInfo info = context.get(RequestInfo.class); + ReactiveLogger.log(context, info::setContext); + sink.next(info); + } + }); + } + protected Flux wrapFluxResponse(Flux flux, AccessLoggerInfo loggerInfo) { - return Mono.subscriberContext() - .flatMap(ctx -> Mono.justOrEmpty(ctx.getOrEmpty(RequestInfo.class)) - .doOnNext(info -> ReactiveLogger.log(ctx, info::setContext))) + return this + .currentRequestInfo() .doOnNext(loggerInfo::putAccessInfo) .thenMany(flux) .doOnError(loggerInfo::setException) .doFinally(f -> { loggerInfo.setResponseTime(System.currentTimeMillis()); eventPublisher.publishEvent(new AccessLoggerAfterEvent(loggerInfo)); - }).subscriberContext(ReactiveLogger.start("accessLogId", loggerInfo.getId())); + }) + .subscriberContext(ReactiveLogger.start("accessLogId", loggerInfo.getId())); } protected Mono wrapMonoResponse(Mono mono, AccessLoggerInfo loggerInfo) { - return Mono.subscriberContext() - .flatMap(ctx -> Mono.justOrEmpty(ctx.getOrEmpty(RequestInfo.class)) - .doOnNext(info -> ReactiveLogger.log(ctx, info::setContext))) + return this + .currentRequestInfo() .doOnNext(loggerInfo::putAccessInfo) .then(mono) .doOnError(loggerInfo::setException) @@ -79,20 +96,26 @@ protected Mono wrapMonoResponse(Mono mono, AccessLoggerInfo loggerInfo) { .doFinally(f -> { loggerInfo.setResponseTime(System.currentTimeMillis()); eventPublisher.publishEvent(new AccessLoggerAfterEvent(loggerInfo)); - }).subscriberContext(ReactiveLogger.start("accessLogId", loggerInfo.getId())); + }) + .subscriberContext(ReactiveLogger.start("accessLogId", loggerInfo.getId())); + } + + private LoggerDefine createDefine(MethodInterceptorHolder holder) { + return loggerParsers + .stream() + .filter(parser -> parser.support(ClassUtils.getUserClass(holder.getTarget()), holder.getMethod())) + .findAny() + .map(parser -> parser.parse(holder)) + .orElse(UNSUPPORTED); } @SuppressWarnings("all") protected AccessLoggerInfo createLogger(MethodInterceptorHolder holder) { AccessLoggerInfo info = new AccessLoggerInfo(); info.setId(IDGenerator.MD5.generate()); - info.setRequestTime(System.currentTimeMillis()); - LoggerDefine define = loggerParsers.stream() - .filter(parser -> parser.support(ClassUtils.getUserClass(holder.getTarget()), holder.getMethod())) - .findAny() - .map(parser -> parser.parse(holder)) - .orElse(null); + + LoggerDefine define = defineCache.computeIfAbsent(holder.getMethod(), method -> createDefine(holder)); if (define != null) { info.setAction(define.getAction()); @@ -113,14 +136,14 @@ protected AccessLoggerInfo createLogger(MethodInterceptorHolder holder) { continue; } if (val instanceof Mono) { - args[i] = ((Mono) val) + args[i] = ((Mono) val) .doOnNext(param -> { value.put(name, param); }); } else if (val instanceof Flux) { List arr = new ArrayList<>(); value.put(name, arr); - args[i] = ((Flux) val) + args[i] = ((Flux) val) .doOnNext(param -> { arr.add(param); }); @@ -143,7 +166,9 @@ public int getOrder() { @Override public boolean matches(Method method, Class aClass) { - return loggerParsers.stream().anyMatch(parser -> parser.support(aClass, method)); + return loggerParsers + .stream() + .anyMatch(parser -> parser.support(aClass, method)); } @Override diff --git a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ResourceAccessLoggerParser.java b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ResourceAccessLoggerParser.java index acf42e5b3..faf351128 100644 --- a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ResourceAccessLoggerParser.java +++ b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/ResourceAccessLoggerParser.java @@ -22,7 +22,7 @@ public class ResourceAccessLoggerParser implements AccessLoggerParser { )); @Override - public boolean support(Class clazz, Method method) { + public boolean support(Class clazz, Method method) { Set a1 = AnnotatedElementUtils.findAllMergedAnnotations(method, annotations); Set a2 = AnnotatedElementUtils.findAllMergedAnnotations(clazz, annotations); diff --git a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/Swagger3AccessLoggerParser.java b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/Swagger3AccessLoggerParser.java index c0b91a8c7..12de9191b 100644 --- a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/Swagger3AccessLoggerParser.java +++ b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/Swagger3AccessLoggerParser.java @@ -12,7 +12,7 @@ public class Swagger3AccessLoggerParser implements AccessLoggerParser { @Override - public boolean support(Class clazz, Method method) { + public boolean support(Class clazz, Method method) { Tag api = AnnotationUtils.findAnnotation(clazz, Tag.class); Operation operation = AnnotationUtils.findAnnotation(method, Operation.class); diff --git a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/SwaggerAccessLoggerParser.java b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/SwaggerAccessLoggerParser.java index 408ab7d33..2ac75180b 100644 --- a/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/SwaggerAccessLoggerParser.java +++ b/hsweb-logging/hsweb-access-logging-aop/src/main/java/org/hswebframework/web/logging/aop/SwaggerAccessLoggerParser.java @@ -11,7 +11,7 @@ public class SwaggerAccessLoggerParser implements AccessLoggerParser { @Override - public boolean support(Class clazz, Method method) { + public boolean support(Class clazz, Method method) { Api api = AnnotationUtils.findAnnotation(clazz, Api.class); ApiOperation operation = AnnotationUtils.findAnnotation(method, ApiOperation.class);