当前位置:   article > 正文

springboot 监控接口(url)耗时_springboot如何监控接口慢调用

springboot如何监控接口慢调用

基于aop 实现

springboot 项目依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!--引入AOP依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

创建注解

/**
 * url 时间统计注解
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TakeTimeRecord {
    /**
     * url 名称
     * @return
     */
    String value() default "";
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

添加切点


@Aspect
@Order(1)
@Component
@Slf4j
public class TakeTimeRecordAspect {

    /**
     * 带有@TakeTime注解的方法 
     * com.example.executiontimedemo.interfaces.TakeTimeRecord (TakeTimeRecord 注解的包路径)
     */
    @Pointcut("@annotation(com.example.executiontimedemo.interfaces.TakeTimeRecord)")
    public void pointcut() {

    }

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        //接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        request.setAttribute("_startTime", System.currentTimeMillis());

    }

    @After("pointcut()")
    public void doAfterReturning(JoinPoint joinPoint) {

        //接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        long startTime = (Long) request.getAttribute("_startTime");
        //记录请求的内容
        log.info("请求URL: {}", request.getServletPath());
        log.info("请求METHOD:{}", request.getMethod());
        //处理完请求后
        log.info("方法执行时间:{}", (System.currentTimeMillis() - startTime));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

创建DemoController

@RestController
public class DemoController {

    @TakeTimeRecord("查询所有信息")
    @GetMapping(value = "/test")
    public Object findAll() {
        return new HashMap<>();
    }

	//添加监控注解
    @TakeTimeRecord("通过id查询数据")
    @GetMapping(value = "/findById")
    public void findById() {
        System.out.println("查询一个数据");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

请求接口控制台打印日志

在这里插入图片描述

基于 Interceptor 实现

创建 LoginInterceptor

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //设置请求开始时间
        request.setAttribute("_startTime", System.currentTimeMillis());
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        long startTime = (Long) request.getAttribute("_startTime");
        //记录请求的内容
        log.info("Interceptor请求URL: {}", request.getServletPath());
        log.info("Interceptor请求METHOD:{}", request.getMethod());
        log.info("Interceptor方法执行时间:{}", (System.currentTimeMillis() - startTime));

        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

创建 LoginConfig

@Configuration
public class LoginConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**配置登录拦截器*/
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")
                //忽略的url
                .excludePathPatterns("/login/**")
                ;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

启动项目,请求url

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/50086
推荐阅读
相关标签
  

闽ICP备14008679号