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.mzph.cn/news/61419.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vue三级市区联动

默认返回值格式&#xff1a;all:code、name都返回 name:只返回name code:只返回code&#xff0c;level&#xff1a;可设置显示层级 1&#xff1a; 省 2&#xff1a; 省、市 3&#xff1a; 省、市、区 v-model 默认值 可以是 name: [ "天津市", "天津市",…

Debian 30 周年,生日快乐!

导读近日是 Debian 日&#xff0c;也是由伊恩-默多克&#xff08;Ian Murdock&#xff09;创立的 Debian GNU/Linux 通用操作系统和社区支持的 Debian 项目 30 周年纪念日。 不管你信不信&#xff0c;从已故的伊恩-默多克于 1993 年 8 月 16 日宣布成立 Debian 项目&#xff0c…

C# 练习题

26. Enum(枚举) /*枚举是一组命名整型常量。枚举类型是使用 enum 关键字声明的。C# 枚举是值类型。换句话说&#xff0c;枚举包含自己的值&#xff0c;且不能继承或传递继承。 */using System;public class EnumTest {enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat };static…

CentOS 7 Nacos 设置开机自动重启

一、说明 Nacos如果是手动启动的话&#xff0c;在服务器宕机或者重启后&#xff0c;没有自动运行&#xff0c;影响很多业务系统&#xff0c;需要每次手动执行命令 startup.sh -m standalone&#xff0c;才能启动 Nacos 服务&#xff0c;不能像docker服务一样&#xff0c;使用 …

博客系统后端(项目系列2)

目录 前言 &#xff1a; 1.准备工作 1.1创建项目 1.2引入依赖 1.3创建必要的目录 2.数据库设计 2.1博客数据 2.2用户数据 3.封装数据库 3.1封装数据库的连接操作 3.2创建两个表对应的实体类 3.3封装一些必要的增删改查操作 4.前后端交互逻辑的实现 4.1博客列表页 …

【ES】elasticsearch8.3.3

这里仅实践操作并根据实际问题进行记录笔记。 运行 ES8 我们需要在自己的电脑上安装好 Docker Desktop。接着我们运行如下的命令&#xff1a;出现两个异常&#xff0c;一个是需要使用winpty因为我使用win的docker desktop&#xff0c;另外一个问题是docker启动elasticsearchE…

跨屏无界 | ZlongGames 携手 Google Play Games 打造无缝游戏体验

一款经典游戏&#xff0c;会在时间的沉淀中被每一代玩家所怀念&#xff0c;经久不衰。对于紫龙游戏来讲&#xff0c;他们就是这样一群怀揣着创作出经典游戏的初心而聚集在一起的团队&#xff0c;致力于研发出被广大玩家喜爱的作品。 从 2015 年团队成立&#xff0c;到 2019 年走…

Ansible-palybook学习

目录 一.playbook介绍二.playbook格式1.书写格式2.notify介绍 一.playbook介绍 playbook 是 ansible 用于配置&#xff0c;部署&#xff0c;和管理被控节点的剧本。通过 playbook 的详细描述&#xff0c;执行其中的一系列 tasks &#xff0c;可以让远端主机达到预期的状态。pl…

一点思考|漫谈 AI 中的「反馈」机制

前言&#xff1a;生物世界中的正负反馈机制能够促进生物进化&#xff0c;为生物圈的良好生态提供保障。本文探究反馈机制在深度神经网络中的体现&#xff0c;由于笔者知识浅薄&#xff0c;故仅列举个人认知范围内的以下几种「反馈」示例。&#xff08;本文初稿诞生于2022年12月…

Krahets 笔面试精选 88 题——40. 组合总和 II

使用深度搜索的方法&#xff1a; 由于题目说候选数组中的每个数字在每个组合只能出现一次&#xff0c;所以&#xff0c;为了避免重复&#xff0c;在开始之前对候选数组进行升序排序&#xff0c;这样优先选择小的数&#xff0c;如果当前的数都小于目标值&#xff0c;则后面的数就…

