目录前言1. 启动并初始化 Sentinel2. Sentinel 与服务器间的默认通信2.1 获取主服务器信息2.2 获取从服务器信息2.3 向主服务器和从服务器发送信息3. 接受来自主服务器和从服务器的频道信息3.1 更新 Senti
参考资料:《Redis设计与实现 第二版》;
第四部分为多机数据库的实现,主要由以下模块组成:复制、Sentinel、集群;
本篇将介绍 Redis 的Sentinel功能。Sentinel(哨兵)是 Redis 的高可用解决方案:由一个或多个 Sentinel 实例(instance)组成的 Sentinel 系统可以监视任意多个主服务器,以及这些主服务器属下的所有从服务器。在被监控的主服务器下线时,自动将下属的某个从服务器升级为主服务器,替代旧主服务器继续处理命令请求;
与本章相关的 Redis 命令总结在下篇文章,欢迎点击收藏,本篇将不再重复:
《Redis常用命令及示例总结(API)》:https://www.cnblogs.com/dlhjw/p/15639773.html
$ redis-sentinel /path/to/your/sentinel.conf
;$ redis-server /path/to/your/sentinel.conf --sentinel
;sentinel.c/sentinelcmds
作为服务器的命令表,因此在 Sentinel 模式下,只能执行 PING、SENTINEL、INFO、SUBSCRIBE、UNSUBSCRIBE、PSUBSCRIBE、PUNSUBSCRIBE 七个命令;sentinel.c/sentinelState
结构,该结构保存了服务器中所有和 Sentinel 功能有关的状态;sentinel.c/sentinelRedisInstance
结构(图16-5);
sentinelRedisInstance
结构里有一个 addr
指针,指向实例的 IP 地址和端口号;__sentinel__:hello
频道;
run_id
、role
(服务器角色);slave
开头的字符串里;sentinelRedisInstance
结构上的区别:
PUBLISH __sentinel__:hello "
;
各参数含义如下:
参数 | 含义 |
---|---|
s_ip | Sentinel 的 IP 地址 |
s_port | Sentinel 的端口号 |
s_runid | Sentinel 的运行 ID |
s_epoch | Sentinel 当前的配置纪元 |
m_name | 主服务器的名字 |
m_ip | 主服务器的 IP 地址 |
m_port | 主服务器的端口号 |
m_epoch | 主服务器当前的配置纪元 |
SUBSCRIBE __sentinel__:hello
;__sentinel__:hello
频道发送信息,又通过订阅连接从服务器的 __sentinel__:hello
频道接受信息;__sentinel__:hello
频道的获取信息:
sentinels
字典:
sentinels
字典里;sentinels
字典里的源 Sentinel 的 flags
属性为 SRI_SENTINEL;而从服务器为 SRI_SLAVE;sentinels
字典里创建相应的示例结构;down-after-milliseconds
选项指定 Sentinel 判断实例进入主观下线所需的时间;flags
的 SRI_S_DOWN 标识;源 Sentinel 使用以下命令询问其他 Sentinel 是否同意主服务器已下线:
SENTINEL is-master-down-by-addr
;
参数 | 说明 |
---|---|
ip | 被 Sentinel 判断为主观下线的主服务器的 IP 地址 |
port | 被 Sentinel 判断为主观下线的主服务器的端口号 |
current_epoch | Sentinel 当前的配置纪元,用于选举领头 Sentinel |
runid | 可以是 * 符合(用于检测主服务器的客观下线状态)或 Sentinel 的运行 ID(用于选举领头 Sentinel) |
目标 Sentinel 收到 SENTINEL 命令后,根据其中的主服务器 IP 和端口号检查主服务器是否已下线;
然后向源 Sentinel 返回一个包含三个参数的 Multi Bulk 作为回复;
参数 | 说明 |
---|---|
down_state | 返回目标 Sentinel 对主服务器的检查结果,1代表主服务器已下线 |
leader_runid | 可以是 * 符号(用于检测主服务器的下线状态)或目标 Sentinel 的局部领头 Sentinel 的运行 ID(用于选举领头 Sentinel) |
leader_epoch | 目标 Sentinel 的局部领头 Sentinel 的配置纪元,用于选举领头 Sentinel |
flags
属性的 SRI_O_DOWN 标识,标识主服务器已经进入客观下线状态;quorum
参数中设置(包括源 Sentinel);role
属性;role
属性从 slave 变成 master 时,表明顺利升级;--结束END--
本文标题: Redis | 第12章 Sentinel 哨兵模式《Redis设计与实现》
本文链接: https://www.lsjlt.com/news/8973.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0