Featured image of post Redis一主双从读写分离+Sentinel高可用配置并整合SpringBoot

Redis一主双从读写分离+Sentinel高可用配置并整合SpringBoot

前言

单台Redis的搭建:redis安装并开机自启动整合springboot实战,安装Redis。

服务器规划

  • 192.168.79.120:Master+Sentinel
  • 192.168.79.121:Slave+Sentinel
  • 192.168.79.122:Slave+Sentinel

Sentinel可以单独部署一个集群,这里是和Redis实例在同一台机器上。Sentinel的个数要为奇数个,因为设计到投票选举Master的问题。

配置一主两从

修改配置文件

192.168.79.120

1
2
3
4
5
6
bind 0.0.0.0
dir /usr/local/redis/db
masterauth root1234
replica-read-only yes
requirepass root1234
appendonly yes

192.168.79.121

1
2
3
4
5
6
7
8
bind 0.0.0.0
dir /usr/local/redis/db
masterauth root1234
# Slave只读
replica-read-only yes
requirepass root1234
appendonly yes
replicaof 192.168.79.120 6379

192.168.79.122

1
2
3
4
5
6
7
8
bind 0.0.0.0
dir /usr/local/redis/db
masterauth root1234
# Slave只读
replica-read-only yes
requirepass root1234
appendonly yes
replicaof 192.168.79.120 6379

配置完成之后,重新启动Redis

1
2
/etc/init.d/redis stop
/etc/init.d/redis start

连接Redis,info repliccation命令查看,从下图可以看出,一主两从配置成功。接下来配置Sentinel。

配置Sentinel

Redis Sentinel为Redis提供了高可用性。实际上,这意味着使用Sentinel可以创建Redis部署,该部署可以在没有人工干预的情况下抵抗某些类型的故障。我们一般在配置Redis一主多从的架构时,很多情况都会做读写分离,Master只写,Slave只读。当Master宕机等不可用时,这是整个Redis集群就变得不可用了,这时候就用到Sentinel了,Sentinel集群会在Master不可用时,重新在Slave中选举出新的Master,从而使得Redis集群变得可用。也是官方提供Redis高可用的方案。

下面进行Sentinel的配置:

1
2
3
4
5
6
7
sentinel auth-pass mymaster root1234
sentinel down-after-milliseconds mymaster 10000
sentinel monitor mymaster 192.168.79.120 6379 2
bind 0.0.0.0
daemonize yes
logfile /usr/local/redis/sentinel/redis-sentinel.log
dir /usr/local/redis/sentinel

每台机器上都启动Sentinel,这时候可以查看Sentinel的日志了。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@localhost sentinel]# tail -f redis-sentinel.log
4593:X 03 May 2021 22:32:35.051 # Configuration loaded
4594:X 03 May 2021 22:32:35.063 * Increased maximum number of open files to 10032 (it was originally set to 1024).
4594:X 03 May 2021 22:32:35.065 * Running mode=sentinel, port=26379.
4594:X 03 May 2021 22:32:35.065 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4594:X 03 May 2021 22:32:35.065 # Sentinel ID is 5b9c2ed15b4a31394a9b3b75f46bbe98a4bd9078
4594:X 03 May 2021 22:32:35.065 # +monitor master mymaster 192.168.79.120 6379 quorum 2
4594:X 03 May 2021 22:32:35.065 * +slave slave 192.168.79.121:6379 192.168.79.121 6379 @ mymaster 192.168.79.120 6379
4594:X 03 May 2021 22:32:35.068 * +slave slave 192.168.79.122:6379 192.168.79.122 6379 @ mymaster 192.168.79.120 6379
4594:X 03 May 2021 22:32:37.060 * +sentinel sentinel b5b7d7bea19d8e34d7936a8cdc683472b61fb4af 192.168.79.121 26379 @ mymaster 192.168.79.120 6379
4594:X 03 May 2021 22:32:37.137 * +sentinel sentinel 5830ec2a347b91d24bb3a2ffce0a2a68f03d0216 192.168.79.122 26379 @ mymaster 192.168.79.120 6379

说明:

192.168.79.120 6379的Redis名字mymaster,有两个副本

+monitor master mymaster 192.168.79.120 6379 quorum 2

下面两条,有两个Slave加入Master。Master名字为mymaster,所在位置是192.168.79.120 6379

+slave slave 192.168.79.121:6379 192.168.79.121 6379 @ mymaster 192.168.79.120 6379

+slave slave 192.168.79.122:6379 192.168.79.122 6379 @ mymaster 192.168.79.120 6379

下面两条,有两个Sentinel加入。Master名字为mymaster,所在位置是192.168.79.120 6379

+sentinel sentinel b5b7d7bea19d8e34d7936a8cdc683472b61fb4af 192.168.79.121 26379 @ mymaster 192.168.79.120 6379

+sentinel sentinel 5830ec2a347b91d24bb3a2ffce0a2a68f03d0216 192.168.79.122 26379 @ mymaster 192.168.79.120 6379

演示Master故障

这时候关掉Master,然后重新查看Redis集群情况

这时候Sentinel的日志输出:192.168.79.120:6379不可用了,mymaster切换到了192.168.79.122:6379。可见Sentinel保证了Redis集群的可用性。当192.168.79.120:6379重新启动,此时该节点就会被降为Slave了,这也可以在Sentinel的日志里看到。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
4594:X 03 May 2021 22:46:24.333 # +sdown master mymaster 192.168.79.120 6379
4594:X 03 May 2021 22:46:24.365 # +new-epoch 3
4594:X 03 May 2021 22:46:24.365 # +vote-for-leader 5830ec2a347b91d24bb3a2ffce0a2a68f03d0216 3
4594:X 03 May 2021 22:46:24.396 # +odown master mymaster 192.168.79.120 6379 #quorum 3/2
4594:X 03 May 2021 22:46:24.397 # Next failover delay: I will not start a failover before Mon May  3 22:52:24 2021
4594:X 03 May 2021 22:46:24.812 # +config-update-from sentinel 5830ec2a347b91d24bb3a2ffce0a2a68f03d0216 192.168.79.122 26379 @ mymaster 192.168.79.120 6379
4594:X 03 May 2021 22:46:24.812 # +switch-master mymaster 192.168.79.120 6379 192.168.79.122 6379
4594:X 03 May 2021 22:46:24.812 * +slave slave 192.168.79.121:6379 192.168.79.121 6379 @ mymaster 192.168.79.122 6379
4594:X 03 May 2021 22:46:24.812 * +slave slave 192.168.79.120:6379 192.168.79.120 6379 @ mymaster 192.168.79.122 6379
4594:X 03 May 2021 22:46:34.861 # +sdown slave 192.168.79.120:6379 192.168.79.120 6379 @ mymaster 192.168.79.122 6379

整合SpringBoot

1
2
3
4
<dependency>
	<groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在配置文件中配置

1
2
3
4
5
6
7
spring:
  redis:
    database: 1
    password: root1234
    sentinel:
      master: mymaster
      nodes: 192.168.79.120:26379,192.168.79.121:26379,192.168.79.122:26379

在使用Sentinel的集群中,我们的客户端是通过Sentinel读写Redis的,所有要配置sentinel实例。这样就可以通过SpringBoot的RedisTemplate去操作Redis了。