抛个问题:如果从服务器挂了,没关系,我们一般会有多个从服务器,其他的请求可以交由没有挂的从服务器继续处理。如果主服务器挂了,怎么办?因为我们的写请求由主服务器处理,只有一台主服务器,那就无法处理写请求了?
Redis提供了哨兵(Sentinal)机制供我们解决上面的情况。如果主服务器挂了,我们可以将从服务器升级为主服务器,等到旧的主服务器(挂掉的那个)重连上来,会将它(挂掉的主服务器)变成从服务器。
这个过程叫做主备切换(故障转移)
在正常的情况下,主从加哨兵(Sentinal)机制是这样子的:
正常情况下
主服务器挂了,主从复制操作就中止了,并且哨兵系统是可以察觉出主服务挂了。:
Sentinel可以察觉主服务掉线,复制操作中止。
Redis提供哨兵机制可以将选举一台从服务器变成主服务器
选举一台从服务器变成主服务器
然后旧的主服务器如果重连了,会变成从服务器:
旧的主服务器如果重连了,会变成从服务器
这篇文章主要讲讲Redis的哨兵(Sentinal)机制的一些细节。希望看完对大家有所帮助~
一、哨兵(Sentinal)机制
High Availability: Redis Sentinel is the official high availability solution for Redis.
哨兵(Sentinal)机制主要用于实现Redis的高可用性,主要的功能如下:
Monitoring. Sentinel constantly checks if your master and slave instances are working as expected.
Sentinel不停地监控Redis主从服务器是否正常工作
Notification. Sentinel can notify the system administrator, another computer programs, via an API, that something is wrong with one of the monitored Redis instances.
如果某个Redis实例有故障,那么哨兵负责发送消息通知管理员
Automatic failover. If a master is not working as expected, Sentinel can start a failover process where a slave is promoted to master, the other additional slaves are reconfigured to use the new master, and the applications using the Redis server informed about the new address to use when connecting.
如果主服务器挂掉了,会自动将从服务器提升为主服务器(包括配置都会修改)。
Configuration provider. Sentinel acts as a source of authority for clients service discovery: clients connect to Sentinels in order to ask for the address of the current Redis master responsible for a given service. If a failover occurs, Sentinels will report the new address.
Sentinel可以作为配置中心,能够提供当前主服务器的信息。
下面来具体讲讲Sentinel是如何将从服务器提升为主服务器的。
tips:Sentinel可以让我们的Redis实现高可用,Sentinel作为这么一个组件,自身也必然是高可用的(不可能是单点的)
1.1启动和初始化Sentinel
首先我们要知道的是:Sentinel本质上只是一个运行在特殊模式下的Redis服务器。因为Sentinel做的事情和Redis服务器是不一样的,所以它们的初始化是有所区别的(比如,Sentinel在初始化的时候并不会载入AOF/RDB文件,因为Sentinel根本就不用数据库)。
然后,在启动的时候会将普通Redis服务器的代码替换成Sentinel专用代码。(所以Sentinel虽然作为Redis服务器,但是它不能执行SET、DBSIZE等等命令,因为命令表的代码被替换了)
接着,初始化Sentinel的状态,并根据给定的配置文件初始化Sentinel监视的主服务器列表。
作者:Java3y
链接:https://www.jianshu.com/p/d7ac81abe452
共同學習,寫下你的評論
評論加載中...
作者其他優質文章