您当前的配置存在概念混淆:cluster.nodes
是用于 Redis Cluster 模式 的配置,而主从架构 + Sentinel 需要使用 Sentinel 专用配置。以下是正确的整合方案:
spring:
redis:
# 删除 cluster 配置段
# 新增 sentinel 配置段
sentinel:
master: mymaster # 主节点名称(需与哨兵配置一致) 创建 Sentinel 配置文件 第二句里面的 这个 sentinel monitor mymaster 127.0.0.1 6379 2
nodes:
- 127.0.0.1:26379 # Sentinel 节点1
- 127.0.0.1:26380 # Sentinel 节点2
- 127.0.0.1:26381 # Sentinel 节点3
# 保留原有配置
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 2
read-from: REPLICA_PREFERRED # 开启读写分离
# 密码配置(按需)
password: your_redis_password # Redis节点密码
# Sentinel节点密码(如果哨兵有密码)
sentinel:
password: sentinel_password
# 创建配置目录
mkdir -p /etc/redis/sentinel/{26379,26380,26381}
# Sentinel 1 (26379)
cat > /etc/redis/sentinel/26379/sentinel.conf <<EOF
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster your_redis_password
sentinel parallel-syncs mymaster 1
daemonize yes
logfile "/var/log/redis/sentinel_26379.log"
EOF
# Sentinel 2 (26380) 和 Sentinel 3 (26381) 配置类似,修改 port 和 logfile 路径
Sentinel 配置文件第二句这个 sentinel monitor mymaster 127.0.0.1 6379 2 指定了Redis主节点然后就需要其配置Redis的地址了,需要注意你配置的主节点的地址和端口号,上面这个只是一个例子而已。
redis-sentinel /etc/redis/sentinel/26379/sentinel.conf
redis-sentinel /etc/redis/sentinel/26380/sentinel.conf
redis-sentinel /etc/redis/sentinel/26381/sentinel.conf
# 连接任意 Sentinel 节点
redis-cli -p 26379
127.0.0.1:26379> sentinel master mymaster
预期输出包含:
num-slaves 2 # 从节点数量
@SpringBootTest
public class SentinelIntegrationTest {
@Autowired
private RedisConnectionFactory connectionFactory;
@Test
void testSentinelConnection() {
LettuceConnectionFactory lettuceFactory = (LettuceConnectionFactory) connectionFactory;
RedisSentinelConfiguration sentinelConfig = lettuceFactory.getSentinelConfiguration();
System.out.println("当前主节点: " + lettuceFactory.getHostName() + ":" + lettuceFactory.getPort());
System.out.println("哨兵节点列表: " + sentinelConfig.getSentinels());
}
}
# 停止主节点 Redis 服务
redis-cli -p 6379 shutdown
# 查看 Sentinel 日志
tail -f /var/log/redis/sentinel_26379.log
预期日志内容:
+sdown master mymaster 127.0.0.1 6379
+vote-for-leader XXXX 1
+switch-master mymaster 127.0.0.1 6379 127.0.0.1 6380
重新运行测试用例,观察主节点地址是否变为新选举的主节点(如 6380)
spring:
redis:
ssl: true # 启用 TLS 加密
lettuce:
sentinel:
password: $SENTINEL_PASSWORD # 从环境变量读取
spring:
redis:
lettuce:
pool:
max-wait: 1000ms # 最大等待时间
time-between-eviction-runs: 30s # 空闲连接检查间隔
management:
endpoints:
web:
exposure:
include: health,metrics,redis
metrics:
export:
prometheus:
enabled: true
组件 | 原配置 | 新配置 | 作用变化 |
---|---|---|---|
Spring Boot | cluster.nodes |
sentinel.nodes |
从直连节点改为通过哨兵发现 |
Redis | 独立主从 | 主从 + Sentinel 监控 | 实现自动故障转移能力 |
客户端路由 | 硬编码节点地址 | 动态获取拓扑信息 | 支持自动切换主节点 |
现象:RedisConnectionFailureException: Unable to connect to Sentinel
解决方案:
netstat -tulnp | grep 26379
tail -f /var/log/redis/sentinel_26379.log
现象:所有请求都路由到主节点
排查步骤:
read-from: REPLICA_PREFERRED
配置生效redis-cli -p 6380 info replication
((LettuceConnectionFactory) redisTemplate.getConnectionFactory()).getTopology()
通过以上配置,您的 Spring Boot 应用将实现: