防止重复提交,主要是使用锁的形式来处理,如果是单机部署,可以使用本地缓存锁(Guava)即可,如果是分布式部署,则需要使用分布式锁(可以使用zk分布式锁或者redis分布式锁),本文的分布式锁以redis分布式锁为例。
一、本地锁(Guava)
1、导入依赖
org.springframework.boot
spring-boot-starter-aop
com.google.guava
guava
21.0
2、自定义本地锁注解
packagecom.example.demo.utils;import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inheritedpublic @interfaceLocalLock {
String key()default "";//过期时间,使用本地缓存可以忽略,如果使用redis做缓存就需要
int expire() default 5;
}
3、本地锁注解实现
packagecom.example.demo.utils;importcom.google.common.cache.Cache;importcom.google.common.cache.CacheBuilder;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.Signature;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.context.annotation.Configuration;importorg.springframework.util.StringUtils;importjava.lang.reflect.Method;importjava.util.concurrent.TimeUnit;
@Aspect
@Configurationpublic classLockMethodInterceptor {//定义缓存,设置最大缓存数及过期日期
private static final Cache CACHE = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(20, TimeUnit.SECONDS).build();
@Around("execution(public * *(..)) && @annotation(com.example.demo.utils.LocalLock)")publicObject interceptor(ProceedingJoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method=signature.getMethod();
LocalLock localLock= method.getAnnotation(LocalLock.class);
String key=getKey(localLock.key(),joinPoint.getArgs());if(!StringUtils.isEmpty(key)){if(CACHE.getIfPresent(key) != null){throw new RuntimeException("请勿重复请求!");
}
CACHE.put(key,key);
}try{returnjoinPoint.proceed();
}catch(Throwable throwable){throw new RuntimeException("服务器异常");
}finally{
}
}privateString getKey(String keyExpress, Object[] args){for (int i = 0; i < args.length; i++) {
keyExpress= keyExpress.replace("arg[" + i + "]", args[i].