find ./* -type d -empty -exec touch {}/.gitkeep \;

这是一个 Linux 下的 find 命令&#xff0c;用于在所有空目录中创建 .gitkeep 文件。让我们来分解一下这个命令做了什么&#xff1a;- find ./* &#xff1a; 在当前目录及其子目录中查找。 -type d &#xff1a; 只查找目录类型的文件。 -empty &#xff1a; 只找出那些空的目…

C# 中操作集合的方法

Add&#xff1a;向集合中添加元素。 List<int> numbers new List<int>(){ 1, 2, 3 }; numbers.Add(4); // numbers 现在为 { 1, 2, 3, 4 }Remove&#xff1a;从集合中移除指定的元素。 List<int> numbers new List<int>(){ 1, 2, 3, 4 }; numbers.Re…

(学习笔记-调度算法)磁盘调度算法

磁盘结构&#xff1a; 常见的机械磁盘是上图左边的样子&#xff0c;中间圆的部分是磁盘的盘片&#xff0c;一般会有多个盘片&#xff0c;每个盘面都有自己的磁头。右边的图就是一个盘片的结构&#xff0c;盘片中的每一层分为多个磁道&#xff0c;每个磁道分为多个扇区&#xff…

MySQL从入门到精通【进阶篇】之 主从复制详解

文章目录 0.前言1. 主从复制简介2. 主从复制的工作流程主从复制过程中的日志文件作用&#xff08;Binary Log&#xff09;和中继日志&#xff08;Relay Log&#xff09; 3. MySQL主从复制的配置4. 参考资料 0.前言 MySQL的主从复制和读写分离是数据库领域的基本概念&#xff0…

Axios中使用CancelToken取消请求

CancelToken 是一个用于取消请求的机制。它允许在请求还未完成时&#xff0c;通过取消请求来终止请求的发送。这在需要在某些情况下中止正在进行的请求时非常有用&#xff0c;比如文件上传时取消上传等。 以下是使用 CancelToken 的一般步骤&#xff1a; 首先&#xff0c;导入…

【LeetCode算法系列题解】第31~35题

CONTENTS LeetCode 31. 下一个排列&#xff08;中等&#xff09;LeetCode 32. 最长有效括号&#xff08;困难&#xff09;LeetCode 33. 搜索旋转排序数组&#xff08;中等&#xff09;LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置&#xff08;中等&#xff09;Lee…

前端vue2、vue3去掉url路由“ # ”号——nginx配置

文章目录 ⭐前言⭐vue2中router默认出现#号&#x1f496;在vue2项目中去掉&#x1f496;在vue3项目中去掉 ⭐vue打包 assetsPublicPath base 为绝对路径 /&#x1f496;vue2 配置 assetsPublicPath&#x1f496;vue3 配置 base&#x1f496;验证 ⭐nginx 配置&#x1f496; 使用…

【第二季】【SpringBoot+Vue】前后端分离项目实战 相关资料

免费资料 资源名称资源访问地址视频地址b站源码gitee笔记笔记

Shell编程之流程控制

目录 if判断 case语句 for循环 while循环 if判断 语法&#xff1a; if [ 条件判断表达式 ] then 程序 elif [ 条件判断表达式 ] then 程序 else 程序 fi 注意&#xff1a; [ 条件判断表达式 ]&#xff0c;中括号和条件判断表达式之间必须有空格。if&#xff0c;elif…

SAP FI之定义财务年和财务年度变式(Fiscal Year Variants)

目录 前言 一、财务年度/财务年度变式 二、使用步骤 1.配置步骤 前言 本文主要介绍SAP会计年度和SAP会计年度变式。 一、财务年度/财务年度变式 财务年度可以具有与日历年相同的期间&#xff0c;也可以不同。中国财政年度从1月到12月&#xff0c;称为历年制&#xff0c;有…