使用Springboot AOP进行请求接口异常监控

news/2025/3/28 21:45:15/

常用注解

@Aspect 切面类
@Before 前置
@AfterReturning 后置
@Around 环绕
@AfterThrowing 异常

切入点设置

execution(public * *(..)) 定义任意公共方法的执行
execution(* set*(..)) 定义任何一个以"set"开始的方法的执行
execution(* com.sys.service.UserService.*(..)) 定义UserService 接口的任意方法的执行
execution(* com.sys.service.*.*(..)) 定义在service包里的任意方法的执行
execution(* com.sys.service ..*.*(..)) 定义在service包和所有子包里的任意类的任意方法的执行
execution(* com.sys.pointcutexp…JoinPointObject.*(…)) 定义在pointcutexp包和所有子包里的JoinPointObject类的任意方法的执行

本次使用aop用途说明

使用@AfterReturning方法拿到请求接口的返回状态码,将需要处理的问题状态码及错误信息保存到数据库及输出到日志文件,方便通过数据库进行接口调用错误信息监控。通过配置切入点对controller的所有请求接口进行处理,就无需再在每一个请求方法中进行处理。
使用@AfterThrowing方法拿到请求接口方法异常调用信息,其他同上。

参考代码

package com.system.common.aop;import com.alibaba.fastjson.JSON;
import com.system.service.SysLogService;
import com.xlx.entity.BaseResponse;
import com.xlx.entity.bsc.system.DO.SystemLogInfoDO;
import com.xlx.utils.ExceptionUtil;
import com.xlx.utils.StringParseUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.Arrays;/*** @version: java version 1.8* @Author: sagitario* @description: 异常请求记录到数据库及日志输出* @date: 2023-04-20 11:44*/
@Component
@Aspect
@Slf4j
public class ApiResponseAOP {@Autowiredprivate SysLogService sysLogService;private static String [] passUrl = {"过滤的请求url"};@Pointcut("execution(* com.*.controller.*.*(..))")private void methodAspect(){}//定义一个切入点@Before("methodAspect()")public void doAudit(JoinPoint joinPoint) {log.info("BeforeAdvice.............");}@AfterReturning(value = "methodAspect()",returning = "methodResult")public void afterReturning(JoinPoint joinPoint, Object methodResult) {MethodSignature ms = (MethodSignature) joinPoint.getSignature();Method method = ms.getMethod();String clazz = method.getDeclaringClass().getName();log.debug("请求类为:" + clazz);String mName = method.getName();log.debug("请求方法为:" + mName);if(Arrays.binarySearch(passUrl, mName)<0){BaseResponse baseResponse = (BaseResponse) methodResult;if(!baseResponse.isSuccess()){StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < joinPoint.getArgs().length; i++) {log.debug("请求参数为:" + JSON.toJSONString(joinPoint.getArgs()[i]));stringBuffer.append(JSON.toJSONString(joinPoint.getArgs()[i]));}log.debug("请求返回内容为:" + methodResult.toString());SystemLogInfoDO systemLogInfoDO = new SystemLogInfoDO();systemLogInfoDO.setInfo(clazz + "." + mName);systemLogInfoDO.setParam(stringBuffer.toString());systemLogInfoDO.setCode(baseResponse.getCode());systemLogInfoDO.setMessage(baseResponse.getMessage());if(baseResponse.getCode()==204){systemLogInfoDO.setLevel("INFO");}else{systemLogInfoDO.setLevel("ERROR");}sysLogService.insertSelective(systemLogInfoDO);}}}@AfterThrowing(value = "methodAspect()",throwing = "e")public void afterThrowing(JoinPoint joinPoint, Exception e) {log.info("AfterThrowing advice");MethodSignature ms = (MethodSignature) joinPoint.getSignature();Method method = ms.getMethod();String clazz = method.getDeclaringClass().getName();log.info("请求类为:" + clazz);String mName = method.getName();log.info("请求方法为:" + mName);SystemLogInfoDO systemLogInfoDO = new SystemLogInfoDO();try {StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < joinPoint.getArgs().length; i++) {log.info("请求参数为:" + JSON.toJSONString(joinPoint.getArgs()[i]));stringBuffer.append(JSON.toJSONString(joinPoint.getArgs()[i]));}systemLogInfoDO.setParam(stringBuffer.toString());}catch (Exception e1){log.info("请求方法参数无法解析:" + ExceptionUtil.getMessage(e1));systemLogInfoDO.setParam("请求方法参数无法解析");}String error = ExceptionUtil.getMessage(e);log.info("afterThrowing异常:{}",error);systemLogInfoDO.setInfo(clazz + "." + mName);systemLogInfoDO.setCode(500);systemLogInfoDO.setMessage(error);systemLogInfoDO.setLevel("EXCEPTION");sysLogService.insertSelective(systemLogInfoDO);}}

日志输出

2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:82] INFO  com.loan.common.aop.ApiResponseAOP - 请求类为:com.loan.controller.RepayController
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:84] INFO  com.loan.common.aop.ApiResponseAOP - 请求方法为:repaymentRecord
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:89] INFO  com.loan.common.aop.ApiResponseAOP - 请求参数为:"110014"
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.xlx.exception.GlobalExceptionHandler:26] ERROR com.xlx.exception.GlobalExceptionHandler - feign.RetryableException: Read timed out executing GET http://external-platform-server/repay/repaymentRecordsat feign.FeignException.errorExecuting(FeignException.java:213)at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:115)......
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:98] INFO  com.loan.common.aop.ApiResponseAOP - afterThrowing异常:feign.RetryableException: Read timed out executing GET http://external-platform-server/repay/repaymentRecordsat feign.FeignException.errorExecuting(FeignException.java:213)at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:115)at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:80)at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)

