HM-SpringCloud微服务系列10.3【Redis哨兵】

1. 哨兵的作用与原理

1.1 哨兵的作用

image

1.2 服务状态监控

image
image

1.3 选举新的master

image

1.4 实现故障转移

image
image
image
image
image

1.5 小结

  1. Sentinel的三个作用是什么?
    • 监控
    • 故障转移
    • 通知
  2. Sentinel如何判断一个redis实例是否健康?
    • 每隔1秒发送一次ping命令,如果超过一定时间没有相向则认为是主观下线
    • 如果大多数sentinel都认为实例主观下线,则判定服务下线
  3. 故障转移步骤有哪些?
    • 首先选定一个slave作为新的master,执行slaveof no one
    • 然后让所有节点都执行slaveof 新master
    • 修改故障节点配置,添加slaveof 新master

2. 搭建哨兵集群

2.1 集群结构

这里我们搭建一个三节点形成的Sentinel集群,来监管之前的Redis主从集群。如图:
image
三个sentinel实例信息如下:

节点 IP PORT
s1 10.193.193.141 27001
s2 10.193.193.141 27002
s3 10.193.193.141 27003

2.2 准备实例和配置

要在同一台虚拟机开启3个实例,必须准备三份不同的配置文件和目录,配置文件所在目录也就是工作目录。
我们在/tmp目录下创建三个文件夹,名字分别叫s1、s2、s3:

# 进入/tmp目录
cd /tmp
# 创建目录
mkdir s1 s2 s3

image

然后我们在s1目录创建一个sentinel.conf文件,添加下面的内容:

port 27001
sentinel announce-ip 10.193.193.141
sentinel monitor mymaster 10.193.193.141 7001 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
dir "/tmp/s1"

解读:

  • port 27001:是当前sentinel实例的端口
  • sentinel monitor mymaster 10.193.193.141 7001 2:指定主节点信息
    • mymaster:主节点名称,自定义,任意写
    • 10.193.193.141 7001:主节点的ip和端口
    • 2:选举master时的quorum值

image
image

然后将s1/sentinel.conf文件拷贝到s2、s3两个目录中(在/tmp目录执行下列命令):

# 方式一:逐个拷贝
cp s1/sentinel.conf s2
cp s1/sentinel.conf s3
# 方式二:管道组合命令,一键拷贝
echo s2 s3 | xargs -t -n 1 cp s1/sentinel.conf

修改s2、s3两个文件夹内的配置文件,将端口分别修改为27002、27003:

sed -i -e 's/27001/27002/g' -e 's/s1/s2/g' s2/sentinel.conf
sed -i -e 's/27001/27003/g' -e 's/s1/s3/g' s3/sentinel.conf

image
image

2.3 启动

由于xshell个人版最多只能打开四个窗口,此处双开进行演示
第一个xshell:
为了方便查看日志,我们打开3个ssh窗口,分别启动3个redis实例,并参考上节https://www.cnblogs.com/yppah/p/16183291.html建立好主从结构集群
image
image
image
image

第二个xshell
然后再打开三个窗口,分别启动sentinel实例,启动命令:

# 第1个
redis-sentinel s1/sentinel.conf
# 第2个
redis-sentinel s2/sentinel.conf
# 第3个
redis-sentinel s3/sentinel.conf

image
image
image

2.4 测试

尝试让master节点7001宕机(手动ctrl+c停止7001)
image
可以看到7002和7003连接不上主节点,也在报错
image
image
查看sentinel日志:
image
image
image

image

查看7003的日志:可以发现7003已由从节点变为主节点
image

image

查看7002的日志:7002此时认7003为新的master,并连接上
image

image
老师演示时是他的7002被选为了新master

此时,我们重启7001
image
image

3. RedisTemplate的哨兵模式

在Sentinel集群监管下的Redis主从集群,其节点会因为自动故障转移而发生变化,Redis的客户端必须感知这种变化,及时更新连接信息。Spring的RedisTemplate底层利用lettuce实现了节点的感知和自动切换。
首先,我们引入课前资料提供的Demo工程:
image
image
image
image

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

image

spring:
  redis:
    sentinel:
      master: mymaster # 指定master名称
      nodes: # 指定redis-sentinel集群信息
        - 10.193.193.141:27001
        - 10.193.193.141:27002
        - 10.193.193.141:27003

image
image

@Bean
public LettuceClientConfigurationBuilderCustomizer configurationBuilderCustomizer(){
	return configBuilder -> configBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}

image
image
image
image


清空控制台,测试一下写操作
image
image


清空控制台,测试一下主从故障切换
回到xshell,手动ctrl+c将当前主节点7003停止
image
查看一下哨兵后台日志,可以看出主节点由7003变回为7001
image
看一下IDEA控制台日志,可以看出RedisTemplate也发现了问题并进行了记录
image
image

posted @   yub4by  阅读(109)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示