Redis Sentinel 及 Redisson 连接问题全解析

在 Kubernetes (k8s) 环境下使用 Redis Sentinel 进行高可用部署时,可能会遇到 failover 超时 、Sentinel 误判 、Spring Boot 连接失败 以及 Redisson 配置错误等问题。本文将对这些问题进行汇总分析,并提供详细的解决方案。


1️⃣ Redis Sentinel 介绍

Redis Sentinel 是 Redis 的高可用组件,主要功能包括:

  • 主节点故障检测:判断 Redis 主节点是否宕机。
  • 自动故障转移:当主节点不可用时,选举新的主节点。
  • 通知客户端:应用程序(如 Spring Boot、Redisson)可以通过 Sentinel 获取最新的主节点地址。

Redis Sentinel 关键配置(sentinel.conf):

conf 复制代码
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
  • down-after-milliseconds mymaster 5000:如果 5 秒内主节点无响应,认为其不可用。
  • failover-timeout mymaster 60000:Sentinel 允许 60 秒内完成故障转移。

2️⃣ Redis Sentinel 报错分析

🚨 问题 1:Redis 日志报 failover-timeout

🔍 可能原因
  • 副本同步未完成,Sentinel 不能立即选出新的主节点。
  • 网络抖动,导致 Sentinel 误判主节点宕机。
  • Sentinel 数量不足,无法进行有效投票。
✅ 解决方案

1️⃣ 检查 Redis 副本同步状态:

sh 复制代码
kubectl exec -it redis-master-0 -- redis-cli INFO replication

如果 connected_slaves: 0,说明没有可用副本,Sentinel 可能无法完成切换。

2️⃣ 增大 failover-timeout:

conf 复制代码
sentinel failover-timeout mymaster 120000  # 增加至 120 秒

然后重启 Sentinel:

sh 复制代码
kubectl delete pod -l app=redis-sentinel

3️⃣ 检查 Sentinel 是否足够:

sh 复制代码
kubectl get pods -l app=redis-sentinel

如果 num-other-sentinels: 0,需要增加副本:

yaml 复制代码
spec:
  replicas: 3  # 至少 3 个 Sentinel

🚨 问题 2:Spring Boot 连接 Redis Sentinel 一直报 added 和 is down

🔍 可能原因
  • spring.redis.sentinel.master 配置错误,导致 Spring Boot 连接不上 Redis Sentinel。
  • sentinel.conf 里 mymaster 拼写错误,导致 Sentinel 无法正确发现主节点。
✅ 解决方案

1️⃣ 检查 sentinel.conf 配置

sh 复制代码
kubectl exec -it redis-sentinel-0 -- redis-cli -p 26379 SENTINEL MASTER mymaster

如果 mymaster 不存在,说明 Sentinel 配置错误。

2️⃣ 检查 Spring Boot 配置

yaml 复制代码
spring:
  redis:
    sentinel:
      master: mymaster  # 必须和 Sentinel 配置一致
      nodes:
        - redis-sentinel-0:26379
        - redis-sentinel-1:26379
        - redis-sentinel-2:26379
    password: yourpassword

3️⃣ 调整 timeout 以减少误判

yaml 复制代码
spring:
  redis:
    timeout: 5000
    lettuce:
      pool:
        max-active: 10
        max-wait: 5000

3️⃣ Redisson 连接 Redis Sentinel 配置

Redisson 是一个高效的 Redis 客户端,支持 Redis Sentinel 模式。

🚀 依赖

在 pom.xml 中引入:

xml 复制代码
<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.23.2</version>
</dependency>

🚀 配置 RedissonClient

java 复制代码
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.useSentinelServers()
              .setMasterName("mymaster")
              .addSentinelAddress("redis://redis-sentinel-0:26379",
                                  "redis://redis-sentinel-1:26379",
                                  "redis://redis-sentinel-2:26379")
              .setPassword("yourpassword")
              .setTimeout(5000)
              .setConnectTimeout(10000);
        return Redisson.create(config);
    }
}

4️⃣ 测试 Redisson 连接

java 复制代码
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {
    private final RedissonClient redissonClient;

    public RedisController(RedissonClient redissonClient) {
        this.redissonClient = redissonClient;
    }

    @GetMapping("/set")
    public String set(@RequestParam String key, @RequestParam String value) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        bucket.set(value);
        return "Set " + key + " = " + value;
    }

    @GetMapping("/get")
    public String get(@RequestParam String key) {
        RBucket<String> bucket = redissonClient.getBucket(key);
        return "Get " + key + " = " + bucket.get();
    }
}

5️⃣ 可能的错误 & 解决方案

❌ Unable to connect to Redis Sentinel

✅ 解决方案

  • 检查 Sentinel 是否正常运行

    sh 复制代码
    kubectl get pods -l app=redis-sentinel

    如果 Sentinel Pod 挂了,需要重新启动。

  • 手动测试 Sentinel

    sh 复制代码
    redis-cli -p 26379 SENTINEL MASTER mymaster

    如果 mymaster 不存在,说明 Sentinel 配置有误。


🔎 总结

🚀 Redis Sentinel + Redisson 高可用方案的关键要点:

1️⃣ Redis Sentinel 需配置正确 ,mymaster 名称需一致,failover-timeout 需足够长。

2️⃣ Spring Boot 连接 Redis Sentinel 时 ,spring.redis.sentinel.master 需要匹配 Sentinel 配置。

3️⃣ Redisson 连接 Sentinel 需要手动配置 RedissonClient ,并保证 sentinel.conf 正确。

4️⃣ 故障排查时,手动执行 SENTINEL MASTER mymaster,检查主节点状态。

📢 如果你仍然遇到问题,可以提供 redis-cli 的 SENTINEL MASTER mymaster 输出,我可以帮你分析!🚀

相关推荐
Promising_GEO9 小时前
GEE实战02期:从卫星波段开始:利用 Sentinel-2 自己计算 NDVI
java·sentinel·遥感·空间分析
xiaoqiMikko15 小时前
Redis 实战(二):宕机会丢多少数据 —— kill -9 与真断电下 RDB / AOF 逐个实测(6.2.4)
redis·后端
夕除17 小时前
redis--ziplist
redis
2301_322414280419 小时前
基于SSM框架的在线考试管理系统的设计与实现14750-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·redis·mysql·tomcat·maven·mybatis·ssm
我星期八休息19 小时前
Redis 的原理和使用(2)
前端·数据库·redis·python·mysql·缓存·bootstrap
imDwAaY2 天前
操作系统如何管理程序运行?从线程、进程到内存隔离与用户态切换:CS162 Note2
android·java·redis
码途行者2 天前
处理超时未支付订单:两套轻量化方案,附 PHP 极简实现
开发语言·redis·php
草莓熊Lotso2 天前
【C++ 基于微服务的即时通讯系统】etcd + brpc 从安装到服务注册发现全链路实战
网络·数据库·c++·redis·微服务·etcd
我不会起名字3222 天前
Redis 突然只能读不能写:MISCONF 报错的 3 类触发条件与 4 步恢复
运维·redis·持久化·排查·misconf
我不会起名字3222 天前
Redis 从 0 到 1(九):主库宕机 30 秒自动切主——1 主 2 从 3 哨兵 + Go 客户端
redis·docker·sentinel·高可用·哨兵