需求场景
电商的很多场景,比如支付,订单提交,涉及到表单重复提交,会导致许多不必要的生产事故。那么如何优雅的设计一套防重,并做到代码的解耦呢?
设计思路
1.利用spring aop 的自定义注解 2.redis 的分布式锁
具体使用
1.自定义注解
import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MultiSubmitLimit {
}
2.注解实现
@Aspect
@Component
@Slf4j
public class MultiSubmitLimitHandler {
/**
* 锁的默认时间为5秒
*/
private static final long REPEAT_LOCK_TIME = 5L;
@Pointcut("@annotation(com.xxx.common.annotation.MultiSubmitLimit))")
public void pointcut() {
}
/**
* 加锁
* @param joinPoint 请求参数
*/
@Before("pointcut()")
public void before(JoinPoint joinPoint) {}
/**
* 释放锁
* @param joinPoint
* @param result
*/
@AfterReturning(pointcut = "pointcut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {}
}
定义一个切点
前置通知
后置通知
3.redis锁防重
加锁
@Before("pointcut()")
public void before(JoinPoint joinPoint) {
long start = System.currentTimeMillis();
Object[] objects = joinPoint.getArgs();
if (objects.length == 1) {
String key = String.valueOf(objects[0].hashCode());
//这里可以加上Token
if (this.setNx(key, "1", REPEAT_LOCK_TIME)) {
log.info("check time : {}ms, key:{}", (System.currentTimeMillis() - start), key);
} else {
log.error("submitting repeat: {}", joinPoint.toLongString());
throw new RuntimeException("不要重复提交");
}
}
}
public Boolean setNx(String key, String value, Long expireTime) {
return redisTemplate.opsForValue().setIfAbsent(key, value, expireTime, TimeUnit.SECONDS);
}
释放锁
@AfterReturning(pointcut = "pointcut()", returning = "res")
public void afterReturning(JoinPoint joinPoint, Object res) {
Object[] objects = joinPoint.getArgs();
if (objects.length == 1) {
String key = String.valueOf(objects[0].hashCode());
//加上token
this.delete(key);
log.info("submitting repeat limit lock released, key:{}", key);
}
}
4.使用的时候只要在对应的方法上加上自定义注解@MultiSubmitLimit