key通过hash算法计算出一个值,然后拿这个值%16384
得到一个数(假如是37)为key的保存位置,然后再存入相应的存储空间位置
假如又增加了一个节点,之前每个节点都会拿出部分槽给新的节点
如果是去除节点,则把被去除节点的槽加入到存在的节点当作
Cluster配置
添加节点
cluster-enabled yes|no
cluster配置文件名,该文件属于自动生成,仅用于快速查找文件并查询文件内容
cluster-config-file
节点服务响应超时时间,用于判定该节点是否下线或切换为从节点
cluster-node-timeout
master连接的slave最小数量
cluster-migration-barrier
Cluster节点操作命令
查看集群节点信息
cluster nodes
进入一个从节点 redis,切换其主节点
cluster replicate <master-id>
发现一个新节点,新增主节点
cluster meet ip:port
忽略一个没有solt的节点
cluster forget <id>
手动故障转移
cluster failover
首先清空之前的数据
[root@maomao ~]# cd /usr/local/redis/data/
[root@maomao data]# ls
appendonly-6379.aof dump-6379.rdb dump.rdb
[root@maomao data]# rm -rf *
配置文件添加
cluster-enabled yes # 开启cluster
cluster-config-file nodes-6379.conf # 配置文件和名字
cluster-node-timeout 10000 # cluster下线时间,超时时间为10秒
复制六个配置文件
sed 's/6379/6380/g' redis-6379.conf > redis-6380.conf
sed 's/6379/6381/g' redis-6379.conf > redis-6381.conf
sed 's/6379/6382/g' redis-6379.conf > redis-6382.conf
sed 's/6379/6383/g' redis-6379.conf > redis-6383.conf
sed 's/6379/6384/g' redis-6379.conf > redis-6384.conf
sed 's/6379/6385/g' redis-6379.conf > redis-6385.conf
检查
cat redis-6385.conf |grep -v '#' |grep -v '^$'
开启服务
redis-server redis_config/redis-6379.conf
...
redis-server redis_config/redis-6384.conf
[root@maomao bin]# ps aux |grep redis-
root 2116 0.2 0.5 165020 10824 pts/0 Sl+ 02:23 0:00 redis-server 127.0.0.1:6379 [cluster]
root 2121 0.2 0.5 165020 10664 pts/7 Sl+ 02:23 0:00 redis-server 127.0.0.1:6380 [cluster]
root 2126 0.2 0.5 165020 10720 pts/6 Sl+ 02:23 0:00 redis-server 127.0.0.1:6381 [cluster]
root 2131 0.2 0.6 171164 12768 pts/2 Sl+ 02:23 0:00 redis-server 127.0.0.1:6382 [cluster]
root 2136 0.2 0.5 165020 10760 pts/5 Sl+ 02:24 0:00 redis-server 127.0.0.1:6383 [cluster]
root 2141 0.1 0.5 165020 10788 pts/1 Sl+ 02:24 0:00 redis-server 127.0.0.1:6384 [cluster]
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379
Adding replica 127.0.0.1:6384 to 127.0.0.1:6380
Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 56aa54f614a2fc8f99e30a109d5e6a91d723193e 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
M: 083eb7940fb5d5444563e3a48d36a1609b8d5af4 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
M: f515c69256533ebb17d2aabefe81d6e630f7eee5 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
S: d25e2d50c3b6136cafc3cdd0a5fef0e86fbf66fb 127.0.0.1:6382
replicates 56aa54f614a2fc8f99e30a109d5e6a91d723193e
S: 86f0f88285ca648df41e8f348e41cf5ef520bdae 127.0.0.1:6383
replicates 083eb7940fb5d5444563e3a48d36a1609b8d5af4
S: 738d205c923600d6f7c8ea8df8313527b8267b95 127.0.0.1:6384
replicates f515c69256533ebb17d2aabefe81d6e630f7eee5
Can I set the above configuration? (type 'yes' to accept):
6379 6380 6381 是master
6382 6383 6384 是slave
slots 就是槽 三个master分的槽都一样多 平均分配
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 127.0.0.1:6379)
M: 56aa54f614a2fc8f99e30a109d5e6a91d723193e 127.0.0.1:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: d25e2d50c3b6136cafc3cdd0a5fef0e86fbf66fb 127.0.0.1:6382
slots: (0 slots) slave
replicates 56aa54f614a2fc8f99e30a109d5e6a91d723193e
S: 86f0f88285ca648df41e8f348e41cf5ef520bdae 127.0.0.1:6383
slots: (0 slots) slave
replicates 083eb7940fb5d5444563e3a48d36a1609b8d5af4
M: 083eb7940fb5d5444563e3a48d36a1609b8d5af4 127.0.0.1:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: f515c69256533ebb17d2aabefe81d6e630f7eee5 127.0.0.1:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 738d205c923600d6f7c8ea8df8313527b8267b95 127.0.0.1:6384
slots: (0 slots) slave
replicates f515c69256533ebb17d2aabefe81d6e630f7eee5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
可以查看日志
[root@maomao bin]# cat /usr/local/redis/data/nodes-6379.conf
集群搭建完成
测试
[root@maomao bin]# redis-cli
127.0.0.1:6379> set name maomao
(error) MOVED 5798 127.0.0.1:6380 # 叫我们去这个槽操作
[root@maomao bin]# redis-cli -c # 操控集群使用 redis-cli -c
127.0.0.1:6379> set name maomao
-> Redirected to slot [5798] located at 127.0.0.1:6380
OK
我们在6382获取key 失败
[root@maomao bin]# redis -cli -c -p 6382
-bash: redis: command not found
[root@maomao bin]# redis-cli -c -p 6382
127.0.0.1:6382> get name
-> Redirected to slot [5798] located at
127.0.0.1:6380
"maomao"
127.0.0.1:6380> exit
连接到6380就能够获取key
[root@maomao bin]# redis-cli -c -p 6380
127.0.0.1:6380> get name
"maomao"
我们将一个主节点下线,然后查看信息
127.0.0.1:6380> cluster nodes
86f0f88285ca648df41e8f348e41cf5ef520bdae 127.0.0.1:6383@16383 slave 083eb7940fb5d5444563e3a48d36a1609b8d5af4 0 1618815771240
2 connected
083eb7940fb5d5444563e3a48d36a1609b8d5af4 127.0.0.1:6380@16380 myself,master - 0 1618815770000 2 connected 5461-10922
56aa54f614a2fc8f99e30a109d5e6a91d723193e 127.0.0.1:6379@16379 master,fail - 1618815743184 1618815740000 1 disconnected
d25e2d50c3b6136cafc3cdd0a5fef0e86fbf66fb 127.0.0.1:6382@16382 master - 0 1618815768232 7 connected 0-5460
f515c69256533ebb17d2aabefe81d6e630f7eee5 127.0.0.1:6381@16381 master - 0 1618815770000 3 connected 10923-16383
738d205c923600d6f7c8ea8df8313527b8267b95 127.0.0.1:6384@16384 slave f515c69256533ebb17d2aabefe81d6e630f7eee5 0 1618815770238
3 connected
master,fail意思是master会回来
一、正则表达式概述 二、正则表达式在VBScript中的应用 三、正则表达式在VavaScr...
一石激起千层浪,继中国区浩浩荡荡的大裁员告一段落之后,甲骨文并未因此收起手...
ADO对象: Connection Command Recordset Record Stream ASP支持的对象很多,可...
【排序算法】之lowb三人组冒泡、插入、选择 什么是lowb三人组 冒泡排序bubble so...
本文将研究 ES6 的 for ... of 循环。 旧方法 在过去,有两种方法可以遍历 javas...
vbs:把一段文字中指定字符颜色变成红色的正则 functionc(Tstr,Word) Dimre Setre...
计算属性computed: 支持缓存,只有依赖数据发生改变,才会重新进行计算 不支持...
前言 相信大家都知道在IDE中代码的智能提示几乎都是标配,虽然一些文本编辑器也...
歌词编辑器 歌词编辑器 第一步:选择要播放的歌曲并播放 第二步:填写全部的歌词...
微信文件传输助手是微信电脑版与手机微信之间相互传输图片等文件的好工具,但很...