zoukankan      html  css  js  c++  java
  • SpringBoot之统计接口执行耗时

    实现功能:使用AOP统计方法执行耗时

    Maven依赖:

            <!--引入AOP依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>

    自定义注解(加上该注解的方法系统自动统计耗时):

    复制代码
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * 统计耗时
     */
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TakeTime {
    
    }
    复制代码

    TakeTimeAspect(使用AOP技术统计方法执行前后消耗时间):

    复制代码
    import com.alibaba.fastjson.JSON;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * 耗时统计
     */
    @Aspect
    @Component
    public class TakeTimeAspect {
    
        private static final Logger logger = LoggerFactory.getLogger(TakeTimeAspect.class);
    
    
        //统计请求的处理时间
        ThreadLocal<Long> startTime = new ThreadLocal<>();
    
        /**
         * 带有@TakeTime注解的方法
         */
        @Pointcut("@annotation(com.emi2c.mybatis.config.annotation.TakeTime)")
        public void log() {
    
        }
    
        @Before("log()")
        public void doBefore(JoinPoint joinPoint) throws Throwable {
            startTime.set(System.currentTimeMillis());
            //接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            //记录请求的内容
            logger.info("请求URL:" + request.getRequestURL().toString());
            logger.info("请求METHOD:" + request.getMethod());
        }
    
        @AfterReturning(returning = "ret", pointcut = "log()")
        public void doAfterReturning(Object ret) {
            //处理完请求后,返回内容
            logger.info("方法返回值:" + JSON.toJSONString(ret));
            logger.info("方法执行时间:" + (System.currentTimeMillis() - startTime.get()));
        }
    
    
    }
    复制代码

    使用示例:

    复制代码
        @TakeTime
        @RequestMapping(value = "/test", method = RequestMethod.GET)
        public List<User> findAll() {
            List<User> userList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                User user = new User();
                user.setUsername("user" + i);
                user.setPassword("password" + i);
                userList.add(user);
            }
            return userList;
        }
    复制代码

     

  • 相关阅读:
    JS的中数的取值范围的大小
    前端通过xlsx插件导入excel
    H5和安卓原生进行交互的操作流程记录
    javascript中字符串和数字之间互相转换的方法总结
    gitlab代码合并到主分支
    typeof和valueof、instance of 之间的区别
    javascript中map会改变原始的数组吗
    使用typescript来写react遇到的一些问题
    使用javascript进行时间数据格式的转换
    在vue的移动端项目中使用vue-echarts
  • 原文地址:https://www.cnblogs.com/javalinux/p/14898140.html
Copyright © 2011-2022 走看看