在Spring Boot中利用Redis解决接口幂等性问题,可以通过以下步骤实现:
1. 核心思路
- 唯一标识:每次请求生成唯一ID(如UUID或业务标识),作为Redis的Key。
- 原子操作:使用Redis的
SETNX
(SET if Not Exists)命令,确保同一请求只能执行一次。 - 过期机制:为Key设置合理过期时间,避免无效数据堆积。
2. 实现步骤
2.1 添加依赖
<!-- Spring Boot Starter Data Redis -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.2 配置RedisTemplate
@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());return template;}
}
2.3 定义幂等性注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Idempotent {String keyPrefix() default "idempotent:";long expireTime() default 5000; // 过期时间(毫秒)
}
2.4 AOP切面处理
@Aspect
@Component
public class IdempotentAspect {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Around("@annotation(idempotent)")public Object handleIdempotent(ProceedingJoinPoint joinPoint, Idempotent idempotent) throws Throwable {String uniqueKey = generateUniqueKey(joinPoint, idempotent.keyPrefix());long expireTime = idempotent.expireTime();// 尝试设置Redis Key(原子操作)Boolean isSet = redisTemplate.opsForValue().setIfAbsent(uniqueKey, "LOCK", expireTime, TimeUnit.MILLISECONDS);if (isSet == null || !isSet) {throw new RuntimeException("重复请求,请稍后再试");}try {return joinPoint.proceed();} finally {// 业务完成后可选延长过期时间或保留原设置// redisTemplate.expire(uniqueKey, 60, TimeUnit.SECONDS);}}private String generateUniqueKey(ProceedingJoinPoint joinPoint, String prefix) {// 从请求参数或Header中提取唯一ID(示例从参数获取)Object[] args = joinPoint.getArgs();String requestId = (String) Arrays.stream(args).filter(arg -> arg instanceof String && ((String) arg).startsWith("req_")).findFirst().orElse(UUID.randomUUID().toString());return prefix + requestId;}
}
2.5 控制器使用示例
@RestController
public class OrderController {@PostMapping("/pay")@Idempotent(keyPrefix = "order:pay:", expireTime = 60000)public ResponseEntity<String> payOrder(@RequestParam String orderId, @RequestParam String reqId) {// 业务逻辑(如扣款、更新订单状态)return ResponseEntity.ok("支付成功");}
}
3. 关键点说明
-
唯一ID生成:
- 客户端生成唯一
reqId
(如UUID),或服务端根据业务参数生成(如userId+orderId
)。 - 避免使用时间戳,防止碰撞。
- 客户端生成唯一
-
过期时间设置:
- 根据业务耗时设置合理过期时间,避免因业务未完成导致Key提前过期。
-
异常处理:
- 业务异常需回滚操作,但幂等性Key保留,防止重复提交。
- 可结合
@Transactional
管理事务与Redis操作的一致性。
-
高并发优化:
- 使用Redis集群提升吞吐量。
- 对极高频请求可考虑本地缓存(如Caffeine)+ Redis双校验。
4. 扩展场景
- 返回缓存结果:首次请求处理完成后,将结果存入Redis,后续相同请求直接返回缓存结果。
- 结合数据库:关键操作在数据库层面添加唯一约束(如订单号唯一索引)。
通过上述方案,可有效避免重复请求导致的数据不一致问题,适用于支付、下单等高风险接口。