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

springboot aop实现接口防重复操作

一、前言
有时在项目开发中某些接口逻辑比较复杂,响应时间长,那么可能导致重复提交问题。

二、如何解决
1.先定义一个防重复提交的注解。

import java.lang.annotation.*;@Inherited
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RepeatSubmit {/*** 防重复操作限时标记数值(存储redis限时标记数值)*/String value() default "value" ;/*** 防重复操作过期时间(借助redis实现限时控制)*/int expireSeconds() default 10;
}

2.编写防重复操作的AOP

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.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Objects;@Slf4j
@Component
@Aspect
@Order(0)
public class NoRepeatSubmitAspect  {private static final String TOKENAuthorization = "Authorization";private static final String TOKENUSERNAME = "api-userName";private static final String PREVENT_DUPLICATION_PREFIX = "PREVENT_DUPLICATION_PREFIX:";@Autowiredprivate RedisService redisService;@Pointcut("@annotation(com.dp.aop.annotation.RepeatSubmit)")public void preventDuplication() {}@Around("preventDuplication()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();if (Objects.isNull(request)) {return joinPoint.proceed();}//获取执行方法Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();//获取防重复提交注解RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);//获取token以及方法标记,生成redisKeyString header = request.getHeader(TOKENAuthorization);String token = header == null ? "" : header;String requestHeader = request.getHeader(TOKENUSERNAME);String headerToken = requestHeader == null ? "" : requestHeader;token = token + headerToken;String url = request.getRequestURI();// 通过前缀 + url + token + 函数参数签名 来生成redis上的 keyString redisKey = PREVENT_DUPLICATION_PREFIX.concat(url).concat(token).concat(getMethodSign(method, joinPoint.getArgs()));RedisLock redisLock = null;try {try {redisLock = redisService.tryLock(redisKey, annotation.expireSeconds());} catch (Exception e) {log.error("tryLock error  ", e);throw new BizException(CommonMsgConstants.NoRepeatSubmitMsg);}return joinPoint.proceed();} catch (Throwable throwable) {log.error("throwable trace is ", throwable);throw new RuntimeException(throwable);} finally {if (Objects.nonNull(redisLock)) {redisLock.unlock();}}}/*** 生成方法标记:采用数字签名算法SHA1对方法签名字符串加签** @param method* @param args* @return*/private String getMethodSign(Method method, Object... args) {StringBuilder sb = new StringBuilder(method.toString());for (Object arg : args) {sb.append(toString(arg));}return DigestUtil.sha1Hex(sb.toString());}private String toString(Object arg) {if (Objects.isNull(arg)) {return "null";}if (arg instanceof Number) {return arg.toString();}return JSONObject.toJSONString(arg);}}

3.接下来定义redisService类

@Component
public class RedisService {public RedisLock tryLock(String lockKey, int expireTime) {String lockValue = UUID.randomUUID().toString();Boolean hasLock = (Boolean)this.redisTemplate.execute((connection) -> {Object nativeConnection = connection.getNativeConnection();String status = null;if (nativeConnection instanceof Jedis) {Jedis jedis = (Jedis)nativeConnection;status = jedis.set(lockKey, lockValue, "nx", "ex", expireTime);} else {JedisCluster jedisx = (JedisCluster)nativeConnection;status = jedisx.set(lockKey, lockValue, "nx", "ex", (long)expireTime);}return "OK".equals(status);});if (hasLock) {return new RedisService.RedisLockInner(this.redisTemplate, lockKey, lockValue);} else {throw new RuntimeException("获取锁失败,lockKey:" + lockKey);}}private class RedisLockInner implements RedisLock {private RedisTemplate redisTemplate;private String key;private String expectedValue;protected RedisLockInner(RedisTemplate redisTemplate, String key, String expectedValue) {this.redisTemplate = redisTemplate;this.key = key;this.expectedValue = expectedValue;}public Object unlock() {final List<String> keys = new ArrayList();keys.add(this.key);final List<String> values = new ArrayList();values.add(this.expectedValue);Object result = this.redisTemplate.execute(new RedisCallback<Long>() {public Long doInRedis(RedisConnection connection) throws DataAccessException {Object nativeConnection = connection.getNativeConnection();return nativeConnection instanceof JedisCluster ? (Long)((JedisCluster)nativeConnection).eval("if redis.call('get',KEYS[1])==ARGV[1]\n then\n   return redis.call('del',KEYS[1])\n else\n   return 0\n end", keys, values) : (Long)((Jedis)nativeConnection).eval("if redis.call('get',KEYS[1])==ARGV[1]\n then\n   return redis.call('del',KEYS[1])\n else\n   return 0\n end", keys, values);}});return result;}public void close() throws Exception {this.unlock();}}
}

4.最后在Controller接口加上注解就行了。

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

相关文章:

  • ubuntu18.04复现yolo v8环境配置之CUDA与pytorch版本问题以及多CUDA版本安装及切换
  • Yaml配置文件读取方法
  • Python3 lambda 函数入门示例 Python lambda 函数
  • 【计算机网络】HTTPs 传输流程
  • 【Linux】国产深度系统装机必备(开发、日常使用)
  • 动态规划入门:斐波那契数列模型以及多状态(C++)
  • LeetCode438.找到字符串中所有字母异位词
  • 【微服务】03-HttpClientFactory与gRpc
  • iOS开发之查看静态库(.a/.framework)中包含的.o文件和函数符号(ar,nm命令)
  • Idea常用快捷键--让你代码效率提升一倍(一)
  • 【Open3D】第二篇:GUI编程
  • 【Python】P0 本系列博文简介与大纲
  • FL Studio 21.1.0 Build 3713中文破解免费下载安装激活
  • 从0开始配置eslint
  • Activity 的启动流程(Android 13)
  • deepspeed学习资料
  • 数据分享|R语言PCA主成分、lasso、岭回归降维分析近年来各国土地面积变化影响...
  • Docker-Consul
  • Pygame编程(2)display模块
  • 第十五天|104.二叉树的最大深度、111.二叉树的最小深度、 222.完全二叉树的节点个数
  • 图像识别技术在医疗领域的革命:探索医学影像诊断的未来
  • 计网第四章(网络层)(二)
  • 原生微信小程序使用 wxs;微信小程序使用 vant-weapp组件
  • qml相关知识1
  • linux+c+qt杂记
  • shouldComponentUpdate有什么作用?
  • 华为OD-滑动窗口最大值
  • Linux:ansible自动化运维工具
  • 前端如何使用WebSocket发送消息
  • 纸贵科技连续三年蝉联IDC中国 FinTech 50榜单