集中我的微服务的 AOP 日志记录

问题描述 投票:0回答:1

我有多个微服务,并建立了 AOP 日志记录来记录我的控制器、服务和存储库层。目前,同一个 AppAspect 类在我的所有微服务中都是多余的。有没有办法集中起来?

一个想法:将 AOP 类作为 jar,将其上传到某个存储库(甚至 AWS S3),然后让我的微服务依赖于它们。

为什么? :每当我想对 AOP 日志进行小更改时,我必须在所有微服务中重复执行此操作。

任何线索都会有所帮助。以下是我的 AOP AppAspect 类

@Aspect
@Component
@Slf4j
public class AppAspect {
    private String ipAddress = "[NO-IP]";

    @Before("execution(* np.com.oskarshrestha.bookstorebackend.controller..*(..)) " +
            "|| execution(* np.com.oskarshrestha.bookstorebackend.service..*(..)) " +
            "|| execution(* np.com.oskarshrestha.bookstorebackend.repository..*(..))")
    public void logBeforeMethodExecution(JoinPoint joinPoint) {
        try {
            // get ip address
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                HttpServletRequest request = requestAttributes.getRequest();
                ipAddress = request.getRemoteAddr();
            }
            // get method name
            String methodName = joinPoint.getSignature().getDeclaringType().getPackage().getName() + "." + joinPoint.getSignature().getName();
            log.info("[{}] Started method execution {}", ipAddress, methodName);
        } catch (Exception e) {
            log.error("{} Error in AppAspect {}", ipAddress, e.getMessage());
        }
    }

    @AfterReturning("execution(* np.com.oskarshrestha.bookstorebackend.controller..*(..)) " +
            "|| execution(* np.com.oskarshrestha.bookstorebackend.service..*(..)) " +
            "|| execution(* np.com.oskarshrestha.bookstorebackend.repository..*(..))")
    public void logAfterMethodExecution(JoinPoint joinPoint) {
        try {
            // get method name
            String methodName = joinPoint.getSignature().getDeclaringType().getPackage().getName() + "." + joinPoint.getSignature().getName();
            log.info("[{}] Completed method execution {}", ipAddress, methodName);
        } catch (Exception e) {
            log.error("{} Error in AppAspect {}", ipAddress, e.getMessage());
        }
    }

    @AfterThrowing(value = "execution(* np.com.oskarshrestha.bookstorebackend.controller..*(..)) " +
            "|| execution(* np.com.oskarshrestha.bookstorebackend.service..*(..)) " +
            "|| execution(* np.com.oskarshrestha.bookstorebackend.repository..*(..))", throwing = "exception")
    public void logAfterMethodExecutionThrows(JoinPoint joinPoint, Exception exception) {
        try {
            // get method name
            String methodName = joinPoint.getSignature().getDeclaringType().getPackage().getName() + "." + joinPoint.getSignature().getName();
            log.info("[{}] Exception thrown by method execution {} {}", ipAddress, methodName, exception.getMessage());
        } catch (Exception e) {
            log.error("{} Error in AppAspect {}", ipAddress, e.getMessage());
        }
    }
}

我尝试寻找参考资料或资源,但没有找到太多线索。这可能吗?

java spring-boot microservices spring-aop
1个回答
0
投票

这不是集中日志的方式。您可以使用 AOP 添加一些额外的逻辑,但这不是您的情况。典型的微服务架构包括外部跟踪和日志记录解决方案:

  1. 跟踪用于跟踪相关微服务从开始到结束的所有请求路径。可以使用 Jaeger + Opentracing 规范(适用于 Spring Boot v2)或 Opentelemetry 规范(适用于 Spring Boot v3)
  2. 进行归档
  3. 日志解决方案通常使用日志存储和ui服务器来浏览日志。可以使用 ELK stack + Grafana(而不是 Kibana)进行存档。

因此,如果您希望微服务系统具有良好的可观察性,您应该尝试这些解决方案。谷歌随机 Kibana 截图:

额外提示:尝试用@Around替换@Before和@After注释(并删除硬编码的包名称)

© www.soinside.com 2019 - 2024. All rights reserved.