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

分布式部署下如何做接口防抖---使用分布式锁

        防抖也即防重复提交,那么如何确定两次接口就是重复的呢?首先,我们需要给这两次接口的调用加一个时间间隔,大于这个时间间隔的一定不是重复提交;其次,两次请求提交的参数比对,不一定要全部参数,选择标识性强的参数即可(生产环境还可以加上用户ID);最后,如果想做的更好一点,还可以加一个请求地址的对比。

        分布式部署下接口防抖有有很多方法,如:使用共享缓存,使用分布式锁,在web开发中一般新增后者

        思路如下:

1. 自定义注解@RequestLock,用于标记需要防抖的接口方法,记录锁的一些信息如:锁过期时间、时间单位等。

2. 自定义@RequestKeyParam注解用于指定生成唯一键的参数(生成锁名的依据)。

3. 封装RequestKeyGenerator 类定义锁的生成逻辑,返回生成的锁名。

4. 实现一个切入点为添加了@RequestLock注解的方法的环绕通知,通知的内容是:生成锁名key, 获取目标对象的的@RequestLock携带的锁过期作为锁名key存入Redis的TTL。

5. 在需要增强的方法上添加@RequestLock,用于加锁的参数上添加@RequestKeyParam。

实现过程:

@RequestLock

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLock {String prefix() default "";long expire() default 5; // 锁过期时间,单位:秒String timeUnit() default "SECONDS";String delimiter() default "&";
}

@RequestKeyParam

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** @description 加上这个注解可以将参数设置为key*/
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RequestKeyParam {}

RequestKeyGenerator 类( 由于 @RequestKeyParam 可以放在方法的参数上,也可以放在对象的属性上,所以这里需要进行两次判断,一次是获取方法上的注解,一次是获取对象里面属性上的注解。)

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;public class RequestKeyGenerator {/*** 获取LockKey** @param joinPoint 切入点* @return*/public static String getLockKey(ProceedingJoinPoint joinPoint) {//获取连接点的方法签名对象MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();//Method对象Method method = methodSignature.getMethod();//获取Method对象上的注解对象RequestLock requestLock = method.getAnnotation(RequestLock.class);//获取方法参数final Object[] args = joinPoint.getArgs();//获取Method对象上所有的注解final Parameter[] parameters = method.getParameters();StringBuilder sb = new StringBuilder();for (int i = 0; i < parameters.length; i++) {final RequestKeyParam keyParam = parameters[i].getAnnotation(RequestKeyParam.class);//如果属性不是RequestKeyParam注解,则不处理if (keyParam == null) {continue;}//如果属性是RequestKeyParam注解,则拼接 连接符 "& + RequestKeyParam"sb.append(requestLock.delimiter()).append(args[i]);}//如果方法上没有加RequestKeyParam注解if (StringUtils.isEmpty(sb.toString())) {//获取方法上的多个注解(为什么是两层数组:因为第二层数组是只有一个元素的数组)final Annotation[][] parameterAnnotations = method.getParameterAnnotations();//循环注解for (int i = 0; i < parameterAnnotations.length; i++) {final Object object = args[i];//获取注解类中所有的属性字段final Field[] fields = object.getClass().getDeclaredFields();for (Field field : fields) {//判断字段上是否有RequestKeyParam注解final RequestKeyParam annotation = field.getAnnotation(RequestKeyParam.class);//如果没有,跳过if (annotation == null) {continue;}//如果有,设置Accessible为true(为true时可以使用反射访问私有变量,否则不能访问私有变量)field.setAccessible(true);//如果属性是RequestKeyParam注解,则拼接 连接符" & + RequestKeyParam"sb.append(requestLock.delimiter()).append(ReflectionUtils.getField(field, object));}}}//返回指定前缀的keyreturn requestLock.prefix() + sb;}
}

Redis

import java.lang.reflect.Method;
import com.summo.demo.exception.biz.BizException;
import com.summo.demo.model.response.ResponseCodeEnum;
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.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.util.StringUtils;/*** @description 缓存实现*/
@Aspect
@Configuration
@Order(2)
public class RedisRequestLockAspect {private final StringRedisTemplate stringRedisTemplate;@Autowiredpublic RedisRequestLockAspect(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}@Around("execution(public * * (..)) && @annotation(com.summo.demo.config.requestlock.RequestLock)")public Object interceptor(ProceedingJoinPoint joinPoint) {MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();Method method = methodSignature.getMethod();RequestLock requestLock = method.getAnnotation(RequestLock.class);if (StringUtils.isEmpty(requestLock.prefix())) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "重复提交前缀不能为空");}//获取自定义keyfinal String lockKey = RequestKeyGenerator.getLockKey(joinPoint);// 使用RedisCallback接口执行set命令,设置锁键;设置额外选项:过期时间和SET_IF_ABSENT选项final Boolean success = stringRedisTemplate.execute((RedisCallback<Boolean>)connection -> connection.set(lockKey.getBytes(), new byte[0],Expiration.from(requestLock.expire(), requestLock.timeUnit()),RedisStringCommands.SetOption.SET_IF_ABSENT));if (!success) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "您的操作太快了,请稍后重试");}try {return joinPoint.proceed();} catch (Throwable throwable) {throw new BizException(ResponseCodeEnum.BIZ_CHECK_FAIL, "系统异常");}}
}

 接口方法和实体类

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;@RestController
public class UserController {@PostMapping("/add")@RequestLock(prefix = "user_add_")public ResponseEntity<String> addUser(@RequestBody AddUserRequest addUserRequest) {// 这里假设调用服务层方法添加用户,实际需注入 userService 并确保其有 add 方法// 示例中先注释掉,实际使用要补充服务层调用逻辑// userService.add(addUserRequest);return ResponseEntity.ok("添加用户成功");}
}import lombok.Data;@Data
public class AddUserRequest {@RequestKeyParamprivate String userName;@RequestKeyParamprivate String userPhone;// 其他字段...
}


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

相关文章:

