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 集群,从而实现高可用性和自动故障恢复。

相关推荐
SimonKing40 分钟前
全网爆火的OpenClaw保姆级教程Linux版,它来了。
java·后端·程序员
青柠代码录1 小时前
【Linux】常用命令:sort
后端
小江的记录本1 小时前
【MyBatis-Plus】MyBatis-Plus的核心特性、条件构造器、分页插件、乐观锁插件
java·前端·spring boot·后端·sql·tomcat·mybatis
驕傲的兎孒2 小时前
基于 SpringBoot + Vue3 + AI 打造企业级售后服务支持平台 | 实战方案分享
人工智能·spring boot·后端
大傻^2 小时前
Spring AI Alibaba 可观测性实践:AI应用监控与链路追踪
java·人工智能·后端·spring·springaialibaba
诗人不写诗2 小时前
spring是如何组织切面的
java·后端·spring
小杨同学492 小时前
STM32 进阶封神之路(二十二):DMA 实战全攻略 ——ADC 采集 + 串口收发 + 内存复制(库函数 + 代码落地)
后端·单片机·嵌入式
天下无贼!3 小时前
【Python】2026版——FastAPI 框架快速搭建后端服务
开发语言·前端·后端·python·aigc·fastapi
大傻^3 小时前
Spring AI Alibaba Agent开发:基于ChatClient的智能体构建模式
java·数据库·人工智能·后端·spring·springaialibaba
大傻^3 小时前
Spring AI Alibaba ChatClient实战:流式输出与多轮对话管理
java·人工智能·后端·spring·springai·springaialibaba