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

Vue3+Spring boot 前后端防抖增强方案

我将基于您提供的防抖切面代码进行改进,主要优化点包括使用Redis实现分布式防抖、增强键生成策略、改进异常处理等。以下是完整的改进方案:

1. 改进的防抖注解 (Debounce.java)

java

复制

下载

package com.weiyu.anno;import java.lang.annotation.*;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Debounce {/*** 防抖时间(毫秒),默认5秒*/long value() default 5000;/*** 自定义键值(支持SpEL表达式)*/String key() default "";/*** 防抖键类型*/KeyType keyType() default KeyType.METHOD;/*** 错误提示信息*/String message() default "操作过于频繁,请稍后再试";enum KeyType {METHOD,     // 方法级别防抖(默认)USER,       // 用户级别防抖(需要用户上下文)IP,         // IP级别防抖PARAM,      // 参数级别防抖(基于参数值)CUSTOM      // 完全自定义键}
}

2. 改进的防抖工具类 (DebounceUtil.java)

java

复制

下载

package com.weiyu.utils;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;import java.util.concurrent.TimeUnit;@Component
public class DebounceUtil {@Autowiredprivate StringRedisTemplate redisTemplate;private static final ExpressionParser parser = new SpelExpressionParser();/*** 检查并设置防抖锁* @param key 防抖键* @param debounceTime 防抖时间(毫秒)* @return 是否允许操作(true:允许,false:防抖中)*/public boolean checkAndSet(String key, long debounceTime) {// 使用SETNX+EXPIRE原子操作Boolean result = redisTemplate.opsForValue().setIfAbsent(key, "1", debounceTime, TimeUnit.MILLISECONDS);return result != null && result;}/*** 生成防抖键* @param joinPoint 切点* @param keyType 键类型* @param customKey 自定义键表达式* @return 生成的防抖键*/public String generateKey(ProceedingJoinPoint joinPoint, Debounce.KeyType keyType, String customKey) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();String methodName = signature.getDeclaringTypeName() + "#" + signature.getName();// 1. 处理自定义SpEL表达式if (keyType == Debounce.KeyType.CUSTOM && StringUtils.hasText(customKey)) {return evaluateSpEL(joinPoint, customKey);}// 2. 处理其他键类型switch (keyType) {case USER:return "DEBOUNCE:USER:" + getCurrentUserId() + ":" + methodName;case IP:return "DEBOUNCE:IP:" + getClientIp() + ":" + methodName;case PARAM:return "DEBOUNCE:PARAM:" + methodName + ":" + Arrays.hashCode(joinPoint.getArgs());case CUSTOM: // 已处理,此处为fallbackcase METHOD:default:return "DEBOUNCE:METHOD:" + methodName;}}/*** 解析SpEL表达式*/private String evaluateSpEL(ProceedingJoinPoint joinPoint, String expression) {try {StandardEvaluationContext context = new StandardEvaluationContext();// 设置方法参数context.setVariable("args", joinPoint.getArgs());// 设置方法签名context.setVariable("method", joinPoint.getSignature());// 设置目标对象context.setVariable("target", joinPoint.getTarget());Expression exp = parser.parseExpression(expression);return "DEBOUNCE:CUSTOM:" + exp.getValue(context, String.class);} catch (Exception e) {throw new IllegalArgumentException("Invalid SpEL expression: " + expression, e);}}/*** 获取当前用户ID(需要根据实际认证系统实现)*/private String getCurrentUserId() {// 示例:从Spring Security上下文中获取Authentication authentication = SecurityContextHolder.getContext().getAuthentication();if (authentication != null && authentication.isAuthenticated()) {return authentication.getName();}return "ANONYMOUS";}/*** 获取客户端IP*/private String getClientIp() {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();String ip = request.getHeader("X-Forwarded-For");if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return ip;}
}

3. 改进的防抖切面 (DebounceAspect.java)

java

复制

下载

package com.weiyu.aop;import com.weiyu.anno.Debounce;
import com.weiyu.exception.DebounceException;
import com.weiyu.utils.DebounceUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;import java.lang.reflect.Method;/*** 改进的防抖切面(支持Redis分布式锁)*/
@Aspect
@Component
public class DebounceAspect {@Autowiredprivate DebounceUtil debounceUtil;@Around("@annotation(debounceAnnotation)")public Object debounce(ProceedingJoinPoint joinPoint, Debounce debounceAnnotation) throws Throwable {// 获取方法上的注解(处理继承情况)if (debounceAnnotation == null) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();debounceAnnotation = AnnotationUtils.findAnnotation(method, Debounce.class);if (debounceAnnotation == null) {return joinPoint.proceed();}}// 生成防抖键String key = debounceUtil.generateKey(joinPoint,debounceAnnotation.keyType(),debounceAnnotation.key());// 获取防抖时间long debounceTime = debounceAnnotation.value();// 检查并设置防抖锁if (!debounceUtil.checkAndSet(key, debounceTime)) {throw new DebounceException(debounceAnnotation.message());}// 执行目标方法try {return joinPoint.proceed();} catch (Throwable throwable) {// 异常时移除防抖锁(可选)// debounceUtil.removeLock(key);throw throwable;}}
}

4. 自定义防抖异常 (DebounceException.java)

java

复制

下载

package com.weiyu.exception;/*** 防抖业务异常*/
public class DebounceException extends RuntimeException {private int code = 429; // 使用429状态码表示操作过于频繁public DebounceException(String message) {super(message);}public int getCode() {return code;}
}

5. 全局异常处理器 (GlobalExceptionHandler.java)

java

复制

下载

package com.weiyu.exception;import com.weiyu.common.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;@RestControllerAdvice
public class GlobalExceptionHandler {/*** 处理防抖异常*/@ExceptionHandler(DebounceException.class)public Result handleDebounceException(DebounceException e) {return Result.error(e.getCode(), e.getMessage());}
}

6. 统一响应对象 (Result.java)

java

复制

下载

package com.weiyu.common;public class Result<T> {private int code;private String msg;private T data;// 成功静态方法public static <T> Result<T> success(T data) {Result<T> result = new Result<>();result.setCode(200);result.setMsg("操作成功");result.setData(data);return result;}// 错误静态方法public static <T> Result<T> error(int code, String msg) {Result<T> result = new Result<>();result.setCode(code);result.setMsg(msg);return result;}// Getters and Setters
}

7. 使用示例

java

复制

下载

@RestController
@RequestMapping("/api/download")
public class DownloadController {// 基本方法级别防抖(5秒内只能调用一次)@Debounce@GetMapping("/basic")public Result<String> basicDownload() {// ... 下载逻辑return Result.success("下载成功");}// 用户级别防抖(每个用户5秒内只能下载一次)@Debounce(keyType = Debounce.KeyType.USER, value = 5000)@GetMapping("/user")public Result<String> userSpecificDownload() {// ... 下载逻辑return Result.success("下载成功");}// 自定义键防抖(基于文件ID)@Debounce(keyType = Debounce.KeyType.CUSTOM,key = "'FILE:' + #fileId", // SpEL表达式message = "该文件正在下载中,请稍后再试")@GetMapping("/file/{fileId}")public Result<String> fileDownload(@PathVariable String fileId) {// ... 下载逻辑return Result.success("下载成功");}// IP级别防抖(每个IP 10秒内只能下载一次)@Debounce(keyType = Debounce.KeyType.IP, value = 10000)@GetMapping("/ip-based")public Result<String> ipBasedDownload() {// ... 下载逻辑return Result.success("下载成功");}
}

8. 前端适配方案

javascript

复制

下载

// 在响应拦截器中处理防抖异常
instance.interceptors.response.use(response => {// ... 正常处理},error => {if (error.response) {// 识别防抖异常(HTTP 429状态码)if (error.response.status === 429) {ElMessage.error(error.response.data?.msg || "操作过于频繁,请稍后再试");return Promise.reject(error);}// 识别业务层防抖异常(code=429)const res = error.response.data;if (res && res.code === 429) {ElMessage.error(res.msg || "操作过于频繁,请稍后再试");return Promise.reject(error);}}// ... 其他错误处理}
);

改进亮点

  1. 分布式支持

    • 使用Redis实现分布式防抖锁

    • 原子操作(SETNX + EXPIRE)确保并发安全

  2. 灵活的键策略

    • 支持方法、用户、IP、参数、自定义五种键类型

    • 集成SpEL表达式引擎实现动态键生成

  3. 增强的异常处理

    • 自定义防抖异常类型(DebounceException)

    • 统一使用429状态码(Too Many Requests)

    • 可自定义错误消息

  4. 注解增强

    • 支持方法继承的注解查找

    • 可配置防抖时间和错误消息

    • 支持SpEL表达式自定义键

  5. 集成Spring生态

    • 自动获取请求上下文(IP、用户信息)

    • 通过全局异常处理器统一处理

  6. 更精确的防抖

    • 精确到毫秒级别的防抖控制

    • 避免本地内存防抖的分布式问题

这个改进方案保持了原有代码的结构和风格,同时增加了企业级应用所需的分布式支持和灵活性,特别适合在微服务架构中使用。

http://www.lryc.cn/news/574819.html

相关文章:

  • 学习记录:DAY33
  • 2025年渗透测试面试题总结-2025年HW(护网面试) 09(题目+回答)
  • HarmonyOS开发基础 --面向鸿蒙的TypeScript基础语法一文入门
  • 大模型本地部署,拥有属于自己的ChatGpt
  • 《仿盒马》app开发技术分享-- 兑换列表展示(68)
  • OSS安全合规实战:金融行业敏感数据加密+KMS自动轮转策略(满足等保2.0三级要求)
  • 如何使用MQTTX软件来进行MQTT协议的测试
  • # Python中等于号的使用
  • 逆向入门(7)汇编篇-mul指令的学习
  • DAY 41 简单CNN
  • 防御OSS Bucket泄露:RAM权限策略+日志审计+敏感数据扫描三重防护
  • DeepSeek智能总结 | 邓紫棋音乐版权纠纷核心梳理
  • 软件工程:从理论到实践,构建可靠软件的艺术与科学
  • 智慧家政数字化小程序开发:重构行业服务生态的创新引擎
  • 代码随想录|图论|01图论基础
  • 医药企业CMO研发管线管理专项介绍
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | MovieApp(电影卡片组件)
  • ArkTS与仓颉开发语言:鸿蒙编程的双子星
  • day41
  • 深入理解 BOM:浏览器对象模型详解
  • IoTDB的基本概念及常用命令
  • 【css】增强 CSS 的复用性与灵活性的Mixins
  • ArkUI-X通过Stage模型开发Android端应用指南(二)
  • 【软考高级系统架构论文】### 论软件系统架构评估
  • linux grep的一些坑
  • 接口自动化测试之 pytest 接口关联框架封装
  • Unity_导航操作(鼠标控制人物移动)_运动动画
  • matplotilb实现对MACD的实战
  • SQL关键字三分钟入门:UPDATE —— 修改数据
  • Camera Sensor接口协议全解析(五)SLVS-EC接口深度解析