  • macOS 26正式发布,全新Liquid Glass设计语言亮相
  • 旅游管理实训室:支撑实践教学的核心载体
  • 5118 API智能处理采集数据教程
  • 项目——视频共享系统测试
  • 【C++】状态模式
  • GitHub 解码指南:用 AI 赋能,五步快速掌握任意开源项目
  • MySQL 8.0 OCP 1Z0-908 题目解析(20)
  • MVC 架构设计模式
  • 【Linux仓库】进程优先级及进程调度【进程·肆】
  • 小黑黑日常积累大模型prompt句式2:【以段落的形式输出,不分点列举】【如果没有相关内容则不输出】【可读性强】【输出格式规范】
  • Java学习第八部分——泛型
  • git 中删除提交历史
  • 代码随想录算法训练营第四十五天|动态规划part12
  • Fiddler中文版抓包工具在后端API调试与Mock中的巧用
  • 应用在核电行业的虚拟现实解决方案
  • Laravel8中调取腾讯云文字识别OCR
  • 【前端开发】Uniapp分页器:新增输入框跳转功能
  • SpringCloud系列(49)--SpringCloud Stream消息驱动之实现生产者
  • Rubber Band Algorithm 应力及反作用力测试
  • 运维打铁: 企业运维开发痛点之解决方案
  • ModuleNotFoundError: No module named ‘onnxruntime‘
  • 【免费.NET方案】CSV到PDF与DataTable的快速转换
  • 图论基础算法入门笔记
  • MySQL 8.0 OCP 1Z0-908 题目解析(18)
  • 深度学习2(逻辑回归+损失函数+梯度下降)
  • 在 VSCode 中高效配置自定义注释模板 (无需插件)
  • Python 中如何使用 Conda 管理版本和创建 Django 项目
  • Flowable多引擎架构搭建方案
  • 车载以太网-IP 掩码 vlan 端口
  • 前端的一些报错