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

embedded/2024/9/23 2:46:04/

一、实现方式

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")@MethodExporterpublic 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 确定方法名后获得该方法上面配置的注解标签MyRedisCacheMethodExporter 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.ppmy.cn/embedded/115361.html

相关文章

Python知识点:如何使用Python进行算法交易

开篇&#xff0c;先说一个好消息&#xff0c;截止到2025年1月1日前&#xff0c;翻到文末找到我&#xff0c;赠送定制版的开题报告和任务书&#xff0c;先到先得&#xff01;过期不候&#xff01; 使用Python进行算法交易的完整指南 在当今快节奏的金融市场中&#xff0c;算法…

systemctl控制服务和守护进程

system守护进程介绍: systemd daemon(守护进程)管理linux的启动,包括服务的启动和管理 systemd可在系统引导时以及运行中的系统上激活系统资源、服务器守护进程和其他进程。 守护进程daemon是在后台运行或等待的进程,以执行不同的任务。通常daemon在系统启动时…

Android架构组件: MVVM模式的实战应用与数据绑定技巧

随着Android应用的复杂性增加&#xff0c;开发人员面临代码重用性、可维护性和扩展性问题。为了解决这些问题&#xff0c;谷歌推出了Android架构组件&#xff08;Android Architecture Components&#xff09;&#xff0c;这套框架能帮助构建高效、可维护的应用。MVVM&#xff…

基于YOLOv5的教室人数检测统计系统

基于YOLOv5的教室人数检测统计系统可以有效地用于监控教室内的学生数量&#xff0c;适用于多种应用场景&#xff0c;比如 自动考勤、安全监控或空间利用分析 以下是如何构建这样一个系统的概述&#xff0c;包括环境准备、数据集创建、模型训练以及如何处理不同类型的媒体输入…

DNF Decouple and Feedback Network for Seeing in the Dark

DNF: Decouple and Feedback Network for Seeing in the Dark 在深度学习领域&#xff0c;尤其是在低光照图像增强的应用中&#xff0c;RAW数据的独特属性展现出了巨大的潜力。然而&#xff0c;现有架构在单阶段和多阶段方法中都存在性能瓶颈。单阶段方法由于域歧义&#xff0c…

weblogic CVE-2018-2894 靶场攻略

漏洞描述 Weblogic Web Service Test Page中⼀处任意⽂件上传漏洞&#xff0c;Web Service Test Page 在 "⽣产模式"下默认不开启&#xff0c;所以该漏洞有⼀定限制。 漏洞版本 weblogic 10.3.6.0 weblogic 12.1.3.0 weblogic 12.2.1.2 28 weblogic 12.2.1.3 …

多个PDF合并为一个PDF

1、安装依赖&#xff1a; 首先&#xff0c;确保在Python环境中安装了 PyPDF2 库&#xff0c;可以通过以下命令安装&#xff1a; pip install pypdf22、合并 PDF 文件的代码&#xff1a; import os from PyPDF2 import PdfMergerdef merge_pdfs(pdf_list, output_path):merge…

一种全新的webapi框架C#webmvc初步介绍

这个框架分三部分&#xff0c;第一部分数据结构层&#xff0c;第二部分http和业务管理以及sql层&#xff0c;第三部分加密层和工具类。 数据结构层分key和数据长度定义 public class Auth { [Key] public string Id { get; set; } [MaxLength(50)…