springboot 使用aop 监控接口请求信息


maven依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>  

创建注解类

package com.avivacofco.epidemic.aop;

import java.lang.annotation.*;

/**
 * @author :jerry_wei
 * @date :Created in 2020/5/25 17:41
 * @description:请求日志
 * @modified By:
 * @version:
 */
//注解声明周期
@Retention(RetentionPolicy.RUNTIME)
//注解 修饰方法
@Target(ElementType.METHOD)
@Documented
public @interface ResLog {

    //接口描述
    String desc() default  "";

}

  

配置内容

package com.avivacofco.epidemic.aop;

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.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.lang.reflect.Method;

/**
 * @author :jerry_wei
 * @date :Created in 2020/5/25 17:44
 * @description:
 * @modified By:
 * @version:
 */
@Aspect
@Component
public class AopConfig {

    private static Logger logger = LoggerFactory.getLogger(AopConfig.class);

    @Pointcut("@annotation(com.avivacofco.epidemic.aop.ResLog)")
    public void resLog(){

    }


    @Around("resLog()")
    public Object around(ProceedingJoinPoint pjp){
        long begin = System.currentTimeMillis();
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        ResLog resLog = method.getAnnotation(ResLog.class);

        String desc = resLog.desc();

        //可以添加日志表
        //记录用户 轨迹
        // 先进 filter 在进 interceptor 最后进 aop
        // 可以通过 token 拦截,然后 通过token 获取用户数据 在aop 中 获取 user 数据


        logger.info("请求开始");
        logger.info("请求连接 {}",request.getRequestURL().toString());
        logger.info("接口描述 {}",desc);
        logger.info("请求类型 {}",request.getMethod());
        logger.info("请求方法 {}.{}",signature.getDeclaringTypeName(),signature.getName());
        logger.info("请求ip {}",request.getRemoteAddr());
        logger.info("请求入参 {}", JSON.toJSONString(pjp.getArgs()));
        logger.info("请求token {}",request.getHeader("sso-token"));
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

        long end = System.currentTimeMillis();
        logger.info("请求耗时 {}",end-begin);
        logger.info("请求返回 {}",JSON.toJSON(result));
        logger.info("请求结束");

        return result;

    }

}

  

正常使用

    @ResLog(desc = "阅读消息接口")
    @PostMapping ("/read")
    public Map read(@RequestBody Map resMap){
        return remindService.read(resMap);
    }

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM