优雅统计接口耗时:Spring Boot实战中的四种高效方案
一、需求背景与方案选型
在电商系统压力测试中,我们发现某些接口响应时间超过2秒,但难以快速定位瓶颈。本文将通过四种方案实现接口耗时统计:
方案 | 优点 | 适用场景 |
---|---|---|
Spring AOP | 非侵入式、灵活度高 | 需要详细方法级统计 |
Filter | 简单易用、全局覆盖 | 快速实现入口统计 |
Interceptor | 结合请求上下文 | 需要获取请求参数 |
Micrometer+Prometheus | 生产级监控、可视化 | 长期性能监控分析 |
二、AOP方案实现(推荐)
2.1 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2.2 耗时统计切面
@Aspect
@Component
@Slf4j
public class ApiTimeAspect {
// 定义切入点:所有Controller的public方法
@Pointcut("execution(public * com.example..controller.*.*(..))")
public void apiPointcut() {}
@Around("apiPointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result;
try {
result = joinPoint.proceed();
} finally {
long cost = System.currentTimeMillis() - startTime;
recordCost(joinPoint, cost);
}
return result;
}
private void recordCost(ProceedingJoinPoint joinPoint, long cost) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
log.info("API耗时统计 || 方法: {} || 耗时: {}ms", methodName, cost);
// 可扩展存储到数据库
// monitorService.saveApiCost(methodName, cost);
}
}
2.3 自定义注解实现精准统计
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TimeMonitor {
String value() default "";
}
// 在切面中修改切入点表达式
@Pointcut("@annotation(com.example.annotation.TimeMonitor)")
public void annotationPointcut() {}
// 使用示例
@RestController
public class OrderController {
@TimeMonitor("创建订单接口")
@PostMapping("/orders")
public Order createOrder() {
// 业务逻辑
}
}
三、Filter方案实现(快速接入)
3.1 实现Filter
@WebFilter(urlPatterns = "/*")
@Slf4j
public class TimeCostFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
long start = System.currentTimeMillis();
try {
chain.doFilter(request, response);
} finally {
HttpServletRequest req = (HttpServletRequest) request;
String uri = req.getRequestURI();
long cost = System.currentTimeMillis() - start;
log.info("请求路径: {} || 耗时: {}ms", uri, cost);
}
}
}
3.2 启用Filter扫描
@SpringBootApplication
@ServletComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
四、Interceptor方案实现(结合请求参数)
4.1 实现Interceptor
@Component
@Slf4j
public class TimeInterceptor implements HandlerInterceptor {
private static final ThreadLocal<Long> TIME_HOLDER = new ThreadLocal<>();
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) {
TIME_HOLDER.set(System.currentTimeMillis());
return true;
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex) {
long start = TIME_HOLDER.get();
long cost = System.currentTimeMillis() - start;
TIME_HOLDER.remove();
String params = getRequestParams(request);
log.info("请求路径: {}?{} || 耗时: {}ms",
request.getRequestURI(), params, cost);
}
private String getRequestParams(HttpServletRequest request) {
return request.getParameterMap().entrySet().stream()
.map(entry -> entry.getKey() + "=" + Arrays.toString(entry.getValue()))
.collect(Collectors.joining("&"));
}
}
4.2 注册Interceptor
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private TimeInterceptor timeInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(timeInterceptor)
.addPathPatterns("/api/**");
}
}
五、生产级监控方案(Prometheus集成)
5.1 添加依赖
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
5.2 配置监控指标
@Configuration
public class MetricsConfig {
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
}
// 在Controller方法上添加注解
@RestController
public class ProductController {
@Timed(value = "product.detail.time", description = "商品详情接口耗时")
@GetMapping("/products/{id}")
public Product getDetail(@PathVariable Long id) {
// 业务逻辑
}
}
5.3 Prometheus配置示例
scrape_configs:
- job_name: 'spring_app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
六、性能优化建议
- 异步日志写入:避免日志输出阻塞请求线程
@Async
public void saveCostLog(String method, long cost) {
// 异步存储到数据库
}
- 采样率控制:高并发场景下按比例采样
if (random.nextDouble() < 0.1) { // 10%采样率
recordCost(joinPoint, cost);
}
- 异常处理:确保统计逻辑不破坏主流程
try {
recordCost(...);
} catch (Exception e) {
log.error("耗时统计异常", e);
}
七、方案对比与选型建议
维度 | AOP方案 | Filter方案 | Interceptor方案 | Prometheus方案 |
---|---|---|---|---|
实现复杂度 | 中 | 低 | 中 | 高 |
数据粒度 | 方法级 | 请求级 | 请求级 | 方法级 |
性能影响 | 低(纳秒级) | 低 | 低 | 中 |
扩展性 | 高 | 中 | 中 | 高 |
生产可维护性 | 高 | 中 | 中 | 极高 |
选型建议:
- 快速验证:Filter方案
- 精准统计:AOP+自定义注解
- 生产监控:Prometheus+Micrometer
扩展阅读:
- Spring AOP官方文档
- Micrometer监控指南
- 阿里巴巴Java诊断工具Arthas
掌握接口耗时统计技巧,让系统性能优化有据可依! 🚀