当前位置: 首页 > news >正文

通过注解统计接口调用耗时

 

要通过注解统计接口调用耗时,可以按照以下步骤进行操作:

  1. 首先,在您的项目中引入一个AOP(面向切面编程)框架,比如Spring AOP或AspectJ。这些框架可以帮助您在方法执行前后插入额外的逻辑。

  2. 创建一个自定义的注解,用于标记需要被统计耗时的方法。例如,您可以创建一个名为@Timing的注解。

  3. 在AOP配置文件中,定义一个切面(Aspect),并使用切点表达式匹配包含@Timing注解的方法。切点表达式可以筛选出带有@Timing注解的方法,以便后续对其进行处理。

  4. 在切面中,使用@Around注解的方法中记录方法执行开始时间和结束时间,并计算耗时。

  5. 可以将耗时信息记录到日志中或者其他适当的位置,以供后续分析和监控。

  • 创建一个Spring Boot项目,引入以下依赖(pom.xml):
<dependencies><!--其他依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--其他依赖-->
</dependencies>
  • 创建一个自定义注解@Timing,用于标记需要统计耗时的方法。
package com.example.demo.aspect;import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Timing {
}
  • 创建一个切面类TimingAspect,用于处理被@Timing注解标记的方法。
package com.example.demo.aspect;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;@Aspect
@Component
public class TimingAspect {private static final Logger LOGGER = LoggerFactory.getLogger(TimingAspect.class);@Around("@annotation(com.example.demo.aspect.Timing)")public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {long startTime = System.currentTimeMillis();Object result = joinPoint.proceed();long endTime = System.currentTimeMillis();LOGGER.info("{} executed in {} ms", joinPoint.getSignature(), endTime - startTime);return result;}
}
  • 创建一个Controller类,其中的方法使用@Timing注解标记。
package com.example.demo.controller;import com.example.demo.aspect.Timing;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyController {@GetMapping("/hello")@Timingpublic String hello() {// 模拟耗时操作try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return "Hello, World!";}
}
  • 在启动类上添加@EnableAspectJAutoProxy注解,开启AOP代理。
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;@SpringBootApplication
@EnableAspectJAutoProxy
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}
}

 当访问 ​http://localhost:8080/hello​时,TimingAspect中的logExecutionTime方法将会在接口执行前后打印日志,并记录接口调用耗时。

http://www.lryc.cn/news/231769.html

相关文章:

  • Oracle-动态sql学习笔记,由易至难讲解七个例子
  • Kafka 的应用场景
  • 保驾“双十一” 博睿数据助力电商零售迎高峰无烦忧
  • F.binary_cross_entropy、nn.BCELoss、nn.BCEWithLogitsLoss与F.kl_div函数详细解读
  • 后端接口性能优化分析
  • 【ceph】ceph集群中使用多路径(Multipath)方法
  • Xshell+Xftp通过代理的方式访问局域网内网服务器
  • 对盒子中的材料进行计数
  • 科技驱动固定资产管理变革:RFID技术的前沿应用
  • Django路由层之有名分组和无名分组、反向解析、路由分发、伪静态的概念、名称空间、虚拟环境、Django1和Django2的区别
  • 【nlp】2.5 人名分类器实战项目(对比RNN、LSTM、GRU模型)
  • 海康Visionmaster-环境配置:MFC 二次开发环境配置方法
  • 利用EXCEL中的VBA对同一文件夹下的多个数据文件进行特定提取
  • FPGA时序约束(七)文献时序约束实验测试
  • 【数据库开发】DataX开发环境的安装部署(Python、Java)
  • Flutter实践一:package组织
  • SpringCloud微服务:Ribbon负载均衡
  • 【教程】大气化学在线耦合模式WRF/Chem
  • GDS 命令的使用 srvctl service TAF application continuity
  • go 语言之 select
  • 23款奔驰GLC260L升级小柏林音响 全新15个扬声器
  • ssh 免密码登录
  • 小程序使用腾讯位置插件获取当前位置
  • 零基础学Python怎么学习?我来告诉你
  • 开源软件 FFmpeg 生成模型使用图片数据集
  • Linux Shell 通配符 / glob 模式
  • 深入了解域名与SSL证书的关系
  • 计算属性与watch的区别,fetch与axios在vue中的异步请求,单文本组件使用,使用vite创建vue项目,组件的使用方法
  • 2023.11.14 hivesql的容器,数组与映射
  • Android Glide照片宫格RecyclerView,点击SharedElement共享元素动画查看大图,Kotlin(1)