Redis哨兵(主从架构 + Sentinel)


整合 Sentinel 与现有主从架构的完整步骤


在这里插入图片描述

一、配置修正说明

您当前的配置存在概念混淆:cluster.nodes 是用于 Redis Cluster 模式 的配置,而主从架构 + Sentinel 需要使用 Sentinel 专用配置。以下是正确的整合方案:


二、正确配置方案

1. 修改 application.yml
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

配置写好之后无需关心内部是如何来处理的,写业务代码和之前的一个Redis是一样的。

三、Sentinel 服务端配置

1. 创建 Sentinel 配置文件
# 创建配置目录
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的地址了,需要注意你配置的主节点的地址和端口号,上面这个只是一个例子而已。

2. 启动 Sentinel 服务
redis-sentinel /etc/redis/sentinel/26379/sentinel.conf
redis-sentinel /etc/redis/sentinel/26380/sentinel.conf
redis-sentinel /etc/redis/sentinel/26381/sentinel.conf

四、验证整合效果

1. 检查 Sentinel 拓扑发现
# 连接任意 Sentinel 节点
redis-cli -p 26379
127.0.0.1:26379> sentinel master mymaster

预期输出包含:

num-slaves 2  # 从节点数量
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());
    }
}

五、故障转移测试

1. 模拟主节点宕机
# 停止主节点 Redis 服务
redis-cli -p 6379 shutdown
2. 观察故障转移过程
# 查看 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
3. 验证客户端自动切换

重新运行测试用例,观察主节点地址是否变为新选举的主节点(如 6380)


六、生产环境优化配置

1. 安全加固
spring:
  redis:
    ssl: true  # 启用 TLS 加密
    lettuce:
      sentinel:
        password: $SENTINEL_PASSWORD  # 从环境变量读取
2. 连接池调优
spring:
  redis:
    lettuce:
      pool:
        max-wait: 1000ms  # 最大等待时间
        time-between-eviction-runs: 30s  # 空闲连接检查间隔
3. 监控配置
management:
  endpoints:
    web:
      exposure:
        include: health,metrics,redis
  metrics:
    export:
      prometheus:
        enabled: true

七、架构示意图

Spring Boot
Sentinel 26379
Sentinel 26380
Sentinel 26381
Master 6379
Slave 6380
Slave 6381

八、关键配置对照表

组件原配置新配置作用变化
Spring Bootcluster.nodessentinel.nodes从直连节点改为通过哨兵发现
Redis独立主从主从 + Sentinel 监控实现自动故障转移能力
客户端路由硬编码节点地址动态获取拓扑信息支持自动切换主节点

九、常见问题解决

问题1:客户端无法连接 Sentinel

现象RedisConnectionFailureException: Unable to connect to Sentinel
解决方案

  1. 检查 Sentinel 端口是否开放:netstat -tulnp | grep 26379
  2. 验证 Sentinel 认证密码是否正确
  3. 查看 Sentinel 日志:tail -f /var/log/redis/sentinel_26379.log
问题2:读写分离失效

现象:所有请求都路由到主节点
排查步骤

  1. 确认 read-from: REPLICA_PREFERRED 配置生效
  2. 检查从节点复制状态:redis-cli -p 6380 info replication
  3. 查看 Lettuce 拓扑发现结果:
    ((LettuceConnectionFactory) redisTemplate.getConnectionFactory()).getTopology()
    

通过以上配置,您的 Spring Boot 应用将实现:

  1. 自动故障转移:主节点宕机时自动切换
  2. 读写分离:读操作优先分发到从节点
  3. 动态拓扑感知:客户端实时感知集群状态变化
  4. 生产级可靠性:通过连接池和 SSL 确保稳定安全
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值