当前位置: 首页 > article >正文

如何在微服务的日志中记录每个接口URL、状态码和耗时信息?

一、实现方式

1.直接通过SpringCloud-GateWay 的GlobalFilter实现

2.AOP+反射+自定义注解自己封装

二、具体实现

1.自定义注解
@Target({ElementType.METHOD})//作用在方法上
@Retention(RetentionPolicy.RUNTIME)//运行时生效
public @interface MethodExporter{
    //自定义注解只是为了提供切入点
}
2.通用controller
@RestController
@Slf4j
public class MethodExporterController{

    //http://localhost:24618/method/list?page=1&rows=7
    @GetMapping(value = "/method/list")
    @MethodExporter
    public Map list(@RequestParam(value = "page",defaultValue = "1") int page,
                    @RequestParam(value = "rows",defaultValue = "5")int rows){
        Map<String,String> result = new LinkedHashMap<>();
        result.put("code","200");
        result.put("message","success");

        //暂停毫秒
        try { TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); }

        return result;
    }

    @MethodExporter
    @GetMapping(value = "/method/get")
    public Map get(){
        Map<String,String> result = new LinkedHashMap<>();
        result.put("code","404");
        result.put("message","not-found");

        //暂停毫秒
        try { TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000)); } catch (InterruptedException e) { e.printStackTrace(); }

        return result;
    }

    @GetMapping(value = "/method/update")
    public String update(){
        System.out.println("update method without     @MethodExporter");
        return "ok update";
    }

}
3.AOP切面类
@Aspect
@Component
@Slf4j
public class MethodExporterAspect
{
    //容器捞鱼,谁带着使用了MethodExporter注解将会触发Around业务逻辑
    @Around("@annotation(com.atguigu.interview2.annotations.MethodExporter)")
    public Object methodExporter(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
    {
        Object retValue = null;

        long startTime = System.currentTimeMillis();
        System.out.println("-----@Around环绕通知AAA-methodExporter");

        retValue = proceedingJoinPoint.proceed(); //放行

        long endTime = System.currentTimeMillis();
        long costTime = endTime - startTime;

        //1 获得重载后的方法名
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();

        //2 确定方法名后获得该方法上面配置的注解标签MyRedisCache
        MethodExporter methodExporterAnnotation = method.getAnnotation(MethodExporter.class);

        if (methodExporterAnnotation != null)
        {
            //3 获得方法里面的形参信息
            StringBuffer jsonInputParam = new StringBuffer();
            Object[] args = proceedingJoinPoint.getArgs();
            DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
            String[] parameterNames = discoverer.getParameterNames(method);
            for (int i = 0; i < parameterNames.length; i++)
            {
                jsonInputParam.append(parameterNames[i] + "\t" + args[i].toString()+";");
            }
            //4 将返回结果retValue序列化
            String jsonResult = null;
            if(retValue != null){
                jsonResult = new ObjectMapper().writeValueAsString(retValue);
            }else{
                jsonResult = "null";
            }

            log.info("\n方法分析上报中 " +
                    "\n类名方法名:"+proceedingJoinPoint.getTarget().getClass().getName()+"."+proceedingJoinPoint.getSignature().getName()+"()"+
                    "\n执行耗时:"+costTime+"毫秒"+
                    "\n输入参数:"+jsonInputParam+""+
                    "\n返回结果:"+jsonResult+"" +
                    "\nover"
            );
            System.out.println("-----@Around环绕通知BBB-methodExporter");
        }

        return retValue;
    }
}


http://www.kler.cn/news/309165.html

相关文章:

  • python中Web开发框架的使用
  • 多速率信号处理
  • sourceTree使用笔记
  • ClickHouse的安装配置+DBeaver远程连接
  • DP子序列问题
  • Spring Boot-静态资源管理问题
  • Spring Cloud全解析:服务调用之Feign的编解码器
  • WebSocket 协议
  • VMware vSphere 8.0 Update 3b 发布下载,新增功能概览
  • 飞速爆单!TikTok跨境选品逻辑大揭秘!
  • socat用法结合案例分析
  • 我的AI工具箱Tauri版-MoYin文本转语音
  • 算法训练——day14字母异位词
  • 计算机三级网络技术总结(二)
  • 【D3.js in Action 3 精译_022】3.2 使用 D3 完成数据准备工作
  • Golang | Leetcode Golang题解之第400题第N位数字
  • 通信工程学习:什么是LCAS链路容量调整机制
  • LLM大模型基础知识学习总结,零基础入门到精通 非常详细收藏我这一篇就够了
  • 1.接口测试基础
  • Selenium等待机制:理解并应用显式等待与隐式等待,解决页面加载慢的问题
  • golang实现正向代理http_proxy和https_proxy
  • 【Python】从基础到进阶(八):文件操作与上下文管理
  • mybatis-generator代码生成
  • iOS 18 新功能:控制中心大變身!控制項目自由選配
  • 手机、平板电脑编程———未来之窗行业应用跨平台架构
  • 「iOS」push与present
  • Centos7安装gitlab-ce(rpm安装方式)
  • 干耳怎么掏耳朵?可视挖耳勺推荐平价
  • DeepFake换脸检测
  • 828华为云征文|基于华为云Flexus云服务器X实现个人博客搭建