当前位置: 首页 > news >正文

Spring Boot中利用Redis解决接口幂等性问题

在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 {
    @Bean
    public 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 {
    @Autowired
    private 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. 关键点说明

  1. 唯一ID生成

    • 客户端生成唯一reqId(如UUID),或服务端根据业务参数生成(如userId+orderId)。
    • 避免使用时间戳,防止碰撞。
  2. 过期时间设置

    • 根据业务耗时设置合理过期时间,避免因业务未完成导致Key提前过期。
  3. 异常处理

    • 业务异常需回滚操作,但幂等性Key保留,防止重复提交。
    • 可结合@Transactional管理事务与Redis操作的一致性。
  4. 高并发优化

    • 使用Redis集群提升吞吐量。
    • 对极高频请求可考虑本地缓存(如Caffeine)+ Redis双校验。

4. 扩展场景

  • 返回缓存结果:首次请求处理完成后,将结果存入Redis,后续相同请求直接返回缓存结果。
  • 结合数据库:关键操作在数据库层面添加唯一约束(如订单号唯一索引)。

通过上述方案,可有效避免重复请求导致的数据不一致问题,适用于支付、下单等高风险接口。

相关文章:

  • GPIO接口
  • Django ORM自定义排序的实用示例
  • Axure RP 9 安装与汉化指南(附安装包)包含下载、安装、汉化、授权,Axure9 汉化教程、Axure9 汉化步骤
  • MoonSharp 文档三
  • Rust规律归纳随笔
  • 使用Mermaid语法绘制的C语言程序从Linux移植到Windows的流程图
  • Leetcode8-字符串转换整数(atoi)
  • 【技术白皮书】内功心法 | 第二部分 | Telnet远程登录的工作原理
  • 大模型:定义与主流架构全解析
  • 【TVM教程】为 Mobile GPU 自动调优卷积网络
  • SwiftUI 让视图自适应高度的 6 种方法(四)
  • Kubernetes Pod的生命周期概述
  • LearnOpenGL-笔记-其二
  • DeepSeek-实用集成大礼包
  • 基于某著名企业IPD与质量管理体系融合的研发质量管理方案(64页PPT)
  • 【保姆级教程】使用 oh-my-posh 和 clink 打造个性化 PowerShell 和 CMD
  • 十二天-双指针技术:链表问题的高效解法
  • springboot433-基于SpringBoot的流浪猫爱心救助系统(源码+数据库+纯前后端分离+部署讲解等)
  • Fast DDS Security--多方密钥协商协议
  • 人脸识别之数据集中 PI20 和 CFMT 之间关联的模型预测贝叶斯(Python+论文代码实现)
  • 上海市国防动员办公室副主任吴斌接受审查调查
  • 国台办:台湾自古属于中国,历史经纬清晰,法理事实清楚
  • 当代科技拟召开债券持有人会议 ,对“H20科技2”进行四展
  • 习近平出席中拉论坛第四届部长级会议开幕式并发表主旨讲话
  • 泽连斯基:乌克兰已做好与俄罗斯举行会谈的准备
  • 市自规局公告收回新校区建设用地,宿迁学院:需变更建设主体