接上一篇:Redis分布式缓存(二)| 主从架构搭建、全量/增量同步原理、主从同步优化
Redis提供了哨兵(Sentinel)机制来实现主从集群的自动故障恢复。
哨兵的结构如图:

哨兵的作用如下:
Sentinel会不断检查您的master和slave是否按预期工作master故障,Sentinel会将一个slave提升为master。当故障实例恢复后也以新的master为主Sentinel充当Redis客户端的服务发现来源,当集群发生故障转移时,会将最新信息推送给Redis的客户端Sentinel基于心跳机制监测服务状态,每隔1秒向集群的每个实例发送ping命令:
sentinel节点发现某实例未在规定时间响应,则认为该实例主观下线。quorum)的sentinel都认为该实例主观下线,则该实例客观下线。quorum值最好超过Sentinel实例数量的一半。
一旦发现master故障,sentinel需要在salve中选择一个作为新的master,选择依据是这样的:
slave节点与master节点断开时间长短,如果超过指定值(down-after-milliseconds * 10)则会排除该slave节点slave-priority值,越小优先级越高,如果是0则永不参与选举slave-prority一样,则判断slave节点的offset值,越大说明数据越新,优先级越高slave节点的运行id大小,越小优先级越高。当选出一个新的master后,该如何实现切换呢?
流程如下:
sentinel给备选的slave1节点发送slaveof no one命令,让该节点成为mastersentinel给所有其它slave发送slaveof 192.168.188.128 7002命令,让这些slave成为新master的从节点,开始从新的master上同步数据。sentinel将故障节点标记为slave,当故障节点恢复后会自动成为新的master的slave节点
Sentinel的三个作用是什么?
Sentinel如何判断一个redis实例是否健康?
故障转移步骤有哪些?
以下操作基于上一篇文章进行的,在基于上一篇文章已经设定了7001,7002,7003三个实例,以7001为master节点,7002,7003为slave节点的情况下操作。
上一篇文章:Redis分布式缓存(二)| 主从架构搭建、全量/增量同步原理、主从同步优化
这里我们搭建一个三节点形成的Sentinel集群,来监管之前的Redis主从集群。如图:

三个sentinel实例信息如下:
| 节点 | IP | PORT |
|---|---|---|
| s1 | 192.168.188.128 | 27001 |
| s2 | 192.168.188.128 | 27002 |
| s3 | 192.168.188.128 | 27003 |
要在同一台虚拟机开启3个实例,必须准备三份不同的配置文件和目录,配置文件所在目录也就是工作目录。
我们创建三个文件夹,名字分别叫s1、s2、s3:
# 进入/tmp目录
cd /tmp
# 创建目录
mkdir s1 s2 s3
如图:

然后我们在s1目录创建一个sentinel.conf文件,添加下面的内容:
创建文件:
touch s1/sentinel.conf
添加内容
port 27001
sentinel announce-ip 192.168.188.128
sentinel monitor mymaster 192.168.188.128 7001 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
dir "/tmp/s1"
解读:
port 27001:是当前·sentinel·实例的端口sentinel monitor mymaster 192.168.188.128 7001 2:指定主节点信息
mymaster:主节点名称,自定义,任意写192.168.188.128 7001:主节点的ip和端口2:选举master时的quorum值然后将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
为了方便查看日志,我们打开3个ssh窗口,分别启动3个redis实例,启动命令:
# 第1个
redis-sentinel /tmp/s1/sentinel.conf
# 第2个
redis-sentinel /tmp/s2/sentinel.conf
# 第3个
redis-sentinel /tmp/s3/sentinel.conf
启动后:

尝试让master节点7001宕机,查看sentinel日志,我这里是27003这个窗口:

查看7002的日志,我这里是7002升为master节点:

查看7003的日志:

在Sentinel集群监管下的Redis主从集群,其节点会因为自动故障转移而发生变化,Redis的客户端必须感知这种变化,及时更新连接信息。Spring的RedisTemplate底层利用lettuce实现了节点的感知和自动切换。
下面,我们通过一个测试来实现RedisTemplate集成哨兵机制。
自行创建一个SpringBoot工程
在项目的pom文件中引入依赖:
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/get/{key}")
public String test1(@PathVariable String key) {
return redisTemplate.opsForValue().get(key);
}
@GetMapping("/set/{key}/{value}")
public String test2(@PathVariable String key, @PathVariable String value) {
redisTemplate.opsForValue().set(key, value);
return "success";
}
}
然后在配置文件application.yml中指定redis的sentinel相关信息:
logging:
level:
io.lettuce.core: debug
pattern:
dateformat: MM-dd HH:mm:ss:SSS
# 哨兵模式
spring:
redis:
sentinel:
master: mymaster # 指定master名称
nodes:
- 192.168.188.128:27001
- 192.168.188.128:27002
- 192.168.188.128:27003
在项目的启动类中,添加一个新的bean:
@Bean
public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer(){
return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
}
这个bean中配置的就是读写策略,包括四种:
replicareplica)节点读取replica)节点读取,所有的slave都不可用才读取master大家自行测试,查看idea控制台的日志,可以看到读写分离时会自动切换节点,这里我就不做演示了。
下一篇:Redis分布式缓存(四)| 分片集群搭建、散列插槽、集群伸缩、故障转移、与SpringBoot集成分片集群