- 创建自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DuplicateSubRedis {
String prefixValue();
String[] paramValues();
long intervalSecond() default 3;
}
- 创建AOP
@Slf4j
@Aspect
@Component
public class DuplicateRedisAspect {
private static SpelExpressionParser parser = new SpelExpressionParser();
@Pointcut("@annotation(com.workcell.workdata.xsgc.annotation.DuplicateSubRedis)")
public void controllerPointcut() {}
private final static String CONNECTOR = StrUtil.UNDERLINE;
private final static String DUPLICATE_REDIS_KEY = "duplicate";
private final static String SERVICE_NAME = "base";
@Resource
private RedisTemplate redisTemplate;
@Around("controllerPointcut()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
DuplicateSubRedis duplicateSubRedis = method.getAnnotation(DuplicateSubRedis.class);
String prefixValue = duplicateSubRedis.prefixValue();
String[] paramValues = duplicateSubRedis.paramValues();
long intervalSecond = duplicateSubRedis.intervalSecond();
if(StrUtil.isBlank(prefixValue) || ObjectUtil.isNull(paramValues) || ObjectUtil.isNull(intervalSecond)){
throw new RuntimeException("防重注解参数为空!");
}
String paramValue= getParamKeyValue(paramValues,method,joinPoint.getArgs());
WorkdataUser user = SecurityUtils.getUser();
String baseKey = TenantContextHolder.getTenantId() + ":"+ DUPLICATE_REDIS_KEY + ":";
String businessKey = baseKey+ SERVICE_NAME + CONNECTOR + prefixValue + CONNECTOR + 1 + CONNECTOR + paramValue;
if(redisTemplate.hasKey(businessKey)){
throw new RuntimeException("请求次数频繁,稍后重试!");
}
if(intervalSecond < 0){
intervalSecond = 1;
}
redisTemplate.opsForValue().set(businessKey,1,intervalSecond, TimeUnit.SECONDS);
return joinPoint.proceed();
}
private String getParamKeyValue(String[] keys,Method method,Object[] args){
StringBuffer result = new StringBuffer();
Arrays.stream(keys).forEach(v->{
String str = this.generateKeyBySpEL(v, method, args);
if(StrUtil.isNotBlank(str)){
result.append(str);
}
});
return result.toString();
}
private String generateKeyBySpEL(String key, Method method, Object[] args) {
Expression expression = parser.parseExpression(key);
EvaluationContext context = new StandardEvaluationContext();
DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
String[] parameterNames = discoverer.getParameterNames(method);
Map<String,String> resultMap = new HashMap<>();
for (int i = 0; i < parameterNames.length; i++) {
Object arg = args[i];
if(arg instanceof Map){
resultMap = (Map<String, String>) arg;
}
context.setVariable(parameterNames[i],arg);
}
return expression.getValue(context).toString();
}
}
- Controller使用注解
@DuplicateSubRedis(prefixValue = "accountApplyNew", intervalSecond = 1, paramValues = {"#auditParam.deptId","#auditParam.applyStatus"})
@ApiOperation(value = "组织申请", notes = "组织申请")
@PostMapping("/accountApplyNew")
public R<Boolean> accountApplyNew(@RequestBody StructureDeptUserAllDTO.AccountAuditParam auditParam){
return R.ok();
}