在SpringBoot项目中坚控接口调用情况是提升系统可观测性的重要手段,本文详细介绍五种实现方案及其适用场景。

通过Spring AOP特性,我们可以创建一个切面类来统一拦截Controller方法,无需修改原有代码即可实现接口坚控功能。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>2.0.xversion>
dependency>
package com.example.aspect;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
@Slf4j
@Aspect
@Component
public class ApiMonitorAspect {
private final Map callCountMap = new ConcurrentHashMap<>();
@Pointcut("execution(* com.example.controller.*.*(..))")
public void controllerPointcut() {}
@Around("controllerPointcut()")
public Object monitorApi(ProceedingJoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String methodName = joinPoint.getTarget().getClass().getSimpleName() + "." + joinPoint.getSignature().getName();
long currentCount = callCountMap.computeIfAbsent(methodName, k -> new AtomicLong(0)).incrementAndGet();
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long elapsed = System.currentTimeMillis() - startTime;
log.info("接口坚控信息:n 请求地址: {} {}n 接口方法: {}n 调用次数: {}n 响应耗时: {}ms",
request.getMethod(), request.getRequestURL(),
methodName, currentCount, elapsed);
return result;
}
}
2026-05-28 14:30:22.156 [http-nio-8080-exec-1] INFO c.e.aspect.ApiMonitorAspect -
接口坚控信息:
请求地址: GET http://localhost:8080/api/user/list
接口方法: UserController.getUserList
调用次数: 1
响应耗时: 45ms
通过自定义注解可以精确控制需要坚控的接口范围,避免对所有接口进行坚控。
package com.example.annotation;
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 Monitored {
String value() default "";
}
@Slf4j
@Aspect
@Component
public class MonitoredApiAspect {
@Pointcut("@annotation(com.example.annotation.Monitored)")
public void monitoredPointcut() {}
@Around("monitoredPointcut() && @annotation(monitored)")
public Object monitorAnnotatedApi(ProceedingJoinPoint joinPoint, Monitored monitored) throws Throwable {
// 坚控逻辑实现
}
}
通过实现HandlerInterceptor接口,可以在请求处理前后进行拦截和坚控。
@Slf4j
@Component
public class ApiMonitorInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
// 请求前处理逻辑
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
// 请求后处理逻辑
}
}
结合Micrometer和Spring Boot Actuator,可以实现生产环境下的接口坚控需求。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>io.micrometergroupId>
<artifactId>micrometer-registry-prometheusartifactId>
dependency>
本文详细介绍了五种接口坚控方案,从开发调试到生产环境坚控,开发者可根据实际需求选择最合适的实现方式。