Spring Boot整合请参考本人博客:
Spring Boot整合Redis 3主3从分片集群—详细图文+代码,从新建项目开始手把手教学:redisTemplate

一、介绍

  • 无中心化配置相对简单
  • Redis 集群实现水平扩容,分摊压力,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
  • Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
  • 多键操作是不被支持的
  • 多键的Redis事务是不被支持的。lua脚本不被支持

二、相关配置

2.1 cluster-enabled

  • cluster-enabled yes
  • 是否打开集群模式

2.2 cluster-config-file

  • cluster-config-file nodes-6379.conf
  • 节点配置文件名

2.3 cluster-node-timeout

  • cluster-node-timeout 15000
  • 节点超时时间,集群自动进行主从切换。毫秒

2.4 cluster-replica-validity-factor

#如果数据看起来太旧,故障主服务器的副本将避免启动故障转移。
#
#没有简单的方法可以让副本实际测量其“数据年龄”,因此需要执行以下两项检查:
#
# 1)如果有多个副本能够进行故障转移,它们会交换消息,以便让具有最佳复制偏移的副本获得优势(处理来自主服务器的更多数据)。副本将尝试按偏移量获取它们的等级,并在故障切换开始时应用与其等级成比例的延迟。
#
# 2)每个单独的复制品计算与它的主复制品的最后交互的时间。这可以是收到的最后一个ping或命令(如果主服务器仍处于“连接”状态),也可以是与主服务器断开连接后经过的时间(如果复制链路当前已断开)。如果最后一次交互太旧,副本将根本不会尝试故障转移。
  • cluster-replica-validity-factor 10
  • 副本有效性因子(第2种方式)
  • 如果自上次与主服务器交互以来,经过的时间超过(节点超时*副本有效性因子)+复制副本周期,副本服务器将不会执行故障转移。
  • 0:获得最大的可用性,这意味着副本将始终尝试故障转移主服务器

2.5 cluster-migration-barrier

  • cluster-migration-barrier 1
  • 保证redis集群中不会出现裸奔(这个主节点没有对应的从节点)的主节点
  • 当某个主节点没有对应的从节点时,其他主节点在保证自己在分配从节点给裸奔节点后还有1个从节点的情况下,将分配一个从节点过去

2.6 cluster-require-full-coverage

  • cluster-require-full-coverage yes
  • yes:如果某一段插槽的主从都挂掉,整个集群都挂掉。
  • no:如果某一段插槽的主从都挂掉,该插槽数据全都不能使用,也无法存储。

2.7 cluster-replica-no-failover

  • cluster-replica-no-failover no
  • 当主服务器宕机,这个从服务器不能被当成主服务器
  • 但是,仍可手动强制切换

2.8 cluster-allow-reads-when-down

  • cluster-allow-reads-when-down no
  • no:当集群被标记为失败时,Redis集群中的节点将停止服务所有流量,无论是当节点无法达到主节点的仲裁量,或当完全覆盖没有满足。 这可以防止从不知道集群变化的节点读取可能不一致的数据。
  • yes:允许在故障状态期间进行读操作

三、实操部署

3.1 部署介绍

  • 三主三从
  • 192.168.100.103:6379、192.168.100.103:6380、192.168.100.103:6381、192.168.100.103:6389、192.168.100.103:6390、192.168.100.103:6391

3.2 环境搭建

3.2.1 创建配置文件

[root@demo03 ~]# cd /opt/myredis/
[root@demo03 myredis]# ll
总用量 120
-rw-r--r--. 1 root root   205 8月  22 18:52 redis_6379.conf
-rw-r--r--. 1 root root   205 8月  22 18:52 redis_6380.conf
-rw-r--r--. 1 root root   205 8月  22 18:52 redis_6381.conf
-rw-r--r--. 1 root root   205 8月  22 18:52 redis_6389.conf
-rw-r--r--. 1 root root   205 8月  22 18:53 redis_6390.conf
-rw-r--r--. 1 root root   205 8月  22 19:34 redis_6391.conf
-rw-rw-r--. 1 root root 92232 8月  21 15:18 redis.conf

