Spring Boot整合请参考本人博客:
Spring Boot整合Redis 3主3从分片集群—详细图文+代码,从新建项目开始手把手教学:redisTemplate
无中心化配置相对简单
Redis 集群实现水平扩容,分摊压力,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
多键操作是不被支持的
多键的Redis事务是不被支持的。lua脚本不被支持
#如果数据看起来太旧,故障主服务器的副本将避免启动故障转移。
#
#没有简单的方法可以让副本实际测量其“数据年龄”,因此需要执行以下两项检查:
#
# 1)如果有多个副本能够进行故障转移,它们会交换消息,以便让具有最佳复制偏移的副本获得优势(处理来自主服务器的更多数据)。副本将尝试按偏移量获取它们的等级,并在故障切换开始时应用与其等级成比例的延迟。
#
# 2)每个单独的复制品计算与它的主复制品的最后交互的时间。这可以是收到的最后一个ping或命令(如果主服务器仍处于“连接”状态),也可以是与主服务器断开连接后经过的时间(如果复制链路当前已断开)。如果最后一次交互太旧,副本将根本不会尝试故障转移。
[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
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
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
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
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
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
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
[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
[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]#
[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>
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]#
[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>
[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]#
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