Redis Sentinel(哨兵)是 Redis 官方提供的集群管理工具,是 Redis 高可用的解决方案,本身是一个独立运行的进程,它可以监视多个 Master-Slave 集群,发现 Master 宕机之后,能进行自动切换,将该 Master 下的某个 Slave 晋升为 Master,继续处理请求。
Redis Sentinel 主要功能:
- 监控:检查主从服务器是否运行正常;
- 提醒:通过 API 向管理员或者其他应用程序发送故障通知;
- 自动故障迁移:主从切换。
Redis Sentinel 是一个分布式系统,你可以在一个架构中运行多个 Sentinel 进程,这些进程使用流言协议 (Gossip Protocols) 接收 Master 是否下线的信息,并使用投票协议来决定是否执行自动故障迁移,以及选择哪个 Slave 作为新的 Master。这个跟 Zookeeper 是比较类似的。
流言协议:
- 每个节点都随机地与对方通信,最终所有节点的状态达成一致;
- 种子节点定期随机向其他节点发送节点列表以及需要传播的消息;
- 不保证信息一定会传递给所有节点,但是最终会趋于一致。
1.架构说明
Redis Sentinel 架构:

Redis Sentinel 故障转移:

1、如果 master 宕机了,连接出现中断,多个 Sentinel 发现并确认 master 有问题;
2、选举出一个 Sentinel 作为领导;
3、选举一个 slave 作为新的 master;
4、通知其余 slave 成为新的 master 的 slave;
5、通知客户端主从变化;
6、Sentinel 等待老的 master 复活成为新 master 的 slave。

整个过程其实就是由我们手动处理故障变成了 Sentinel 进行故障发现、故障自动转移、通知客户端的过程。
Redis Sentinel 还可以监控多个 master-slave 集群,每个 master-slave 使用 master-name 作为标识,有效节省资源。
#安装与配置
最终要达到的效果如下:

Sentinel 的默认端口是 26379。生产环境中 Sentinel 节点个数应该为大于等于 3 且最好为奇数。
安装与配置大致过程如下:
1、配置开启主从节点;
2、配置开启 Sentinel 监控主节点(Sentinel 是特殊的 Redis,Sentinel 本身是不存储数据的,而且支持的命令非常有限,主要作用就是监控、完成故障转移、通知);
3、实际应该多机器部署 Sentinel,保证 Sentinel 高可用;
4、详细配置节点
详细配置过程:
Redis 主节点 redis/config/redis-7000.conf 配置(redis.conf 模板文件在 redis/redis.conf):
# 关闭保护模式
protected-mode no
# 配置启动端口
port 7000
# 配置后台启动
daemonize yes
# 修改pidfile指向路径 redis-${port}.pid
pidfile /var/run/redis-7000.pid
# 日志记录方式 redis-${port}.log
logfile "redis-7000.log"
# 配置dump数据存放目录
dir "/opt/soft/redis/data/"
# 配置dump数据文件名 redis-${port}.rdb
dbfilename dump-7000.rdb
启动命令:
redis-server redis-7000.conf
Redis 从节点 redis/config/redis-7001.conf 配置(redis-7002.conf 只是端口号不同):
# 关闭保护模式
protected-mode no
# 配置启动端口
port 7001
# 配置后台启动
daemonize yes
# 修改pidfile指向路径 redis-${port}.pid
pidfile /var/run/redis-7001.pid
# 日志记录方式 redis-${port}.log
logfile "redis-7001.log"
# 配置dump数据存放目录
dir "/opt/soft/redis/data/"
# 配置dump数据文件名 redis-${port}.rdb
dbfilename dump-7001.rdb
# 配置master的ip地址、端口,在Redis启动时会自动从master进行数据同步
slaveof 127.0.0.1 7000
启动命令:
redis-server redis-7001.conf
redis-server redis-7002.conf
Sentinel 节点 redis/config/redis-sentinel-26379.conf 配置(这里只给出其中一个配置,另外两个只是端口号不同,sentinel.conf 模板文件在 redis/sentinel.conf):
# 配置启动端口
port 26379
# 配置后台启动
daemonize yes
# 配置工作目录
dir "/opt/soft/redis/data/"
# 日志记录方式 redis-sentinel-${port}.log
logfile "redis-sentinel-26379.log"
# sentinel监听master的名字、ip、端口、几个sentinel认为master有问题就发生故障转移(最好配置sentinel节点的二分之一加一)
sentinel monitor mymaster 127.0.0.1 7000 2
# 每一个sentinel节点都会向master节点、slave节点和其他sentinel节点1秒钟ping一次。在down-after-milliseconds毫秒内没有进行回复,则判定该节点失败
sentinel down-after-milliseconds mymaster 30000
# slave复制时可以并行的个数,建议1,减轻master的压力
sentinel parallel-syncs mymaster 1
# 故障转移时间
sentinel failover-timeout mymaster 180000
启动命令:
redis-sentinel redis-sentinel-26379.conf
redis-sentinel redis-sentinel-26380.conf
redis-sentinel redis-sentinel-26381.conf
Sentinel 会自动发现 Redis slave,并写入到 redis-sentinel-${port}.conf,Sentinel 彼此之间也能自动感知到。
2.客户端连接
客户端初始化时连接的是 Sentinel 节点集合,不再是具体的 Redis 节点,但 Sentinel 只是配置中心不是代理。Sentinel 中的数据节点与普通数据节点没有区别。
1.请求响应流程
- 客户端遍历 Sentinel 节点集合,获取一个可用的 Sentinel 节点;
- 客户端执行一个 Sentinel 的 API:get-master-addr-by-name < masterName> 获取 master 节点真正的地址和端口;
- 客户端执行 role 或者 role replication 进行一次验证,验证是否是真的 master 节点;
- 连接 master 进行操作;
- 客户端会订阅 Sentinel 的某一个频道,这个频道里会有谁是 master 的一个变化,假如有变化,则发送消息给客户端,客户端再去新的 master 进行连接。
2.Java客户端连接
Jedis 方式:
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinelSet, poolConfig, timeout);
Jedis jedis = null;
try {
jedis = jedisSentinelPool.getResource();
// jedis command
} catch (Exception e) {
logger.error("error", e);
} finally {
if(jedis != null) {
jedis.close();
}
}
JedisSentinelPool 的实现原理:
先看下 JedisSentinelPool 的构造函数:
public JedisSentinelPool(String masterName

最低0.47元/天 解锁文章
3645

被折叠的 条评论
为什么被折叠?