3.2.2 redis_6379.conf

include /opt/myredis/redis.conf
pidfile "/var/run/redis_6379.pid"
port 6379
dbfilename "dump_6379.rdb"
masterauth "123456"
cluster-enabled yes
cluster-config-file node_6379.conf
cluster-node-timeout 15000

3.2.3 redis_6380.conf

include /opt/myredis/redis.conf
pidfile "/var/run/redis_6380.pid"
port 6380
dbfilename "dump_6380.rdb"
masterauth "123456"
cluster-enabled yes
cluster-config-file node_6380.conf
cluster-node-timeout 15000

3.2.4 redis_6381.conf

include /opt/myredis/redis.conf
pidfile "/var/run/redis_6381.pid"
port 6381
dbfilename "dump_6381.rdb"
masterauth "123456"
cluster-enabled yes
cluster-config-file node_6381.conf
cluster-node-timeout 15000

3.2.5 redis_6389.conf

include /opt/myredis/redis.conf
pidfile "/var/run/redis_6389.pid"
port 6389
dbfilename "dump_6389.rdb"
masterauth "123456"
cluster-enabled yes
cluster-config-file node_6389.conf
cluster-node-timeout 15000

3.2.6 redis_6390.conf

include /opt/myredis/redis.conf
pidfile "/var/run/redis_6390.pid"
port 6390
dbfilename "dump_6390.rdb"
masterauth "123456"
cluster-enabled yes
cluster-config-file node_6390.conf
cluster-node-timeout 15000

3.2.7 redis_6391.conf

include /opt/myredis/redis.conf
pidfile "/var/run/redis_6391.pid"
port 6391
dbfilename "dump_6391.rdb"
masterauth "123456"
cluster-enabled yes
cluster-config-file node_6391.conf
cluster-node-timeout 15000

3.2.8 注意

  • /opt/myredis/redis.conf:在redis初始配置文件的基础上将bind注释掉,dir /opt/myredis,requirepass 123456。
  • 主机和从机都需要设置:masterauth “123456”。且主从密码必须一致,因为主从会切换。密码不同的话,如果A主机变成从机,B从机变成主机,那C从机配置的masterauth对B主机来说就是错的了

3.3 启动服务

[root@demo03 myredis]# redis-server redis_6379.conf 
[root@demo03 myredis]# redis-server redis_6380.conf 
[root@demo03 myredis]# redis-server redis_6381.conf 
[root@demo03 myredis]# redis-server redis_6389.conf 
[root@demo03 myredis]# redis-server redis_6390.conf 
[root@demo03 myredis]# redis-server redis_6391.conf 
[root@demo03 myredis]# ps aux | grep redis
root       6980  0.2  0.4 165100 12576 ?        Ssl  20:53   0:00 redis-server *:6379 [cluster]
root       6986  0.2  0.3 165100 10528 ?        Ssl  20:53   0:00 redis-server *:6380 [cluster]
root       6992  0.2  0.3 165100 10528 ?        Ssl  20:53   0:00 redis-server *:6381 [cluster]
root       6998  0.2  0.3 165100 10528 ?        Ssl  20:53   0:00 redis-server *:6389 [cluster]
root       7004  1.1  0.3 165100 10544 ?        Ssl  20:53   0:00 redis-server *:6390 [cluster]
root       7010  1.4  0.3 165100 10540 ?        Ssl  20:53   0:00 redis-server *:6391 [cluster]
root       7016  0.0  0.0 112824   988 pts/0    S+   20:53   0:00 grep --color=auto redis

3.4 开启集群

  • redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.100.103:6379 192.168.100.103:6380 192.168.100.103:6381 192.168.100.103:6389 192.168.100.103:6390 192.168.100.103:6391
  • -a:节点密码
  • –cluster-replicas 1:采用最简单的方式配置集群,一台主机,一台从机,正好三组
