[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"article-detail-ssr-56755662075464716":3},{"id":4,"title":5,"tag":6,"createTime":7,"updateTime":7,"renderedHtml":8,"description":9,"ogImageUrl":10},"56755662075464716","哨兵机制原理详解","Redis","2024-01-27","\u003Ch2 id=\"h2-0--sentinel\">初始化 Sentinel\u003C\u002Fh2>\n\u003Cp>初始化 Sentinel 的最后一步是创建连向 master 的网络连接，Sentinel 将成为 master 的客户端，它可以向主服务器发送命令，并从命令回复中获取相关的信息。\n对于每个被 Sentinel 监视的主服务器来说，Sentinel 会创建两个连向主服务器的异步网络连接：\u003C\u002Fp>\n\u003Col>\n\u003Cli>命令连接：这个连接专门用于向主服务器发送命令，并接收命令回复。\u003C\u002Fli>\n\u003Cli>订阅连接：这个连接专门用于订阅主服务器的 __sentinel__ :hello 频道。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cblockquote>\n\u003Cp>\u003Ccode>为什么要建立两个连接：\u003C\u002Fcode>\u003C\u002Fp>\n\u003Cp>在 Redis 目前的发布与订阅功能中，被发送的信息都不会保存在 Redis 服务器里面，如果在信息发送时，想要接收信息的客户端不在线或者断线，那么这个客户端就会丢失这条信息。因此，为了不丢失__sentinel__:hello 频道的任何信息，Sentinel 必须专门用一个订阅连接来接收该频道的信息。\u003C\u002Fp>\n\u003Cp>另一方面，除了订阅频道之外，Sentinel 还必须向主服务器发送命令，以此来与主服务器进行通信，所以 Sentinel 还必须向主服务器创建命令连接。\u003C\u002Fp>\n\u003Cp>因为 Sentinel 需要与多个实例创建多个网络连接，所以 Sentinel 使用的是异步连接。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Ch2 id=\"h2-1-\">三个定时任务（重要）\u003C\u002Fh2>\n\u003Cp>Sentinel 维护着三个定时任务以监测 Redis 节点及其它 Sentinel 节点的状态。\u003C\u002Fp>\n\u003Ch3 id=\"h3-2-info-\">INFO 任务\u003C\u002Fh3>\n\u003Cp>在 Sentinel 的配置文件中有 master 的 IP 和端口号，所以 Sentinel 在初始时是知道 master 节点的位置，于是 Sentinel 可以向 master 建立命令连接和订阅连接。\u003C\u002Fp>\n\u003Cp>\u003Cimg class=\"article-img article-img-zoomable\" src=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202305182250389.png\" alt=\"\" loading=\"lazy\" decoding=\"async\" style=\"width: 100%; height: auto; max-width: 100%; display: block;\" srcset=\"https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202305182250389.png?imageMogr2\u002Fthumbnail\u002F400x\u002Fformat\u002Fwebp\u002Fquality\u002F85 400w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202305182250389.png?imageMogr2\u002Fthumbnail\u002F800x\u002Fformat\u002Fwebp\u002Fquality\u002F85 800w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202305182250389.png?imageMogr2\u002Fthumbnail\u002F1200x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1200w, https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202305182250389.png?imageMogr2\u002Fthumbnail\u002F1920x\u002Fformat\u002Fwebp\u002Fquality\u002F85 1920w\" sizes=\"(max-width: 576px) 100vw, (max-width: 992px) 90vw, 720px\">\u003C\u002Fp>\n\u003Cp>Sentinel 默认会以每十秒一次的频率，通过命令连接向 master 发送 INFO 命令，并通过分析 master 返回的 INFO 命令回复更新主从节点的相关信息。下面是 INFO 命令的回复的例子：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"text\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">text\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">13\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-text\">\u003Cspan class=\"ssr-code-line\"># Server\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">run_id:7611c59dc3a29aa6fa0609f841bb6a1019008a9c\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\"># Replication\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">role:master\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">slave0:ip=127.0.0.1,port=11111,state=online,offset=43,lag=0\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">slave1:ip=127.0.0.1,port=22222,state=online,offset=43,lag=0\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">slave2:ip=127.0.0.1,port=33333,state=online,offset=43,lag=0\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\"># Other sections\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>Sentinel 可以获取以下两方面的信息：\u003C\u002Fp>\n\u003Col>\n\u003Cli>\n\u003Cp>一方面是关于 master 本身的信息，包括 master 的运行 ID（run_id:7611c59······a9c），以及 master 的角色（role:master）。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>另一方面是关于 master 属下所有 slave 的信息，每个 slave 都由一个&quot;slave&quot;字符串开头的行记录，每行的 ip 域记录了 slave 的 IP 地址，而 port 域则记录了 slave 的端口号。根据这些 IP 地址和端口号，Sentinel 无须用户提供 slave 的地址信息，就可以自动发现 slave。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cp>当 Sentinel 发现 slave 的 IP 和端口号之后就可以向 slave 发起命令连接和订阅连接，从而获得 slave 更加具体的信息。Sentinel 向 slave 发送 INFO 命令，会得到类似如下的回复：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"text\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">text\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">3\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">4\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">5\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">6\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">7\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">8\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">9\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">10\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">11\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">12\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">13\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-text\">\u003Cspan class=\"ssr-code-line\"># Server\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">run_id:32be0699dd27b410f7c90dada3a6fab17f97899f\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\"># Replication\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">role:slave\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">master_host:127.0.0.1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">master_port:6379\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">master_link_status:up\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">slave_repl_offset:11887\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">slave_priority:100\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\"># Other sections\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">...\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>根据 INFO 命令的回复，Sentinel 会提取出以下信息：\u003C\u002Fp>\n\u003Col>\n\u003Cli>slave 的运行 ID（run_id）。\u003C\u002Fli>\n\u003Cli>slave 的角色 role。\u003C\u002Fli>\n\u003Cli>master 的 IP 地址 master_host，以及 master 的端口号 master_port。\u003C\u002Fli>\n\u003Cli>master 的连接状态 master_link_status。\u003C\u002Fli>\n\u003Cli>slave 的优先级 slave_priority。\u003C\u002Fli>\n\u003Cli>slave 的复制偏移量 slave_repl_offset。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cp>根据这些信息，Sentinel 会对 slave 的相关信息进行更新。\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>\u003Ccode>总结：\u003C\u002Fcode>\u003C\u002Fp>\n\u003Cp>Sentinel 每隔 10s 就会通过命令连接向所有的 master 和 slave 节点发送 INFO 命令，根据命令的回复，sentinel 可以知道 master 和 slave 的各种详细信息，并更新相关数据。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Ch3 id=\"h3-3-\">订阅\u002F发布任务\u003C\u002Fh3>\n\u003Cp>当 Sentinel 与一个 master 或者 slave 建立起订阅连接之后，Sentinel 就会通过订阅连接，向服务器发送以下命令：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"text\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">text\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-text\">\u003Cspan class=\"ssr-code-line\">SUBSCRIBE __sentinel__:hello\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>\u003Ccode>Sentinel对 __sentinel__:hello 频道的订阅会一直持续到Sentinel与Redis节点的连接断开为止。\u003C\u002Fcode> 订阅成功后，每个 Sentinel 节点每 2 秒就会向每个 Redis 节点发布一条__sentinel__ :hello 主题的信息（通过命令连接发送 publish 命令），当 Redis 节点中该主题的信息发生了变化，就会立即通知到所有订阅者（Sentinel）。举个例子，假设现在有 sentinel1、sentinel2、sentinel3 三个 Sentinel 在监视同一个 master，那么当 sentinel1 向 master 的 __sentinel__ :hello 频道发送一条信息时，所有订阅了 __sentinel__ :hello 频道的 Sentinel（包括 sentinel1 自己在内）都会收到 master 的返回信息。\u003C\u002Fp>\n\u003Cp>当一个 Sentinel 从 __sentinel__:hello 频道收到一条信息时，Sentinel 会对这条信息进行分析，提取出信息中的 Sentinel IP 地址、Sentinel 端口号、Sentinel 运行 ID 等八个参数（通过这些信息，当前的 Sentinel 就可以知道其他 Sentinel 的 IP 和端口号，从而向其他 Sentinel 建立连接），并进行以下检查：\u003C\u002Fp>\n\u003Cul>\n\u003Cli>如果本地信息中记录的 Sentinel 运行 ID 和接收信息的 Sentinel 的运行 ID 相同，那么说明这条信息是 Sentinel 自己发送的，Sentinel 将丢弃这条信息，不做进一步处理。\u003C\u002Fli>\n\u003Cli>如果本地信息中记录的 Sentinel 运行 ID 和接收信息的 Sentinel 的运行 ID 不相同，那么说明这条信息是监视同一个 Redis 节点的其他 Sentinel 发来的，接收信息的 Sentinel 将根据信息中的各个参数，对相应 master 节点的相关信息进行更新（Sentinel 节点本地存储了 master 节点的相关信息，对这里面的信息进行更新）。\u003C\u002Fli>\n\u003Cli>如果接收信息的 Sentinel 的运行 ID 在本地中不存在，那么说明有新建立的 Sentinel 节点对 master 节点的监控，当前 Sentinel 节点会在 master 的记录中增加一条新 Sentinel 节点的信息。\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>当 Sentinel 通过频道信息发现一个新的 Sentinel 时，它不仅会为新 Sentinel 创建相关记录信息，还会创建一个连向新 Sentinel 的命令连接，而新 Sentinel 也同样会创建连向这个 Sentinel 的命令连接，最终监视同一 master 的多个 Sentinel 将形成相互连接的网络。\u003C\u002Fp>\n\u003Cblockquote>\n\u003Ch6 id=\"h6-4-sentinel-\">Sentinel 之间不会创建订阅连接\u003C\u002Fh6>\n\u003Cp>Sentinel 在连接 master 或者 slave 时，会同时创建命令连接和订阅连接，但是在连接其他 Sentinel 时，却只会创建命令连接，而不创建订阅连接。这是因为 Sentinel 需要通过接收 master 或者 slave 发来的频道信息来发现未知的新 Sentinel，所以才需要建立订阅连接，而相互已知的 Sentinel 只要使用命令连接来进行通信就足够了。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Cblockquote>\n\u003Cp>\u003Ccode>总结：\u003C\u002Fcode>\u003C\u002Fp>\n\u003Cp>每个 Sentinel 节点每 2 秒就会向每个 Redis 节点发布一条消息。因为一个 Sentinel 可以通过分析接收到的频道信息来获知其他 Sentinel 的存在，并通过发送频道信息来让其他 Sentinel 知道自己的存在，所以用户在使用 Sentinel 的时候并不需要提供各个 Sentinel 的地址信息，监视同一个 master 的多个 Sentinel 可以自动发现对方。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Ch3 id=\"h3-5-\">心跳任务\u003C\u002Fh3>\n\u003Cp>在默认情况下，Sentinel 会以每秒一次的频率向所有与它创建了命令连接的节点（包括 master、slave、其他 Sentinel 在内）发送 PING 命令，并通过节点返回的 PING 命令回复来判断节点是否在线。\u003C\u002Fp>\n\u003Cp>节点对 PING 命令的回复可以分为以下两种情况：\u003C\u002Fp>\n\u003Col>\n\u003Cli>有效回复：实例返回+PONG、-LOADING、-MASTERDOWN 三种回复的其中一种。\u003C\u002Fli>\n\u003Cli>无效回复：实例返回除+PONG、-LOADING、-MASTERDOWN 三种回复之外的其他回复，或者在指定时限内没有返回任何回复。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Ch2 id=\"h2-6-redis-\">Redis 节点下线判断\u003C\u002Fh2>\n\u003Ch3 id=\"h3-7-\">主观下线判断\u003C\u002Fh3>\n\u003Cp>Sentinel 配置文件中的 down-after-milliseconds 选项指定了 Sentinel 判断 Redis 节点进入主观下线所需的时间长度：如果一个节点在 down-after-milliseconds 毫秒内，连续向 Sentinel 返回无效回复，那么 Sentinel 会修改这个节点的相关消息，把该节点的 flags 属性增加 SRI_S_DOWN 标识，以此来表示这个实例已经进入主观下线状态。\u003C\u002Fp>\n\u003Cp>用户设置的 down-after-milliseconds 选项的值，不仅会被 Sentinel 用来判断 master 的主观下线状态，还会被用于判断 master 属下的所有 slave，以及所有同样监视这个 master 的其他 Sentinel 的主观下线状态。举个例子，如果用户向 Sentinel 设置了以下配置：\u003C\u002Fp>\n\u003Cdiv class=\"ssr-code-block\" data-lang=\"text\">\u003Cdiv class=\"ssr-code-head\">\u003Cspan class=\"ssr-code-lang\">text\u003C\u002Fspan>\u003Cdiv class=\"ssr-code-actions\">\u003Cbutton class=\"ssr-code-fullscreen ssr-code-iconbtn\" type=\"button\" aria-label=\"全屏查看代码\" title=\"全屏\">\u003Csvg viewBox=\"0 0 24 24\" width=\"17\" height=\"17\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M5 5h6v2H7v4H5V5zm14 0v6h-2V7h-4V5h6zM5 19v-6h2v4h4v2H5zm14 0h-6v-2h4v-4h2v6z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003Cbutton class=\"ssr-code-copy ssr-code-iconbtn\" type=\"button\" aria-label=\"复制代码\" title=\"复制\">\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--default\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--success\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z\"\u002F>\u003C\u002Fsvg>\u003Csvg class=\"ssr-code-copy-icon ssr-code-copy-icon--error\" viewBox=\"0 0 24 24\" width=\"15\" height=\"15\" aria-hidden=\"true\">\u003Cpath fill=\"currentColor\" d=\"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z\"\u002F>\u003C\u002Fsvg>\u003C\u002Fbutton>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003Cdiv class=\"ssr-code-body\">\u003Cdiv class=\"ssr-code-pane\">\u003Cdiv class=\"ssr-code-gutter\" aria-hidden=\"true\">\u003Cspan class=\"ssr-code-num\">1\u003C\u002Fspan>\u003Cspan class=\"ssr-code-num\">2\u003C\u002Fspan>\u003C\u002Fdiv>\u003Cpre class=\"ssr-code-pre\">\u003Ccode class=\"hljs language-text\">\u003Cspan class=\"ssr-code-line\">sentinel monitor mymaster 127.0.0.1 6380 2\u003C\u002Fspan>\u003Cspan class=\"ssr-code-line\">sentinel down-after-milliseconds mymaster 30000\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\u003C\u002Fdiv>\u003C\u002Fdiv>\u003C\u002Fdiv>\n\u003Cp>那么 30000 毫秒会成为 Sentinel 判断 master、master 属下所有 slave、监视该 master 的其他 Sentinel 进入主观下线的标准。\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>\u003Ccode>注意：\u003C\u002Fcode>在 Redis 的分布式集群中，多个 Sentinel 设置的主观下线时长可能不同。这样可能会造成 Sentinel1 认为 master 主观下线，而 Sentinel2 并不认为 master 主观下线的情况。\u003C\u002Fp>\n\u003C\u002Fblockquote>\n\u003Ch3 id=\"h3-8-\">客观下线判断\u003C\u002Fh3>\n\u003Cp>当 Sentinel 将一个 master 判断为主观下线之后，为了确认这个 master 是否真的下线了，它会向同样监视这一 master 的其他 Sentinel 进行询问，看它们是否也认为 master 已经进入了下线状态（可以是主观下线或者客观下线）。当 Sentinel 从其他 Sentinel 那里接收到\u003Ccode>足够数量\u003C\u002Fcode>（数量大于等于 quorum ）的已下线判断之后，Sentinel 会将 master 的 flags 属性增加 SRI_O_DOWN 标识，表示 master 已经进入客观下线状态，并对 master 执行故障转移操作。\u003C\u002Fp>\n\u003Ch2 id=\"h2-9-sentinel-leader-\">Sentinel Leader 选举\u003C\u002Fh2>\n\u003Cp>当 Sentinel 节点对 master 做出客观下线判断后会由 Sentinel Leader 来完成后续的故障转移，即 Sentinel 集群中的节点也并非是对等节点，是存在 Leader 与 Follower 的。\u003C\u002Fp>\n\u003Cp>Sentinel 集群的 Leader 选举是通过 Raft 算法实现的。Raft 算法比较复杂，详见：\u003Ca href=\"https:\u002F\u002Fzhuanlan.zhihu.com\u002Fp\u002F32052223\">Raft 算法详解 - 知乎 \u003C\u002Fa>。这里仅简单介绍一下大致思路。\u003C\u002Fp>\n\u003Cp>每个选举参与者都具有当选 Leader 的资格，当其完成了“客观下线”判断后，就会立即“毛遂自荐”推选自己做 Leader，然后将自己的提案发送给所有参与者。其它参与者在收到提案后，只要自己手中的选票没有投出去，其就会立即通过该提案并将同意结果反馈给提案者，后续再过来的提案会由于该参与者没有了选票而被拒绝。当提案者收到了同意反馈数量大于等于 max(quorum，sentinelNum\u002F2+1)时，该提案者当选 Leader。\u003C\u002Fp>\n\u003Cblockquote>\n\u003Cp>说明：\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\n\u003Cp>在网络没有问题的前提下，基本就是谁先做出了“客观下线”判断，谁就会首先发起 Sentinel Leader 的选举，谁就会得到大多数参与者的支持，谁就会当选 Leader。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>Sentinel Leader 选举会在次故障转移发生之前进行。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003Cli>\n\u003Cp>故障转移结束后 Sentinel 不再维护这种 Leader-Follower 关系，即 Leader 不再存在。\u003C\u002Fp>\n\u003C\u002Fli>\n\u003C\u002Ful>\n\u003C\u002Fblockquote>\n\u003Ch2 id=\"h2-10-\">故障转移过程\u003C\u002Fh2>\n\u003Ch3 id=\"h3-11-\">整体过程\u003C\u002Fh3>\n\u003Cp>在选举产生出 Sentinel Leader 之后，Sentinel Leader 将对已下线的 master 执行故障转移操作，该操作包含以下三个步骤：\u003C\u002Fp>\n\u003Col>\n\u003Cli>在已下线 master 属下的所有 slave 里面，挑选出一个 slave，并将其角色转换为 master。\u003C\u002Fli>\n\u003Cli>让已下线 master 属下的所有 slave 改为复制新的 master。\u003C\u002Fli>\n\u003Cli>将已下线的 master 设置为新 master 的 slave，当这个旧的 master 重新上线（重启）时，它就会成为新 master 节点的 slave。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cp>故障转移操作第一步要做的就是在已下线的 master 属下的所有 slave 中，挑选出一个状态良好、数据完整的 slave，然后向这个 slave 发送 SLAVEOF no one 命令，将这个 slave 转换为 master。\u003C\u002Fp>\n\u003Ch3 id=\"h3-12-master-\">Master 选择算法\u003C\u002Fh3>\n\u003Cp>在进行故障转移时，Sentinel Leader 需要从所有 Redis 的 Slave 节点中选择出新的 Master。其选择算法为：\u003C\u002Fp>\n\u003Col>\n\u003Cli>删除列表中所有处于下线或者断线状态的从服务器，这可以保证列表中剩余的从服务器都是正常在线的。\u003C\u002Fli>\n\u003Cli>删除列表中所有最近五秒内没有回复过 Sentinel Leader 的 INFO 命令的从服务器，这可以保证列表中剩余的从服务器都是最近成功进行过通信的。\u003C\u002Fli>\n\u003Cli>删除所有与已下线 master 连接断开超过 down-after-milliseconds*10 毫秒的 slave：down-after-milliseconds 选项指定了判断主服务器下线所需的时间，而删除断开时长超过 down-after-milliseconds*10 毫秒的从服务器，则可以保证列表中剩余的从服务器都没有过早地与主服务器断开连接，换句话说，列表中剩余的从服务器保存的数据都是比较新的。\u003C\u002Fli>\n\u003Cli>删除 replica-priority（优先级）值为 0 的 Redis 节点。\u003C\u002Fli>\n\u003Cli>在剩余 Redis 节点中选择出 replica-priority 最小的的节点列表。如果只有一个节点，则直接返回，否则，继续。\u003C\u002Fli>\n\u003Cli>从优先级相同的节点列表中选择复制偏移量最大的节点（和已宕机的 master 节点的相似度最高）。如果只有一个节点，则直接返回，否则，继续。\u003C\u002Fli>\n\u003Cli>从复制偏移值量相同的节点列表中选择动态 ID 最小的节点返回。\u003C\u002Fli>\n\u003C\u002Fol>\n\u003Cp>对最后选择的 slave 发送 SLAVEOF no one 命令之后，Sentinel Leader 会以每秒一次的频率（平时是每十秒一次），向被升级的从服务器发送 INFO 命令，并观察命令回复中的角色（role）信息，当被升级服务器的 role 从原来的 slave 变为 master 时，Sentinel Leader 就知道被选中的从服务器已经顺利升级为 master 了。\u003C\u002Fp>\n\u003Ch3 id=\"h3-13-\">修改从服务器的复制目标\u003C\u002Fh3>\n\u003Cp>当新的主服务器出现之后，Sentinel Leader 下一步要做的就是，让已下线 master 属下的所有 slave 去复制新的 master，这一动作可以通过向 slave 发送 SLAVEOF 命令来实现。\u003C\u002Fp>\n\u003Ch3 id=\"h3-14-\">将旧的主服务器变为从服务器\u003C\u002Fh3>\n\u003Cp>故障转移操作最后要做的是，将已下线的 master 设置为新的 master 的 slave。因为旧的 master 已经下线，所以这种设置是保存在旧 master 的记录信息中，当旧 master 重新上线时，Sentinel （不是 Sentinel Leader）就会向它发送 SLAVEOF 命令，让它成为新 master 的 slave。\u003C\u002Fp>\n\u003Ch2 id=\"h2-15-\">节点上线\u003C\u002Fh2>\n\u003Ch3 id=\"h3-16--redis-\">原 Redis 节点上线\u003C\u002Fh3>\n\u003Cp>无论是原下线的 master 节点还是原下线的 slave 节点，只要是原 Redis 集群中的节点上线，只需启动 Redis 即可。因为每个 Sentinel 中都保存有原来其监控的所有 Redis 节点列表，Sentinel 会定时查看这些 Redis 节点是否恢复。如果查看到其已经恢复，则会命其从当前 master 进行数据同步。不过，如果是原 master 上线，在新 master 晋升后 Sentinel Leader 会立即先将原 master 节点更新为 slave，并同步新 master 节点的数据。\u003C\u002Fp>\n\u003Ch3 id=\"h3-17--redis-\">新 Redis 节点上线\u003C\u002Fh3>\n\u003Cp>如果需要在 Redis 集群中添加一个新的节点，其未曾出现在 Redis 集群中，则上线操作只能手工完成。即添加者在添加之前必须知道当前 master 是谁，然后在新节点启动后运行 slaveof 命令加入集群。\u003C\u002Fp>\n\u003Ch3 id=\"h3-18-sentinel-\">Sentinel 节点上线\u003C\u002Fh3>\n\u003Cp>如果要添加的是 Sentinel 节点，无论其是否曾经出现在 Sentinel 集群中，都需要手工完成。即添加者在添加之前必须知道当前 master 是谁，然后在配置文件中修改 sentinel monitor 属性，指定要监控的 master。然后启动 Sentinel 即可。\u003C\u002Fp>\n","初始化 Sentinel 初始化 Sentinel 的最后一步是创建连向 master 的网络连接，Sentinel 将成为 master 的客户端，它可以向主服务器发送命令，并从命令回复中获取相关的信息。 对于每个被 Sentinel 监视的主服务器来说，Sentinel 会创建两个连向主服务器的异步网络连接： 1.","https:\u002F\u002Fliubing-1314895948.cos.ap-chengdu.myqcloud.com\u002Fimg\u002F202305182250389.png"]