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

05_项目集成飞书预警

05_项目集成飞书预警

一、推送信息格式:

在这里插入图片描述

二、切面类及请求上下文信息类

请求上下文信息类:

/*** @desc: 请求上下文信息类* @author: sqnugy* @date: 2025/5/8**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApiContext {private String methodName;private String className;private String argsJson;
}

请求上下文信息保存类(通过 ThreadLocal 保存)

/*** @desc: ThreadLocal 存储请求上下文信息(ApiContext)* @author: sqnugy* @date: 2025/5/8**/
public class ApiContextHolder {private static final ThreadLocal<ApiContext> contextHolder = new ThreadLocal<>();public static void set(ApiContext context) {contextHolder.set(context);}public static ApiContext get() {return contextHolder.get();}public static void clear() {contextHolder.remove();}
}

ApiOperation 注解切面类:

import cn.jcs.boot.video.review.util.JsonUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
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.springframework.stereotype.Component;
import org.slf4j.MDC;import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;/*** @desc: ApiOperation 注解的切面类,通过 ApiOperaton 注解进行织入切面,并将获取到的请求的类名、方法名、入参 保存到上下文中* @author: sqnugy* @date: 2025/5/8**/
@Aspect
@Component
@Slf4j
public class ApiOperationAspect {/** 以 @ApiOperation 注解为切点,凡是添加 @ApiOperation 的方法,都会执行环绕中的代码 */@Pointcut("@annotation(io.swagger.annotations.ApiOperation)")public void apiOperation() {}/*** 环绕* @param joinPoint* @return* @throws Throwable*/@Around("apiOperation()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {try {long startTime = System.currentTimeMillis();MDC.put("traceId", UUID.randomUUID().toString());String className = joinPoint.getTarget().getClass().getSimpleName();String methodName = joinPoint.getSignature().getName();Object[] args = joinPoint.getArgs();String argsJsonStr = Arrays.stream(args).map(toJsonStr()).collect(Collectors.joining(", "));String description = getApiOperationDescription(joinPoint);log.info("====== 请求开始: [{}], 入参: {}, 请求类: {}, 请求方法: {} =================================== ",description, argsJsonStr, className, methodName);// 保存上下文信息ApiContext apiContext = new ApiContext();apiContext.setClassName(className);apiContext.setMethodName(methodName);apiContext.setArgsJson(argsJsonStr);//将上下文信息保存到 ThreadLocal 内ApiContextHolder.set(apiContext);Object result = joinPoint.proceed();long executionTime = System.currentTimeMillis() - startTime;String resultJson = JsonUtils.toJsonString(result);log.info("====== 请求结束: [{}], 耗时: {}ms, 出参: {} =================================== ",description, executionTime, resultJson);return result;} finally {MDC.clear();}}/*** 获取注解的描述信息* @param joinPoint* @return*/private String getApiOperationDescription(ProceedingJoinPoint joinPoint) {// 1. 从 ProceedingJoinPoint 获取 MethodSignatureMethodSignature signature = (MethodSignature) joinPoint.getSignature();// 2. 使用 MethodSignature 获取当前被注解的 MethodMethod method = signature.getMethod();// 3. 从 Method 中提取 LogExecution 注解ApiOperation apiOperatiog = method.getAnnotation(ApiOperation.class);// 4. 从 LogExecution 注解中获取 description 属性return apiOperatiog.value();}/*** 转 JSON 字符串* @return*/private Function<Object, String> toJsonStr() {return arg -> JsonUtils.toJsonString(arg);}}

三、全局异常处理类

import cn.hutool.Hutool;
import cn.hutool.json.JSONUtil;
import cn.jcs.boot.video.review.common.CommonResult;
import cn.jcs.boot.video.review.common.aspect.ApiContext;
import cn.jcs.boot.video.review.common.aspect.ApiContextHolder;
import cn.jcs.boot.video.review.util.FeishuUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.method.HandlerMethod;import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Parameter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @author sqnugy* @version 1.0* @desc 全局的异常处理类* @date 2025/5/7 19:49*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {/*** 业务异常处理** @param e 捕捉的异常* @return*/@ExceptionHandler(BusinessException.class)public CommonResult<?> handlerMallServiceException(BusinessException e) {log.error("业务异常", e);IErrorCode errorCode = e.getErrorCode();if (errorCode != null) {return CommonResult.failed(errorCode);}e.printStackTrace();try {notifyFeishu(e, "业务异常");} catch (Exception e1) {e1.printStackTrace();}return CommonResult.failed(e.getMessage());}/*** 表单校验** @param e 参数校验异常* @return*/@ExceptionHandler(MethodArgumentNotValidException.class)public CommonResult<?> handlerValidException(MethodArgumentNotValidException e) {log.error("表单校验异常", e);BindingResult bindingResult = e.getBindingResult();List<ObjectError> allErrors = bindingResult.getAllErrors();Map<String, String> message = new HashMap<>();for (ObjectError error : allErrors) {FieldError fe = (FieldError) error;message.put(fe.getField(), error.getDefaultMessage());}FeishuUtil.send(e.getMessage());try {notifyFeishu(e, "参数校验异常");} catch (Exception e1) {e1.printStackTrace();}return CommonResult.validateFailed("表单数据错误", message);}/*** 全局异常处理** @param e* @param handlerMethod* @return*/@ExceptionHandler(Exception.class)public CommonResult<?> handlerException(Exception e, HandlerMethod handlerMethod) {log.error("报错方法名字 [{}]", handlerMethod.getMethod().getName(), e);try {notifyFeishu(e, handlerMethod);} catch (Exception e1) {e1.printStackTrace();}return CommonResult.failed("系统异常,请稍后重试");}private String getStackTraceAsString(Throwable throwable) {StringWriter sw = new StringWriter();PrintWriter pw = new PrintWriter(sw);throwable.printStackTrace(pw);return sw.toString();}private void notifyFeishu(Exception e, String title) {try {ApiContext context = ApiContextHolder.get();String methodName = context != null ? context.getMethodName() : "未知方法";String className = context != null ? context.getClassName() : "未知类";String argsJson = context != null ? context.getArgsJson() : "无入参";String stackTrace = getStackTraceAsString(e);String message = String.format("🚨 %s 异常告警\n" +"📍 类名:%s\n" +"🔧 方法:%s\n" +"📥 入参:%s\n" +"🧵 异常信息:%s\n" +"📄 堆栈信息:\n%s",title,className,methodName,argsJson,e.getMessage(),stackTrace);FeishuUtil.send(message);} catch (Exception innerEx) {innerEx.printStackTrace();} finally {ApiContextHolder.clear();}}private void notifyFeishu(Exception e, HandlerMethod handlerMethod) {try {ApiContext context = ApiContextHolder.get();String params = context != null ? context.getArgsJson() : "获取失败";String stackTrace = getStackTraceAsString(e);String message = String.format("❗ 全局异常 异常告警\n📌 方法:%s.%s\n📥 入参:%s\n🧵 异常堆栈:\n%s",context != null ? context.getClassName() : "未知类",handlerMethod.getMethod().getName(),params,stackTrace);FeishuUtil.send(message);} catch (Exception innerEx) {innerEx.printStackTrace();} finally {ApiContextHolder.clear(); // 避免内存泄漏}}}

四、飞书通知工具类

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;import java.util.HashMap;
import java.util.Map;/*** @author sqnugy* @Date 2025/5/7* @description:*/
public class FeishuUtil {private final static String URL = "https://open.feishu.cn/open-apis/bot/v2/hook/e6eb4bab-5exxx"; #机器人链接public static void send(String feishuMsg){Map<String, Object> params = new HashMap<>();params.put("msg_type", "text");Map<String, Object> data = new HashMap<>();data.put("text", feishuMsg);params.put("content", data);String body = JSON.toJSONString(params);String result = HttpUtil.post(URL, body);}}
d(String feishuMsg){Map<String, Object> params = new HashMap<>();params.put("msg_type", "text");Map<String, Object> data = new HashMap<>();data.put("text", feishuMsg);params.put("content", data);String body = JSON.toJSONString(params);String result = HttpUtil.post(URL, body);}}
http://www.xdnf.cn/news/342307.html

相关文章:

  • 浙大与哈佛联合开源图像编辑模型IC-Edit,实现高效、精准、快速的指令编辑~
  • 淘宝九宫格验证码识别
  • 【UltralyticsYolo11图像分类完整项目-02】onnx模型转engine格式+TensorRT版Gpu预测C++实现
  • 动态规划之两个数组的dp问题(最长公共子序列)
  • Unity图集系统(Sprite Atlas)
  • Vue实现不同网站之间的Cookie共享功能
  • 信息系统项目管理工程师备考计算类真题讲解十四
  • 【软件设计师:软件工程】9.软件开发模型与方法
  • Java三大基本特征之多态
  • auto_ptr和unique_ptr
  • 统一授权与加密防护,CodeMeter 护航机器视觉创新全链路
  • kafka logs storage
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(16):单词与句子
  • Element-ui Table tree 结构使用(解决无展开箭头)
  • (14)Element Plus项目综合案例
  • 基础算法系列——树的入门
  • kafka records deletion policy
  • 如何设置内网映射端口到外网访问?哪些软件可以进行端口映射?
  • 2025.05.07-携程春招笔试第二题
  • flutter build apk出现的一些奇怪的编译错误
  • K8s网络从0到1
  • 《易语言学习大全》
  • k8s术语之DaemonSet
  • [python] 函数基础
  • 深入解析asyncio的实现与应用
  • C#简易Modbus从站仿真器
  • 如何将 Build at、Hash 和 Time git 的 Tag 号等构建信息,自动写入一个 JSON 文件
  • sql serve 多表联合查询,根据一个表字段值动态改变查询条件
  • 【Dify系列教程重置精品版】第七章:在Dify对话中显示本地图片之FastAPI与Uvicorn
  • PCL点云按指定方向进行聚类(指定类的宽度)