[root@demo03 myredis]# redis-cli -a 123456 --cluster create --cluster-replicas 1 192.168.100.103:6379 192.168.100.103:6380 192.168.100.103:6381 192.168.100.103:6389 192.168.100.103:6390 192.168.100.103:6391
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.100.103:6390 to 192.168.100.103:6379
Adding replica 192.168.100.103:6391 to 192.168.100.103:6380
Adding replica 192.168.100.103:6389 to 192.168.100.103:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4bcde0c5b4e682ef50f13c8d855e9c64cebf289f 192.168.100.103:6379
   slots:[0-5460] (5461 slots) master
M: 2d7da2354f15799f219d0e2d14761ad3688d4ae6 192.168.100.103:6380
   slots:[5461-10922] (5462 slots) master
M: 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 192.168.100.103:6381
   slots:[10923-16383] (5461 slots) master
S: 07765e5b3f9d767c02987d2944331ccdbec1cad4 192.168.100.103:6389
   replicates 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e
S: ffa79ccc62ea9cb82a493525c147096780ea6775 192.168.100.103:6390
   replicates 4bcde0c5b4e682ef50f13c8d855e9c64cebf289f
S: 4ad9bee72b20ab3e80068c720af90b63f9d635e8 192.168.100.103:6391
   replicates 2d7da2354f15799f219d0e2d14761ad3688d4ae6
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.100.103:6379)
M: 4bcde0c5b4e682ef50f13c8d855e9c64cebf289f 192.168.100.103:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 2d7da2354f15799f219d0e2d14761ad3688d4ae6 192.168.100.103:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 07765e5b3f9d767c02987d2944331ccdbec1cad4 192.168.100.103:6389
   slots: (0 slots) slave
   replicates 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e
S: 4ad9bee72b20ab3e80068c720af90b63f9d635e8 192.168.100.103:6391
   slots: (0 slots) slave
   replicates 2d7da2354f15799f219d0e2d14761ad3688d4ae6
S: ffa79ccc62ea9cb82a493525c147096780ea6775 192.168.100.103:6390
   slots: (0 slots) slave
   replicates 4bcde0c5b4e682ef50f13c8d855e9c64cebf289f
M: 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 192.168.100.103:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@demo03 myredis]#

3.5 模拟节点宕机

3.5.1 查看集群信息

  • 第一组:6379(主)、6390(从)
  • 第二组:6380(主)、6391(从)
  • 第三组:6381(主)、6389(从)
[root@demo03 myredis]# redis-cli -c -p 6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> cluster nodes
2d7da2354f15799f219d0e2d14761ad3688d4ae6 192.168.100.103:6380@16380 master - 0 1661173073466 2 connected 5461-10922
07765e5b3f9d767c02987d2944331ccdbec1cad4 192.168.100.103:6389@16389 slave 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 0 1661173071456 3 connected
4bcde0c5b4e682ef50f13c8d855e9c64cebf289f 192.168.100.103:6379@16379 myself,master - 0 1661173072000 1 connected 0-5460
4ad9bee72b20ab3e80068c720af90b63f9d635e8 192.168.100.103:6391@16391 slave 2d7da2354f15799f219d0e2d14761ad3688d4ae6 0 1661173071000 2 connected
ffa79ccc62ea9cb82a493525c147096780ea6775 192.168.100.103:6390@16390 slave 4bcde0c5b4e682ef50f13c8d855e9c64cebf289f 0 1661173070450 1 connected
0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 192.168.100.103:6381@16381 master - 0 1661173072460 3 connected 10923-16383
127.0.0.1:6379>

3.5.2 模拟宕机

  • 6379宕机
127.0.0.1:6379> shutdown
not connected> 
[root@demo03 myredis]# ps aux | grep redis
root       6986  0.3  0.3 165100 10356 ?        Ssl  20:53   0:01 redis-server *:6380 [cluster]
root       6992  0.4  0.3 165100 10316 ?        Ssl  20:53   0:01 redis-server *:6381 [cluster]
root       6998  0.3  0.3 165100 10408 ?        Ssl  20:53   0:01 redis-server *:6389 [cluster]
root       7004  0.3  0.3 165100 10448 ?        Ssl  20:53   0:01 redis-server *:6390 [cluster]
root       7010  0.3  0.3 165100 10396 ?        Ssl  20:53   0:01 redis-server *:6391 [cluster]
root       7091  0.0  0.0 112824   984 pts/0    S+   21:00   0:00 grep --color=auto redis
[root@demo03 myredis]#

