一、定义注解 Idempotent
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* @Author:
* @Description: 幂等注解 主要作用于方法和类上 作用在类上表示这个类里所有的方法都做限制
* 如果要使用nacos配置文件,不要这里使用@ConfigurationProperties,而是应该再写一个类
* 使用注解获取ncos的配置后,在切面里覆盖这个元数据
* @Date: 2024/4/25
**/
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Idempotent {
/**
* 幂等的超时时间,默认为 1 秒
*
* 注意,如果执行时间超过它,请求还是会进来
*/
int timeout() default 3;
/**
* 时间单位,默认为 SECONDS 秒
*/
TimeUnit timeUnit() default TimeUnit.SECONDS;
/**
* redis锁前缀
* @return
*/
String keyPrefix() default "submit:";
/**
* key分隔符
* @return
*/
String delimiter() default ":";
/**
* 提示信息,正在执行中的提示
*/
String message() default "重复请求,请稍后重试";
}
二、定义切面
import lombok.SneakyThrows;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
imp