Skip to content

Commit

Permalink
优化访问日志性能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 27, 2021
1 parent 5f752c8 commit 7846812
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,11 @@ public class ReactiveAopAccessLoggerSupport extends StaticMethodMatcherPointcutA
@Autowired
private ApplicationEventPublisher eventPublisher;

private final Map<Method, LoggerDefine> defineCache = new ConcurrentReferenceHashMap<>();

private static final LoggerDefine UNSUPPORTED = new LoggerDefine();

@SuppressWarnings("all")
public ReactiveAopAccessLoggerSupport() {
setAdvice((MethodInterceptor) methodInvocation -> {
MethodInterceptorHolder methodInterceptorHolder = MethodInterceptorHolder.create(methodInvocation);
Expand All @@ -55,44 +61,61 @@ public ReactiveAopAccessLoggerSupport() {
});
}

private Mono<RequestInfo> 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()
.<RequestInfo>flatMap(ctx -> Mono.<RequestInfo>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()
.<RequestInfo>flatMap(ctx -> Mono.<RequestInfo>justOrEmpty(ctx.getOrEmpty(RequestInfo.class))
.doOnNext(info -> ReactiveLogger.log(ctx, info::setContext)))
return this
.currentRequestInfo()
.doOnNext(loggerInfo::putAccessInfo)
.then(mono)
.doOnError(loggerInfo::setException)
.doOnSuccess(loggerInfo::setResponse)
.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());
Expand All @@ -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<Object> arr = new ArrayList<>();
value.put(name, arr);
args[i] = ((Flux) val)
args[i] = ((Flux<?>) val)
.doOnNext(param -> {
arr.add(param);
});
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Annotation> a1 = AnnotatedElementUtils.findAllMergedAnnotations(method, annotations);
Set<Annotation> a2 = AnnotatedElementUtils.findAllMergedAnnotations(clazz, annotations);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7846812

Please sign in to comment.