redis+aop实现接口防刷(幂等)
    		       		warning:
    		            这篇文章距离上次修改已过425天,其中的内容可能已经有所变动。
    		        
        		                
                在实现接口防刷(幂等)时,可以使用Redis和AOP(面向切面编程)来确保同一用户在短时间内多次请求只被处理一次。以下是一个简单的示例:
- 首先,定义一个注解来标记需要实现接口防刷的方法:
 
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Idempotent {
    // 可以添加一些配置参数,如过期时间等
}- 然后,创建一个AOP切面来处理接口防刷逻辑:
 
@Aspect
@Component
public class IdempotentAspect {
 
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 
    @Around("@annotation(idempotent)")
    public Object around(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
 
        // 生成key,可以根据方法和请求参数来生成唯一值
        String key = "idempotent:" + method.getName();
 
        // 使用Redis的setnx命令实现锁
        Boolean lock = stringRedisTemplate.opsForValue().setIfAbsent(key, "lock", 10, TimeUnit.SECONDS);
        if (lock != null && !lock) {
            // key已存在,表示已有请求在处理
            return "Request already processed";
        }
 
        try {
            // 执行原方法
            return joinPoint.proceed();
        } finally {
            // 无论成功与否,最后释放锁
            stringRedisTemplate.delete(key);
        }
    }
}- 在需要防刷的接口方法上使用
@Idempotent注解: 
@RestController
public class MyController {
 
    @Idempotent
    @GetMapping("/doSomething")
    public String doSomething() {
        // 业务逻辑
        return "Processed";
    }
}这样,每当有请求到达doSomething方法时,AOP切面会检查Redis中是否已有相同key的记录。如果没有,则设置一个锁,并执行方法;如果有,则直接返回,从而实现了接口的防刷。锁会在10秒后自动释放,以便允许后续的请求。这个例子使用了StringRedisTemplate,但是也可以根据需要使用其他类型的RedisTemplate。
评论已关闭