当前位置:   article > 正文

springboot整合AOP(1)——监控每个方法的执行时间_springboot3监听每个请求执行时间

springboot3监听每个请求执行时间

1.  需求 背景

性能调优阶段,需要找出执行时间比较长的方法,针对这些方法进行调优。

 

2.  可行方案

一种是传统的在每个方法前后获取System.currentMis(),然后得到方法的执行时间。

这种方式的缺点是方法多会写很多耦合代码,而且不可重用,测试完需要删掉。

 

另一种使用AOP监控方法的前后点,监控方法的执行时间,比较优雅且无侵入。

可行的方案之一是使用 Around Advice, 环绕通知可在方法执行前后做一些操作。

 

AOP,面向切面编程,Aspect Oriented Programming, 可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术

 

3. AOP

jar librarys requires:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-aop</artifactId>
  4. </dependency>

service层,写n多个方法,随便设置些方法执行时间

  1. package com.example.demo.service;
  2. import org.springframework.stereotype.Service;
  3. @Service
  4. public class aopService {
  5. public String getMethod1(){
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. return "方法1休眠1秒";
  12. }
  13. public String getMethod2(){
  14. try {
  15. Thread.sleep(2000);
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. return "方法2休眠2秒";
  20. }
  21. public String getMethod3(){
  22. try {
  23. Thread.sleep(3000);
  24. } catch (InterruptedException e) {
  25. e.printStackTrace();
  26. }
  27. return "方法3休眠3秒";
  28. }
  29. public String getMethod4(){
  30. try {
  31. Thread.sleep(4000);
  32. } catch (InterruptedException e) {
  33. e.printStackTrace();
  34. }
  35. return "方法4休眠4秒";
  36. }
  37. }

接下来创建切面AOP实现类

连接点point,第一个*表示方法返回任意值,然后是包.类.方法,第二个*代表类中的任意方法,括号中两点表示方法的参数任意,这里描述的是切入点为service包下的aopService类中的所有方法,类也可以用*号代替表示包下的所有类

  1. package com.example.demo.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.Around;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.aspectj.lang.reflect.MethodSignature;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.util.StopWatch;
  9. @Aspect
  10. @Component
  11. public class timeAop {
  12. private final static String POINT = "execution (* com.example.demo.service.aopService.*(..))";
  13. @Pointcut(POINT)
  14. public void recordLog(){}
  15. @Around("recordLog()")
  16. public Object around(ProceedingJoinPoint pjp) throws Throwable{
  17. StopWatch stopWatch = new StopWatch();
  18. stopWatch.start();
  19. Object obj = pjp.proceed(pjp.getArgs());
  20. stopWatch.stop();
  21. long cost = stopWatch.getTotalTimeMillis();
  22. MethodSignature signature = (MethodSignature) pjp.getSignature();
  23. String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
  24. System.out.println("----------- 执行" + methodName + "方法, 用时: " + cost + "ms -----------");
  25. return obj;
  26. }
  27. }

最后controller层调用

  1. package com.example.demo.controller;
  2. import com.example.demo.service.aopService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class aopController {
  8. @Autowired
  9. public aopService service;
  10. @RequestMapping("/getAop")
  11. public String getAop(){
  12. String s1 = service.getMethod1();
  13. String s2 = service.getMethod2();
  14. String s3 = service.getMethod3();
  15. String s4 = service.getMethod4();
  16. return s1+s2+s3+s4;
  17. }
  18. }

控制台输出打印了

  1. ----------- 执行com.example.demo.service.aopService.getMethod1方法, 用时: 1005ms -----------
  2. ----------- 执行com.example.demo.service.aopService.getMethod2方法, 用时: 2000ms -----------
  3. ----------- 执行com.example.demo.service.aopService.getMethod3方法, 用时: 3000ms -----------
  4. ----------- 执行com.example.demo.service.aopService.getMethod4方法, 用时: 4000ms -----------

 

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

闽ICP备14008679号