首页 > 编程语言 > 详细

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

时间:2020-06-01 11:51:32      阅读:92      评论:0      收藏:0      [点我收藏+]

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);
    }

  

 

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

原文:https://www.cnblogs.com/jerry-wei/p/13023901.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!