其他

JDK动态代理相关可参考 一篇文章彻底搞懂java AOP、@Before、@After、@AfterReturning、@AfterThrowing、@Around的使用、Spring AOP详解


http://www.ppmy.cn/news/149171.html

相关文章

VS2013过期激活,VS2013激活,vs2013序列号,VS2013密钥,VS013产品密匙

vs2013认证许可已过期 vs2013旗舰版密匙 VS2013 ultimate key VS2013激活码 序列号&#xff0c;产品密钥&#xff0c;产品密匙&#xff0c;验证码&#xff0c;vs2013旗舰版破解&#xff1a;破解密匙&#xff0c;破解序列号&#xff1a; 然后点击"更改我的产品许可证"…

激活office2013

office2013 激活教程及激活工具 本文转自http://jingyan.baidu.com/article/d45ad148e6bdfd69552b80b8.html 首先下载激活工具,百度一下 microsoft toolkit 2.4.1 注意:Office2013不支持win XP系统,仅支持win 7和win 8。 打开激活工具后,相信激活过Office 2010的用户非常熟悉了…

VS2015产品激活密钥

专业版&#xff1a;HMGNV-WCYXV-X7G9W-YCX63-B98R2 企业版&#xff1a;HM6NR-QXX7C-DFW2Y-8B82K-WTYJV

VS2013 产品密钥 – 所有版本

VS2013 产品密钥 – 所有版本 Visual Studio Ultimate 2013 KEY&#xff08;密钥&#xff09;&#xff1a;BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY&#xff08;密钥&#xff09;&#xff1a;FBJVC-3CMTX-D8DVP-RTQCT-92494 Visual Studio Professiona…

office2016更换密钥的方法

今天装了office2016个人零售版&#xff0c;激活的时候在网上找了好多密钥&#xff0c;输入第一个密钥无法激活&#xff0c;想更换密钥却发现更换不了&#xff0c;如下图所示&#xff0c;没有这个选项。 查了下解决办法&#xff0c;发现下面这个方法好用。 在桌面新建一个文本文…

Visual Studio 2015、2013、2012、2010、2008、2005各版本下载+有效密钥激活

&#xff36;isual Studio是微软发布的一个集成开发工具&#xff0c;业内一般简称为VS&#xff0c;广泛应用于Windows软件开发、网站开发等&#xff0c;是目前十分流行的windows应用程序的集成开发工具&#xff0c;如果大家不了解&#xff0c;可以简单的认为它就是一个开发工具…

visio2013专业版激活密匙

Visio 2013最新产品密钥分享&#xff0c;在安装时可以使用以下密钥&#xff1a; 2NYF6-QG2CY-9F8XC-GWMBW-29VV8 FJ2N7-W8TXC-JB8KB-DCQ7Q-7T7V3 VXX6C-DN3HQ-3CRXG-RF4KT-YG7V3 B3C7Q-D6NH2-2VRFW-HHWDG-FVQB6 TCWJK-N6GFH-82BP9-HV7YQ-T6KMQ KD8CP-DN968-RGQM6-PMFMK-M96YQ Q…

VS 2013 产品密钥

VS 2013 产品密钥 Visual Studio Ultimate 2013 KEY&#xff08;密钥&#xff09;&#xff1a;BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY&#xff08;密钥&#xff09;&#xff1a;FBJVC-3CMTX-D8DVP-RTQCT-92494 Visual Studio Professional 2013 KEY&a…