【问题标题】:Logging elapsed time of execution in SpringBoot rest API在 Spring Boot REST API 中记录经过的执行时间
【发布时间】:2019-11-10 18:31:58
【问题描述】:

这可能是一个简单的解决方案,但我无法完成。 我需要在 SpringBoot Rest API 中记录我的请求的总体执行时间。 请求总是进入MainController 总是可以从两个地方退出-

  1. mainRestcontroller同样的方法或者
  2. ExceptionHandlerController处理方法

我创建了一个自定义注释并将其注入到主要的 ControllerExceptionController 方法中,并获取各个方法的经过时间。 所以这就是问题所在。我需要添加这些单独的时间来计算我不想要的总时间。

有没有其他方法可以轻松记录这些信息。

方面类:

@Aspect
@Component
public class PointsAspect {

    private final static Logger logger = LoggerFactory.getLogger(PointsAspect.class);

    @Around("@annotation(annotation)")
    public Object logMethod(final ProceedingJoinPoint proceedingJoinPoint, final LogAround annotation)
            throws Throwable {
        final long start = System.currentTimeMillis();
        Object obj;
        try {
            logger.debug("Starting...! Method Name - " +proceedingJoinPoint.getSignature().getName());
            obj = proceedingJoinPoint.proceed();
        } finally {
            logger.debug("Exiting...! Method Name - " +proceedingJoinPoint.getSignature().getName() +"Execution Time in Milliseconds:> "+ String.valueOf(System.currentTimeMillis()-start));
        }
        return obj;
    }
}

标记界面:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAround {

}

这就是我注入它的方式:

**ExceptionHandlerController.java**

@LogAround
@ExceptionHandler(HttpMessageNotReadableException.class)
public GenericFailureResponse missingRequestBodyException(HttpServletResponse response,
            HttpServletRequest request, Exception ex) throws IOException {
        GenericFailureResponse failureResponse =  new GenericFailureResponse();
        //TODO: exception logic
        return failureResponse;
}



**MainController.java**

@LogAround
 public String getTotalPoints(@RequestParam(required=true) long memberNo,
            HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        //TODO : some logic
        return "something";
 }

【问题讨论】:

    标签: spring spring-boot spring-aop spring-annotations java-annotations


    【解决方案1】:

    您可以使用简单的过滤器。

    @Component
    public class LogTimeFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            long startTime = System.currentTimeMillis();
            chain.doFilter(request, response);
            long duration = System.currentTimeMillis() - startTime;
            System.out.println("Request take " + duration + " ms");
        }
    }
    

    【讨论】:

    • 在新类中添加上述代码后,它开始无限记录消息。应用程序启动后,它会连续记录“Request take ** ms”消息,即使没有命中 API 的任何端点。 2019-07-01 09:32:29.664 INFO [Points,,,] 21896 --- [restartedMain] c.h.g.a.p.MainApplication:在 26.113 秒内启动 MainApplication(JVM 运行 28.843)正在运行..!!!!!! os.web.servlet.DispatcherServlet :在 126 毫秒内完成初始化 请求耗时 64 毫秒 请求耗时 13 毫秒 请求耗时 8 毫秒 请求耗时 5 毫秒 请求耗时 5 毫秒 ----------
    • 你能给我一些实现细节吗?
    • 这是一个简单的 SprongBoot REST API。到目前为止什么都没有做。做 POC 并被这个用例卡住了。
    • 我问是因为我们在很多地方都使用了上面的例子,而且效果很好。如果没有击中任何端点,它就无法运行。也许尝试调试并观察调用堆栈。
    • 我忘了提一件事,我的项目中也有 swagger 依赖项。最初很少有请求是针对 swager-configuration 的,然后对一些随机框架类的请求不断涌现..
    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 2021-04-07
    • 2019-04-26
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多