上一篇文章讲解了redis主从复制,可是当主服务器宕机后,我们需要手动把一台从服务器切换为主服务器,这费事费力,还会造成一段时间内服务不可用。显然我们并不推荐这种方式,更多时候,我们优先考虑哨兵模式。
哨兵模式
Sentinel哨兵是redis官方提供的高可用方案,可以用它来监控多个Redis服务实例的运行情况。哨兵是一个独立的进程,作为进程,它会独立运行。Redis Sentinel是在多个Sentinel进程环境下互相协作工作的。
下面我们将搭建两个哨兵
准备二台redis服务
复制一份redis,并将其配置文件端口号改为6380,配置为从节点。
启动两台redis服务
准备哨兵配置文件
sentinel
# 配置文件:sentinel.conf,在sentinel运行期间是会被动态修改的
# sentinel如果重启时,根据这个配置来恢复其之前所监控的redis集群的状态
# 绑定IP
#bind 0.0.0.0
# 后台运行
daemonize yes
# 哨兵的端口,客户端通过这个端口来发现redis
port 26380
# 哨兵自己的IP,手动设定也可自动发现,用于与其他哨兵通信
# sentinel announce-ip
# 临时文件夹,如果没有需要自行创建
dir "./tmp"
# 日志
logfile "./sentinel.log"
# sentinel监控的master的名字叫做mymaster,初始地址为 127.0.0.1 6379,2代表两个及以上哨兵认定为死亡,才认为是真的死亡
sentinel monitor mymaster 127.0.0.1 6379 2
# 发送心跳PING来确认master是否存活
# 如果master在“一定时间范围”内不回应PONG 或者是回复了一个错误消息,那么这个sentinel会主观地(单方面地)认为这个master已经不可用了
sentinel down-after-milliseconds mymaster 1000
# 如果在该时间(ms)内未能完成failover操作,则认为该failover失败
sentinel failover-timeout mymaster 3000
# 指定了在执行故障转移时,最多可以有多少个从Redis实例在同步新的主实例,在从Redis实例较多的情况下这个数字越小,同步的时间越长,完成故障转移所需的时间就越长
sentinel config-epoch mymaster 0
将该配置文件复制到从节点目录下并修改端口号为26380,启动哨兵:
redis-server sentinel.conf --sentinel
启动两个哨兵。
java哨兵配置
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sunyuqi</groupId>
<artifactId>redis_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>9</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
哨兵配置类
package com.sunyuqi.sentinel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@Configuration
@Profile("sentinel")
class SentinelRedisAppConfig {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
.master("mymaster")
// 哨兵地址
.sentinel("127.0.0.1", 26380)
.sentinel("127.0.0.1", 26381);
return new LettuceConnectionFactory(sentinelConfig);
}
}
引导类
package com.sunyuqi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplicationDemo {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplicationDemo.class,args);
}
}
测试类
package com.sunyuqi.sentinel;
import com.sunyuqi.SpringbootApplicationDemo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootApplicationDemo.class)
@ActiveProfiles("sentinel") // 设置profile
public class SentinelTests {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Test
public void test() throws InterruptedException {
// 每个一秒钟,操作一下redis
int i = 0;
while (true) {
i++;
stringRedisTemplate.opsForValue().set("test-value", String.valueOf(i));
System.out.println("修改test-value值为: " + i);
Thread.sleep(1000L);
}
}
}
我们每隔一秒钟修改redis中的数据,执行过程中,我们关闭掉主节点(端口号为6379)。
此时启动6379节点
进入客户端
查看数据
可以看到数据还在,并且主节点自动切换成了端口号为6380的服务。
slave选举方案
当主服务器宕机后,哨兵进行选举,选举一个从节点成为主节点。
哨兵会从上到下依次筛选
slave节点状态:
哨兵会选取一个健康的节点(哨兵会通过ping命令去检查一个节点的健康状态)
优先级:
redis.conf中的一个配置项:slave-priority值越小,优先级越高。
数据同步情况:
Replication offset processed(选择数据最完善的节点)
最小的run id:
run id比较方案:字典顺序,ASCII码
选举结束后自动进行主从切换:
针对即将成为master的slave节点,将其撤出主从集群(自动执行slaveof no one),针对其他slave节点,使他们成为新的master的从属节点(自动执行:slaveof new_master_host new_master_port)
哨兵服务部署方案
官方建议:一主二从,三个哨兵