Redis(44)Redis哨兵的工作原理是什么?

Redis Sentinel(哨兵)是一个用于监控 Redis 服务器集群并实现高可用性的工具。哨兵系统主要由监控、通知、自动故障转移和配置提供四个部分组成。以下是 Redis Sentinel 的详细工作原理及其结合 Java 代码的示例:

Redis Sentinel 工作原理

  1. 监控(Monitoring)

    • Sentinel 定期通过 PING 命令检查主节点和从节点的状态。如果节点响应超时,则认为节点可能出现故障。
    • 如果某个 Sentinel 标记一个节点为"主观下线"(Subjectively Down,简称 SDOWN),它会通知其他 Sentinel。
  2. 通知(Notification)

    • Sentinel 使用发布/订阅(Pub/Sub)模式通知其他 Sentinel 节点,告知某个节点的状态变化。
    • 当足够多的 Sentinel 节点同意某个节点失效时,它会被标记为"客观下线"(Objectively Down,简称 ODOWN)。
  3. 自动故障转移(Automatic Failover)

    • 当主节点被标记为 ODOWN 时,Sentinel 会选择一个从节点提升为新的主节点。
    • 其他从节点会被重新配置为新的主节点的从节点。
  4. 配置提供者(Configuration Provider)

    • Sentinel 允许客户端应用程序获取当前主节点的信息,以便客户端能够连接到正确的主节点。

Sentinel 配置示例

1. 配置 Redis 实例

配置主从架构的 Redis 实例。

主节点配置(master.conf)

plaintext 复制代码
port 6379
bind 127.0.0.1
dir /var/lib/redis
appendonly yes

从节点配置(slave.conf)

plaintext 复制代码
port 6380
bind 127.0.0.1
dir /var/lib/redis
appendonly yes
slaveof 127.0.0.1 6379

启动主从节点:

sh 复制代码
redis-server master.conf
redis-server slave.conf

2. 配置和启动 Sentinel

创建 Sentinel 配置文件(sentinel.conf):

plaintext 复制代码
port 26379
dir /tmp
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 60000

启动 Sentinel:

sh 复制代码
redis-sentinel sentinel.conf

Java 代码示例

以下 Java 代码展示了如何使用 Jedis 库连接和操作带有 Sentinel 的 Redis 集群。

1. 引入 Jedis 库

在 Maven 项目中,添加 Jedis 依赖:

xml 复制代码
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.0.1</version>
</dependency>

2. Redis Sentinel 连接和操作示例

java 复制代码
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.HostAndPort;
import java.util.HashSet;
import java.util.Set;

public class RedisSentinelExample {
    private static final String MASTER_NAME = "mymaster";

    public static void main(String[] args) {
        // 定义 Sentinel 节点
        Set<String> sentinels = new HashSet<>();
        sentinels.add("127.0.0.1:26379");

        // 创建 JedisSentinelPool 对象
        try (JedisSentinelPool sentinelPool = new JedisSentinelPool(MASTER_NAME, sentinels)) {
            try (Jedis jedis = sentinelPool.getResource()) {
                // Perform Redis operations
                jedis.set("mykey", "myvalue");
                String value = jedis.get("mykey");
                System.out.println("mykey: " + value);

                // Output details of connected master node
                System.out.println("Connected to master: " + jedis.getClient().getHost() + ":" + jedis.getClient().getPort());
            }
        }
    }
}

详细解释

  1. 连接 Redis Sentinel

    • 通过 JedisSentinelPool 连接到 Sentinel 集群。JedisSentinelPool 会自动处理故障转移,并确保客户端连接到正确的主节点。
  2. 执行 Redis 操作

    • 在实例化 Jedis 对象后,可以执行常规的 Redis 操作,如 setget
  3. 自动故障转移

    • 如果主节点失效,Sentinel 会自动提升从节点为新的主节点,并更新连接池中的信息。客户端无需手动处理故障转移。

故障转移模拟

为了测试自动故障转移,可以强制关闭 Redis 主节点,观察 Sentinel 的行为。

sh 复制代码
# 停止 Redis 主节点
redis-cli -p 6379 shutdown

重新运行 Java 程序,观察 Sentinel 如何处理故障转移并连接到新的主节点。

总结

Redis Sentinel 提供了一种高可用性解决方案,通过监控、通知、自动故障转移和配置提供等功能,确保 Redis 服务的可靠性。通过配置 Sentinel 和使用 Jedis 库,客户端应用程序可以方便地连接和操作带有 Sentinel 的 Redis 集群,实现高可用性和自动故障恢复。在实际应用中,确保正确配置 Sentinel 和 Redis 实例,并定期测试故障转移,以保证系统的稳定性和可靠性。

相关推荐
Victor35620 小时前
Redis(45)哨兵模式与集群模式有何区别?
后端
为自己_带盐1 天前
MinIO祭了,RustFS来了!
后端
浅海星星1 天前
基于vue3和springboot框架集成websocket
spring boot·后端·websocket
颜如玉1 天前
MybatisPlus Sql Inject魔法🪄
后端·mybatis·源码
汤姆yu1 天前
基于springboot的在线答题练习系统
java·spring boot·后端·答题练习
2501_909686701 天前
基于SpringBoot的宠物咖啡馆平台
spring boot·后端·宠物
繁依Fanyi1 天前
给URL加上参数,图片就能显示文字?
后端
繁依Fanyi1 天前
我把“Word 一键转 PDF”做成了一件顺手的小工具
后端
桦说编程1 天前
使用注解写出更优雅的代码,以CFFU为例
java·后端·函数式编程