Redis(113)Redis的哨兵机制如何使用?

Redis Sentinel 是 Redis 用于主从复制架构中实现高可用性管理的工具。Redis Sentinel 监控主从节点,并在主节点发生故障时自动进行故障转移,确保系统的高可用性。

下面是详细的 Redis Sentinel 使用讲解以及相关代码示例。

1. Redis Sentinel 结构

Redis Sentinel 主要功能包括:

  1. 监控:定期检查主节点和从节点是否正常运行。
  2. 通知:在检测到节点故障时,通知系统管理员或其他应用程序。
  3. 故障转移:当主节点发生故障时,自动将一个从节点提升为主节点。
  4. 配置提供:客户端可以通过 Sentinel 获取当前主节点的信息。

2. 配置 Redis 实例

假设我们有如下 Redis 实例:

  • 主节点:127.0.0.1:6379
  • 从节点1:127.0.0.1:6380
  • 从节点2:127.0.0.1:6381

配置主节点(无需特别配置,启动即可)

bash 复制代码
redis-server --port 6379

配置从节点

在从节点的 redis.conf 文件中添加如下配置:

conf 复制代码
replicaof 127.0.0.1 6379

启动从节点:

bash 复制代码
redis-server --port 6380
redis-server --port 6381

3. 配置 Redis Sentinel

创建三个 Sentinel 配置文件 sentinel-26379.confsentinel-26380.confsentinel-26381.conf。分别监听不同的端口。

sentinel-26379.conf

conf 复制代码
port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

sentinel-26380.conf

conf 复制代码
port 26380
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

sentinel-26381.conf

conf 复制代码
port 26381
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1

4. 启动 Redis Sentinel

使用以下命令启动 Sentinel 实例:

bash 复制代码
redis-sentinel sentinel-26379.conf
redis-sentinel sentinel-26380.conf
redis-sentinel sentinel-26381.conf

5. 测试 Sentinel

进行故障模拟

我们可以通过关闭主节点来模拟故障:

bash 复制代码
redis-cli -p 6379 shutdown

Sentinel 将检测到主节点故障,并自动进行故障转移,将其中一个从节点提升为新的主节点。

连接并验证新的主节点

使用 Redis Sentinel 命令获取当前主节点信息:

bash 复制代码
redis-cli -p 26379 sentinel get-master-addr-by-name mymaster

这将返回新的主节点的 IP 和端口。用这个信息连接新的主节点进行读写操作。

6. Java 示例代码

通过 Jedis 库在 Java 中实现 Sentinel 机制的客户端连接。

Jedis 配置

添加 Jedis 依赖到你的 pom.xml 文件中(如果使用 Maven):

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

连接 Redis Sentinel 并进行操作

java 复制代码
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

public class RedisSentinelExample {
    public static void main(String[] args) {
        Set<String> sentinels = new HashSet<>();
        sentinels.add("127.0.0.1:26379");
        sentinels.add("127.0.0.1:26380");
        sentinels.add("127.0.0.1:26381");

        try (JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels);
             Jedis jedis = pool.getResource()) {
            // 写操作
            jedis.set("key1", "value1");
            jedis.set("key2", "value2");

            // 读操作
            String value1 = jedis.get("key1");
            String value2 = jedis.get("key2");
            System.out.println("key1: " + value1);
            System.out.println("key2: " + value2);
        }
    }
}

7. Sentinel 高级配置

修改通知脚本

Sentinel 可以配置通知脚本,在发生故障转移时,执行自定义脚本以通知管理员或执行其他操作。例如:

conf 复制代码
sentinel notification-script mymaster /path/to/notification.sh

客户端重新配置脚本

Sentinel 还可以配置客户端重新配置脚本,当主节点发生变化时,通知相关客户端:

conf 复制代码
sentinel client-reconfig-script mymaster /path/to/reconfig.sh

8. Redis Sentinel 命令

以下是一些常用的 Redis Sentinel 命令:

  • SENTINEL MONITOR <name> <ip> <port> <quorum>:添加一个新的主节点监控。
  • SENTINEL REMOVE <name>:移除一个主节点监控。
  • SENTINEL FAILOVER <name>:手动触发故障转移。
  • SENTINEL SENTINELS <master-name>:列出监控某个主节点的所有 Sentinel 实例。
  • SENTINEL SLAVES <master-name>:列出某个主节点的所有从节点。

总结

Redis Sentinel 提供了一种有效的方式来监控 Redis 主从架构的高可用性,通过自动故障转移确保系统的稳定性。通过上述配置和代码示例,可以轻松实现 Redis Sentinel 机制并验证其功能。在实际生产环境中,可以根据需要进一步优化 Sentinel 配置,以满足具体的业务需求。

相关推荐
程序猿追8 小时前
我搭了个网页工具:输入关键词,SERP API 自动吐出比价 Excel
后端
Lee川8 小时前
RAG 实战:从一篇掘金文章出发,拆解检索增强生成的全链路
前端·人工智能·后端
Lee川8 小时前
MCP 高德地图实战:当 AI 学会使用工具,一个协议如何重塑大模型的行动边界
前端·人工智能·后端
楼田莉子9 小时前
C++17新特性:__had_include/属性/求值顺序规则
开发语言·c++·后端
程序员cxuan9 小时前
Codex 把我家烂网给优化后,我 TM 直接原地起飞了。
人工智能·后端·程序员
IT_陈寒9 小时前
Redis批量删除踩了坑,原来DEL命令不是万能的
前端·人工智能·后端
叫我少年10 小时前
C# 命名空间与 using 指令 — 文件范围、全局导入、别名
后端
我是一颗柠檬11 小时前
【MySQL全面教学】MySQL基础SQL语句Day3(2026年)
数据库·后端·sql·mysql·oracle
老毛肚11 小时前
Spring boot 特性和自写Reids组件
java·spring boot·后端
蝎子莱莱爱打怪12 小时前
👍🏻👍🏻6年381颗芯片+韬定律,华为重新定义半导体,为什么还有人喷???
后端·面试·程序员