Redis(43)Redis哨兵(Sentinel)是什么?

Redis Sentinel(哨兵)是一种用于管理 Redis 实例的高可用性解决方案。它提供了监控、通知和自动故障转移等功能,确保 Redis 服务在发生故障时能够自动恢复,提供高可用性和可靠性。以下是详细介绍 Redis Sentinel 的功能及其代码示例。

Redis Sentinel 的功能

  1. 监控(Monitoring)

    • Sentinel 会持续监控主节点和从节点的运行状态,检查它们是否正常工作。
  2. 通知(Notification)

    • 如果 Sentinel 检测到某个节点发生故障,它会通过 API 向系统管理员或其他应用程序发送通知。
  3. 自动故障转移(Automatic Failover)

    • 如果主节点发生故障,Sentinel 会将其中一个从节点提升为新的主节点,并将其他从节点指向新的主节点。
  4. 配置提供者(Configuration Provider)

    • Sentinel 允许客户端应用程序查询当前 Redis 集群的状态信息,以便客户端能够始终连接到正确的主节点。

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

3. Java 代码示例:使用 Jedis 连接带有 Sentinel 的 Redis 集群

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

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 {

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

        // 创建 JedisSentinelPool 对象
        JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels);

        try (Jedis jedis = pool.getResource()) {
            // 操作 Redis
            jedis.set("key", "value");
            String value = jedis.get("key");
            System.out.println("Get key: " + value);
        } finally {
            // 关闭连接池
            pool.close();
        }
    }
}

详细解释

  1. 监控

    • Sentinel 持续监控主节点和从节点的运行状态。配置文件中的 sentinel monitor mymaster 127.0.0.1 6379 2 表示 Sentinel 监控的主节点是 127.0.0.1:6379,并且至少需要 2 个 Sentinel 同意主节点失效,才会进行故障转移。
  2. 通知

    • sentinel down-after-milliseconds mymaster 5000 配置了 Sentinel 判断主节点失效的时间阈值(毫秒)。超出这个时间后,Sentinel 会认为主节点失效并发送通知。
  3. 自动故障转移

    • sentinel failover-timeout mymaster 60000 配置了 Sentinel 进行故障转移的超时时间。故障转移过程中,Sentinel 会将其中一个从节点提升为新的主节点,并将其他从节点指向新的主节点。
  4. 配置提供者

    • 客户端应用程序(如 Java 代码中的 JedisSentinelPool)可以通过 Sentinel 获取当前主节点的信息,并确保连接的是正确的主节点。

故障转移的示例

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

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

此时,Sentinel 会检测到主节点失效,并自动提升从节点为新的主节点,然后重新配置集群。

总结

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

相关推荐
我不是混子13 小时前
Java的SPI机制详解
java·后端
Moonbit13 小时前
MoonBit Pearls Vol.9:正则表达式引擎的两种实现方法:导数与 Thompson 虚拟机
后端·正则表达式·编程语言
文心快码BaiduComate14 小时前
一人即团队,SubAgent引爆开发者新范式
前端·后端·程序员
掘金一周14 小时前
2025年还有前端不会Nodejs ?| 掘金一周 9.25
android·前端·后端
RoyLin14 小时前
前端·后端·node.js
泉城老铁14 小时前
springboot常用的注解需要了解,开发必备
spring boot·后端
RoyLin14 小时前
C++ 基础与核心概念
前端·后端·node.js
aiopencode14 小时前
Charles 抓包 HTTPS 原理详解,从 CONNECT 到 SSL Proxying、常见问题与真机调试实战(含 Sniffmaster 补充方案)
后端
泉城老铁14 小时前
springboot 框架集成工作流的开源框架有哪些呢
spring boot·后端·工作流引擎
aloha_14 小时前
Ubuntu/Debian 系统中,通过包管理工具安装 Redis
后端