配置 Redis 幂等仓库防止重复投递的 YAML 片段
解读
在国内金融、电商、支付类微服务面试中,**“消息重复投递”**是高频扣分点。面试官给一段业务场景:
- 订单服务通过 Camel 路由把支付成功事件发到 Kafka;
- 消费端可能因 rebalance、手动 ack 失败等原因重复拉取;
- 要求用 Redis 做幂等仓库,YAML 配置方式,Camel 3.20+ 版本,Spring Boot 2.7 环境,lettuce 连接池,Cluster 模式,key 带业务前缀,TTL 7 天,异常时降级到内存缓存。
面试官重点考察:
- 是否知道 idempotentRepository 的 Redis 实现类;
- 是否能把 Spring Data Redis 连接工厂无缝嫁接到 Camel;
- 是否理解 幂等 key 的生成策略(ExchangeId、自定义表达式、消息摘要);
- 是否考虑 Redis 集群节点故障时的重试与降级;
- 是否用 YAML 而非 Java Config 完成全部装配,符合国内“约定大于配置”的交付习惯。
答不出“lettuce 还是 jedis”、“cluster 还是哨兵”、“TTL 怎么配” 三问,基本会被判“只背过官网例子”。
知识点
- IdempotentRepository 接口:Camel 幂等顶层抽象,Redis 实现类为 RedisIdempotentRepository。
- Spring Boot 自动配置:spring-boot-starter-data-redis 已提供 LettuceConnectionFactory,Camel 通过 camel-spring-boot-starter 可一键注入。
- Redis Cluster 与单机在 URI 写法上的差异:
- 单机:
redis://auth@host:port - 集群:
redis://auth@host1:port1,host2:port2
国内阿里云、腾讯云 Redis 集群版要求 带密码、带 db0,但 cluster 模式不支持 select db,需要把 database 属性去掉。
- 单机:
- 幂等 key 生成策略:
${exchangeId}:最快,但重启后 ExchangeId 会变化;${header.orderId}:业务字段,需保证全局唯一;- 组合摘要:
${header.orderId}-${header.status},防止状态覆盖。
- TTL 设置:RedisIdempotentRepository 的 ttl 参数单位为秒,7 天 = 604800 秒;国内等保要求敏感数据不超过 90 天,7 天是常见折中值。
- 降级策略:当 Redis 抛出 RedisConnectionFailureException 时,使用 MemoryIdempotentRepository 兜底,但内存模式重启即失效,面试时必须补充“上线后通过告警人工补录”。
- YAML 语法陷阱:
- spring.redis.cluster.nodes 是 List 结构,需用
- host:port写法; - Camel 的 idempotentRepository 属于 route 级 配置,必须写在 camel.springboot.routes 下,不能放在 spring.redis 节点里;
- 自定义 Bean 名称要与 ref="redisRepo" 保持一致,否则 Spring Boot 启动报 NoSuchBeanDefinitionException。
- spring.redis.cluster.nodes 是 List 结构,需用
答案
# 1. Spring Boot Redis 集群配置
spring:
redis:
cluster:
nodes:
- r-bp1.redis.rds.aliyuncs.com:6379
- r-bp2.redis.rds.aliyuncs.com:6379
max-redirects: 5
password: ${REDIS_PASSWORD:YourStrongPassword}
lettuce:
pool:
max-active: 64
max-idle: 32
min-idle: 8
max-wait: 1000ms
timeout: 2000ms
# 2. Camel 路由与幂等仓库
camel:
springboot:
routes:
- id: payment-success-route
from:
uri: "kafka:payment-topic?brokers=alikafka-pre:9092&groupId=order-consumer"
steps:
- idempotentConsumer:
# 使用业务订单号作为幂等 key
simple: "${header.orderId}"
idempotentRepository: "redisRepo"
- to: "bean:orderService?method=handlePayment"
- to: "log:finish"
# 3. 自定义 RedisIdempotentRepository Bean,YAML 无法直接声明 Bean,需配合 spring-boot 的 @Configuration 类
# 以下代码仅用于面试口述,证明你知道“YAML 不能声明 Bean”的边界
# @Configuration
# public class IdempotentConfig {
# @Bean("redisRepo")
# public IdempotentRepository redisRepo(LettuceConnectionFactory factory) {
# RedisIdempotentRepository repo = new RedisIdempotentRepository("order:idem:", factory);
# repo.setTtl(604800); // 7 天
# return repo;
# }
# }
口述要点:
- 因为 Spring Boot 的 YAML 目前不支持直接声明 Bean,所以 redisRepo 必须通过 Java Config 提供;
- keyPrefix 带业务命名空间
order:idem:,防止不同系统冲突; - ttl 604800 秒 满足国内审计“七天可追踪”要求;
- lettuce 连接池参数参考阿里云最佳实践,max-active 不超过 Redis 规格上限;
- 集群版不支持 select DB,因此 RedisIdempotentRepository 构造方法里不传 database 参数;
- 如果面试官追问“能否纯 YAML”,回答:“Camel 3.20 目前仍需 Java Config 注册 Bean,但路由定义可 100% YAML,符合国内‘配置分离’规范。”
拓展思考
- 双写一致性:当 Redis 集群发生 failover,幂等记录可能丢失 几百毫秒,此时如果 Kafka 消费位点已提交,就会出现“重复处理”。国内大厂解法:
- Redis + MySQL 联合幂等:Redis 挡 99.9% 流量,MySQL 唯一索引兜底;
- Redisson 分布式锁:在幂等 key 上加 RLock.tryLock(50, TimeUnit.MILLISECONDS),降低并发窗口。
- 性能调优:
- pipeline 批量写:RedisIdempotentRepository 默认单次
SETEX,qps 高时可重写为 pipeline 批量提交; - key 压缩:订单号过长时,用 MurmurHash 转 64 位十六进制,节省 40% 内存;
- Redis 内存淘汰策略:国内云厂商默认 volatile-lru,需确认 TTL 键占比 > 80%,否则触发 OOM 时幂等键被误淘汰。
- pipeline 批量写:RedisIdempotentRepository 默认单次
- 合规与审计:
- 等保 2.0 要求 敏感操作日志留存 6 个月,幂等键虽只存 7 天,但 路由日志需异步落盘到 ELK,并 通过 TraceId 关联;
- 金融场景下,幂等键的删除需留痕,可监听 Redis keyspace 事件 把删除记录推到 审计 Topic。
- 云原生部署:
- 在 ACK 集群 中,Redis 使用 AlibabaCloud Redis 云盘版,Camel 通过 ServiceBinding CRD 注入 Redis 地址,YAML 零改动即可在测试、预发、生产三环境漂移;
- Grafana 监控:通过 Redis 的 key 数量指标
redis_db_keys观察幂等仓库膨胀,超过 500 万触发扩容。
把上述四点展开,面试官会默认你具备“架构级”而非“demo 级”经验,在国内一线互联网、银行、证券的 Camel 岗位终面中可直接加分。