开发中,经常遇到重复提交表单问题,前端响应慢,鼠标快速点了几次,导致后台插入了两条重复的数据,尽管生成的主键id不一样,但在业务上任然属于重复数据,造成业务数据混乱。所以有必要就这个问题研究下解决方案。当然只有增删改的操作需要考虑防重复提交问题。
org.springframework.boot spring-boot-starter-data-redis org.aspectj aspectjweaver org.springframework.boot spring-boot-starter-aop
要用到redis和aspect,所以引入上述依赖
package com.example.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.*; @SpringBootConfiguration public class RedisConfig { @Bean public RedisTemplateredisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); //创建一个json的序列化对象 GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); //设置value的序列化方式json redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); //设置key序列化方式String redisTemplate.setKeySerializer(new StringRedisSerializer()); //设置hash key序列化方式String redisTemplate.setHashKeySerializer(new StringRedisSerializer()); //设置hash value序列化json redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // 设置支持事务 redisTemplate.setEnableTransactionSupport(true); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public RedisSerializer
package com.example.utils; import org.springframework.data.redis.core.*; import org.springframework.stereotype.Component; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @Component @SuppressWarnings({"unchecked", "rawtypes"}) public class RedisUtils { private static final Logger logger = Logger.getLogger(RedisUtils.class.getSimpleName()); private final RedisTemplate redisTemplate; public RedisUtils(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } /** * 根据key 获取过期时间 * * @param key 键 不能为null * @return 时间(秒) 返回 0 代表为永久有效,-2 代表键不存在 */ publiclong getExpireTime(K key) { Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS); if (expire != null) { return expire; } return -2; } /** * 指定缓存失效时间 * * @param key 键 * @param expireTime 时间(秒) */ public void setExpireTime(K key, long expireTime) { try { if (expireTime > 0) { redisTemplate.expire(key, expireTime, TimeUnit.SECONDS); } } catch (Exception e) { logger.log(Level.SEVERE,e.getMessage()); } } /** * 移除指定 key 的过期时间 * * @param key 键 */ public void removeExpireTime(K key) { redisTemplate.boundValueOps(key).persist(); } /** * 获取缓存中所有的键 * * @param key 键 * @return 缓存中所有的键 */ public Set keys(K key) { return redisTemplate.keys(key); } /** * 判断key是否存在 * * @param key 键 * @return true 存在 false不存在 */ public boolean hasKey(K key) { try { return Boolean.TRUE.equals(redisTemplate.hasKey(key)); } catch (Exception e) { logger.log(Level.SEVERE,e.getMessage()); return false; } } /** * 根据key删除缓 * * @param keys 键 */ public void delete(Collection keys) { redisTemplate.delete(keys); } /** * 设置分布式锁 * * @param key 键,可以用用户主键 * @param value 值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性 * @param expire 锁的时间 * @return 设置成功为 true */ public Boolean setNx(K key, V value, long expire) { return this.setNx(key, value, expire, TimeUnit.SECONDS); } /** * 设置分布式锁 * * @param key 键,可以用用户主键 * @param value 值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性 * @param expire 锁的时间 * @param timeUnit 时间单位 * @return 设置成功为 true */ public Boolean setNx(K key, V value, long expire,TimeUnit timeUnit) { return redisTemplate.opsForValue().setIfAbsent(key, value, expire, timeUnit); } /** * 设置分布式锁,有等待时间 * * @param key 键,可以用用户主键 * @param value 值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性 * @param expire 锁的时间(秒) * @param timeout 在timeout时间内仍未获取到锁,则获取失败 * @return 设置成功为 true */ public Boolean setNx(K key, V value, long expire, long timeout) { return this.setNx(key,value,expire,timeout,TimeUnit.SECONDS); } /** * 设置分布式锁,有等待时间 * * @param key 键,可以用用户主键 * @param value 值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性 * @param expire 锁的时间 * @param timeout 在timeout时间内仍未获取到锁,则获取失败 * @param timeUnit 时间单位 * @return 设置成功为 true */ public Boolean setNx(K key, V value, long expire, long timeout,TimeUnit timeUnit) { long start = System.currentTimeMillis(); //在一定时间内获取锁,超时则返回错误 for (; ; ) { // 获取到锁,并设置过期时间返回true if (Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value, expire, timeUnit))) { return true; } //否则循环等待,在timeout时间内仍未获取到锁,则获取失败 if (System.currentTimeMillis() - start > timeout) { return false; } } } /** * 释放分布式锁 * @param key 锁 * @param value 值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性 * @return 成功返回true, 失败返回false */ public boolean releaseNx(K key, V value) { Object currentValue = redisTemplate.opsForValue().get(key); if (String.valueOf(currentValue) != null && value.equals(currentValue)) { return Boolean.TRUE.equals(redisTemplate.opsForValue().getOperations().delete(key)); } return false; } /** * 普通缓存放入 * * @param key 键 * @param value 值 */ public void set(K key, V value) { try { redisTemplate.opsForValue().set(key, value); } catch (Exception e) { logger.log(Level.SEVERE,e.getMessage()); } } /** * 普通缓存放入并设置时间 * * @param key 键 * @param value 值 * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 */ public void set(K key, V value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { redisTemplate.opsForValue().set(key, value); } } catch (Exception e) { logger.log(Level.SEVERE,e.getMessage()); } } /** * value增加值 * * @param key 键 * @param number 增加的值 * @return 返回增加后的值 */ public Long incrBy(String key, long number) { return (Long) redisTemplate.execute((RedisCallback
package com.example.utils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import java.lang.reflect.Method; import java.util.Objects; /** * 动态注解传参解析工具类 * @Title: SpELUtil * @author: hulei */ public class SpELUtil { /** * 用于SpEL表达式解析. */ private static final SpelExpressionParser parser = new SpelExpressionParser(); /** * 用于获取方法参数定义名字. */ private static final DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); /** * 解析SpEL表达式 * * @param spELStr 表达式 * @param joinPoint 切点 * @return 解析结果 */ public static String generateKeyBySpEL(String spELStr, ProceedingJoinPoint joinPoint) { // 通过joinPoint获取被注解方法 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); // 使用Spring的DefaultParameterNameDiscoverer获取方法形参名数组 String[] paramNames = nameDiscoverer.getParameterNames(method); // 解析过后的Spring表达式对象 Expression expression = parser.parseExpression(spELStr); // Spring的表达式上下文对象 EvaluationContext context = new StandardEvaluationContext(); // 通过joinPoint获取被注解方法的形参 Object[] args = joinPoint.getArgs(); // 给上下文赋值 for (int i = 0; i < args.length; i++) { assert paramNames != null; context.setVariable(paramNames[i], args[i]); } return Objects.requireNonNull(expression.getValue(context)).toString(); } }
package com.example.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.concurrent.TimeUnit; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface NoRepeat { /** * 过期时间,默认3 * @return expire */ int expire() default 3; /** * 注解的动态参数,传入的redisKey * @return redisKey */ String redisKey() default ""; /** * 过期时间单位,默认是秒 * @return TimeUnit */ TimeUnit timeUnit() default TimeUnit.SECONDS; }
package com.example.aop; import com.example.annotation.NoRepeat; import com.example.utils.RedisUtils; import com.example.utils.SpELUtil; 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.stereotype.Component; import java.lang.reflect.Method; @Aspect @Component public class NoRepeatAspect { private final RedisUtils redisUtils; public NoRepeatAspect(RedisUtils redisUtils) { this.redisUtils = redisUtils; } @Around("@annotation(com.example.annotation.NoRepeat)") public Object noRepeat(ProceedingJoinPoint joinPoint) throws Throwable { // 获取请求参数 Object[] args = joinPoint.getArgs(); // 获取请求方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); // 获取注解信息 NoRepeat noRepeat = method.getAnnotation(NoRepeat.class); //获取分布式锁的key,动态参数注解传入,是参数的字符串拼接,自定义传入 String redisKey = SpELUtil.generateKeyBySpEL(noRepeat.redisKey(), joinPoint); String key = redisKey.isEmpty() ? getKey(joinPoint) : redisKey; // 判断是否已经请求过 if (Boolean.TRUE.equals(redisUtils.hasKey(key))) { System.out.println("key:"+key); return "请勿重复提交"; } //标记key请求已经处理过,多线程并发问题验证是否重复提交 boolean lock = redisUtils.setNx(redisKey, "1", noRepeat.expire(), noRepeat.timeUnit()); if(!lock){ //返回false说明分布式锁已设置过, System.out.println("请勿重复提交信息,分布式锁已设置"); return "请勿重复提交信息"; } // 处理请求 return joinPoint.proceed(args); } /** * 获取redis key */ private String getKey(ProceedingJoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); String methodName = method.getName(); String className = joinPoint.getTarget().getClass().getSimpleName(); Object[] args = joinPoint.getArgs(); StringBuilder sb = new StringBuilder(); sb.append(className).append(":").append(methodName); for (Object arg : args) { sb.append(":").append(arg.toString()); } return sb.toString(); } }
package com.example.controller; import com.example.annotation.NoRepeat; import org.springframework.web.bind.annotation.*; import java.util.Map; @RestController public class DemoController { /** * @param map 参数 * @param redisKey 数据用来标识唯一行的key,我们用来作为redis的锁,由前端传入 */ @RequestMapping("/demo") @NoRepeat(redisKey = "#redisKey",expire = 5) public String demo(@RequestParam Mapmap,@RequestParam("redisKey") String redisKey) { map.put("redisKey",redisKey); return map.toString(); } }
apifox搞了个测试接口
由于过期时间设置的是5秒,所以5秒内点击,除了第一次成功提示如下
后续5秒内点击均提示以下结果
超过5秒再点击又会正常返回数据
再用20个线程并发提交相同内容,结果如下
解决了并发时重复提交问题,在第一个线程执行到lock位置时,已经有两个线程也执行到此位置,所以没有报上面的 "请勿重复提交",而是报分布式锁已设置,因为总有一个线程先设置分布式锁
apifox的自动化并发执行接口如下
gitee源码地址: No-Repeat: springboot+aop+redis+spel实现放重复提交