3.5.3 查看集群信息

  • 第一组:6379(失败)、6390(主)
  • 第二组:6380(主)、6391(从)
  • 第三组:6381(主)、6389(从)
[root@demo03 myredis]# redis-cli -c -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380> cluster nodes
4bcde0c5b4e682ef50f13c8d855e9c64cebf289f 192.168.100.103:6379@16379 master,fail - 1661173191721 1661173187702 1 disconnected
2d7da2354f15799f219d0e2d14761ad3688d4ae6 192.168.100.103:6380@16380 myself,master - 0 1661173235000 2 connected 5461-10922
07765e5b3f9d767c02987d2944331ccdbec1cad4 192.168.100.103:6389@16389 slave 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 0 1661173232167 3 connected
ffa79ccc62ea9cb82a493525c147096780ea6775 192.168.100.103:6390@16390 master - 0 1661173235178 7 connected 0-5460
4ad9bee72b20ab3e80068c720af90b63f9d635e8 192.168.100.103:6391@16391 slave 2d7da2354f15799f219d0e2d14761ad3688d4ae6 0 1661173234176 2 connected
0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 192.168.100.103:6381@16381 master - 0 1661173234000 3 connected 10923-16383
127.0.0.1:6380>

3.5.4 节点恢复

  • 6379 重新启动
[root@demo03 myredis]# redis-server redis_6379.conf 
[root@demo03 myredis]# ps aux | grep redis
root       6986  0.4  0.3 165100 10460 ?        Ssl  20:53   0:02 redis-server *:6380 [cluster]
root       6992  0.4  0.3 165100 10272 ?        Ssl  20:53   0:02 redis-server *:6381 [cluster]
root       6998  0.3  0.3 165100 10336 ?        Ssl  20:53   0:01 redis-server *:6389 [cluster]
root       7004  0.4  0.3 165100 10520 ?        Ssl  20:53   0:01 redis-server *:6390 [cluster]
root       7010  0.4  0.3 165100 10344 ?        Ssl  20:53   0:01 redis-server *:6391 [cluster]
root       7100  0.0  0.2  25100  7808 pts/0    S+   21:00   0:00 redis-cli -c -p 6380 -a 123456
root       7127  0.2  0.3 243952 10708 ?        Ssl  21:01   0:00 redis-server *:6379 [cluster]
root       7143  0.0  0.0 112824   988 pts/1    S+   21:01   0:00 grep --color=auto redis
[root@demo03 myredis]#

3.5.5 查看集群信息

  • 第一组:6379(从)、6390(主)
  • 第二组:6380(主)、6391(从)
  • 第三组:6381(主)、6389(从)
127.0.0.1:6380> 
127.0.0.1:6380> cluster nodes
4bcde0c5b4e682ef50f13c8d855e9c64cebf289f 192.168.100.103:6379@16379 slave ffa79ccc62ea9cb82a493525c147096780ea6775 0 1661173291000 7 connected
2d7da2354f15799f219d0e2d14761ad3688d4ae6 192.168.100.103:6380@16380 myself,master - 0 1661173290000 2 connected 5461-10922
07765e5b3f9d767c02987d2944331ccdbec1cad4 192.168.100.103:6389@16389 slave 0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 0 1661173288763 3 connected
ffa79ccc62ea9cb82a493525c147096780ea6775 192.168.100.103:6390@16390 master - 0 1661173291781 7 connected 0-5460
4ad9bee72b20ab3e80068c720af90b63f9d635e8 192.168.100.103:6391@16391 slave 2d7da2354f15799f219d0e2d14761ad3688d4ae6 0 1661173290000 2 connected
0a885b61c796a3bc1f99f0e0ac71543fcc2b852e 192.168.100.103:6381@16381 master - 0 1661173290000 3 connected 10923-16383