此次实践基于spring boot 2.4.1
相关pom
代码语言:javascript复制<!-- aop 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
监控配置类
代码语言:javascript复制
import com.alibaba.fastjson.JSON;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* 接口监控
**/
@Aspect
@Component
public class LogAspect {
/**
* 配置监控的地址 注:这里配置的是controller的路径
*/
@Pointcut("execution(public * com.batata.continuing.web.*..*(..))")
public void methodPointCut() {
}
@Around("methodPointCut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Signature signature = pjp.getSignature();
Long startTime = null; // 开始时间
Long endTime = null; // 结束时间
//被代理的类的类名
String className = pjp.getTarget().getClass().getName();
//方法名
String methodName = signature.getName();
//获取日志
Logger logger = LoggerFactory.getLogger(className);
//参数数组
Object[] requestParams = pjp.getArgs();
StringBuffer sb = new StringBuffer();
for (Object requestParam : requestParams) {
if (requestParam != null) {
sb.append(JSON.toJSONString(requestParam));
sb.append(",");
}
}
String requestParamsString = sb.toString();
// 去除最后一个逗号
if (requestParamsString.length() > 0) {
requestParamsString = requestParamsString.substring(0, requestParamsString.length() - 1);
}
//接口应答前打印日志
logger.info(String.format("【%s】类的【%s】方法,请求参数:%s", className, methodName, requestParamsString));
//接口调用开始响应起始时间
startTime = System.currentTimeMillis();
//正常执行方法
Object response = pjp.proceed();
//接口调用结束时间
endTime = System.currentTimeMillis();
//接口应答之后打印日志
logger.info(String.format("【%s】类的【%s】方法,应答参数:%s", className, methodName, JSON.toJSONString(response)));
//接口耗时
logger.info(String.format("接口【%s】总耗时(毫秒):%s", methodName, (endTime - startTime)));
return response;
}
}
调用接口进行测试