Redis 哨兵机制与实现

一、当没有设置哨兵机制

1.redis主从之前是通过ping来检查对方是否存活,当从节点挂机,主节点会直接将其剔除,当主节点挂掉的时候,从节点这里会显示主节点状态为 down,不会重新选举出来新的主节点,此时我们只有从节点在工作了,所以现在我们只能做读的操作,不能进行写操作了(因为我们在配置文件中设置了从节点的 read-only)

redis能否做到主动请求的方式 redis实现机制_redis

二、哨兵机制

1.配置哨兵机制,其实就是让第三方介入,当我们的主节点挂掉后,他就会为我们选举出一个新的主节点来提供服务,配置哨兵之后欧他就会监控我们所有的redis,当主节点挂掉后,他就会做一个调配将其中一个slave转变成一个master

2.哨兵我们也会配置集群一起来监控redis

redis能否做到主动请求的方式 redis实现机制_配置文件_02


2.我们到redis的解压目录下将哨兵模式的配置文件sentinel.conf拷贝到我们的安装目录下

cp sentinel.conf /usr/local/redis/

redis能否做到主动请求的方式 redis实现机制_java_03


3.对sentinel.conf配置文件进行配置

远程传输命令:scp sentinel.conf root@192.168.1.190:/usr/local/redis

#
# For example you may use one of the following:
#
# bind 127.0.0.1 192.168.1.1
#
rotected-mode no			// 是否开启保护模式,即只能访问指定的IP


# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379			// 端口号


# By default Redis Sentinel does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis-sentinel.pid when
# daemonized
daemonize yes		// 启动后是否后台运行

# When running daemonized, Redis Sentinel writes a pid file in
# /var/run/redis-sentinel.pid by default. You can specify a custom pid file
# location here.
pidfile /var/run/redis-sentinel.pid		// pid路径

# Specify the log file name. Also the empty string can be used to force
# Sentinel to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/usr/local/redis/sentinel/redis-sentinel.log"		// 哨兵模式运行日志


# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
dir /usr/local/redis/sentinel		// 哨兵模式工作目录


# Note: master name should not include special characters or spaces.
# The valid charset is A-z 0-9 and the three characters ".-_".
sentinel monitor capuda-master 192.168.1.188 6379 2  // 主节点别名  主节点IP和端口号  哨兵数量(当有注定数量的哨兵发现主节点已经宕机了,就会开始故障转移,选举一个新的master出来)


# However you can have Redis instances without the authentication enabled
# mixed with Redis instances requiring the authentication (as long as the
# password set is the same for all the instances requiring the password) as
# the AUTH command will have no effect in Redis instances with authentication
# switched off.
#
# Example:
#
sentinel auth-pass capuda-master root		// 主节点的密码  


# Number of milliseconds the master (or any attached replica or sentinel) should
# be unreachable (as in, not acceptable reply to PING, continuously, for the
# specified period) in order to consider it in S_DOWN state (Subjectively
# Down).
#
# Default is 30 seconds.
sentinel down-after-milliseconds mymaster 30000		// 哨兵确认主节点已经宕机的时间点,默认30秒


# How many replicas we can reconfigure to point to the new replica simultaneously
# during the failover. Use a low number if you use the replicas to serve query
# to avoid that all the replicas will be unreachable at about the same
# time while performing the synchronization with the master.
sentinel parallel-syncs mymaster 1		// 同步数据的个数,即当我们的主节点挂了,选举出来新的master,如果slave有5个,同步个数设置为1,那就是一个一个同步数据,如果设置为2,那就是2个同时同步数据


# Default is 3 minutes.
sentinel failover-timeout mymaster 180000		//如果当前哨兵在指定时间内没有做故障转移,那就回有另外一台哨兵来进行操作

4.启动哨兵

1.使用下面的命令会报错,内容是没有配置文件

redis-sentinel
2.使用配置文件启动,需要在我们放置sentinel.conf配置文件的地方使用
/usr/local/redis
redis-sentinel sentinel.conf

使用下面的命令查看是否启动成功,如果成功就会出现sentinel哨兵的进程

ps -ef | grep redis

redis能否做到主动请求的方式 redis实现机制_redis能否做到主动请求的方式_04

5.启用哨兵,当主节点宕机后,就会选举出现新的主节点就行工作,并且当之前的主节点恢复后,就会变成从节点,不会变成主节点

6.使用下面命令登录哨兵客户端

redis-cli -p 26379

redis能否做到主动请求的方式 redis实现机制_配置文件_05

使用下面的命令查看master,之后便会展示主节点的相关配置和ip

sentinel master capuda-master(配置文件中设定的master昵称)

redis能否做到主动请求的方式 redis实现机制_java_06

使用下面的命令查看有多少天slave和slave的相关信息信息,内容和上面差不多,展示对应的从节点的端口号和相关配置信息

sentinel slaves capuda-master(配置文件中设定的master昵称)

使用下面的命令查看有多少台哨兵,已经相关信息

sentinel sentinels capuda-master(配置文件中设定的master昵称)

7.部署约定:

1.哨兵节点应该部署为计数,因为在当有一台哨兵觉得这个主节点宕机了是主观下线,因为我们是出于一个网络环境下,有些时候可能是因为网络原因导致主节点反应过慢,这个时候需要另外一台哨兵再去确认下,如果两台都觉的当前主节点不行了,这是客观下线,这个是时候才能判断当前主节点真的宕机了,这种判断方式也比较稳妥
2.选举也是哨兵进行选举的,如果是偶数的话就存在平票的情况,导致无法选举主节点,最好是使用奇书,比较方便,之后选举出来的新的主节点,会同步诗句,这个我们在配置文件中配置过一个 sync 的配置,他的数量就对应者 一次可以和多少个从节点同步数据

redis能否做到主动请求的方式 redis实现机制_配置文件_07


redis能否做到主动请求的方式 redis实现机制_配置文件_08


redis能否做到主动请求的方式 redis实现机制_redis_09


redis能否做到主动请求的方式 redis实现机制_redis能否做到主动请求的方式_10