一、原始集群(6節點 3主3從):
(1)啟動集群:
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7001/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7002/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7003/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7004/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7005/redis.conf
[root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7006/redis.conf
(2)查看服務器運行狀態:
[root@bhz004 ~]# ps -el | grep redis
5 S 0 1999 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2003 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2007 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2011 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2017 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2023 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
(3)查看集群狀態
二、新集群操作
(4)我們新建倆個服務,按照之前搭建的集群方式新增倆個節點:(一主一從 master、slave)
Master:7007 Slave:7008
步驟一:創建7007/7008文件夾。拷貝redis.conf文件到對於的7007,7008目錄下 要進行修改配置文件。
[root@bhz004 redis-cluster]# mkdir 7007
[root@bhz004 redis-cluster]# mkdir 7008
[root@bhz004 redis-cluster]# cd 7001
[root@bhz004 7001]# cp redis.conf /usr/local/redis-cluster/7007/
[root@bhz004 7001]# cp redis.conf /usr/local/redis-cluster/7008/
[root@bhz004 7001]# vim /usr/local/redis-cluster/7007/redis.conf
修改內容如下:
port:7007
dir /usr/local/redis-cluster/7007/
cluster-config-file nodes7007.conf
[root@bhz004 7001]# vim /usr/local/redis-cluster/7008/redis.conf
修改內容如下:
port:7008
dir /usr/local/redis-cluster/7008/
cluster-config-file nodes7008.conf
步驟二:啟動7007和7008倆個服務並查看服務狀態。
[root@bhz004 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7007/redis.conf
[root@bhz004 7001]# /usr/local/redis/bin/redis-server /usr/local/redis-cluster/7008/redis.conf
[root@bhz004 7001]# ps -el | grep redis
(5)學習redis-trib命令使用:
[root@bhz004 local]# cd /usr/local/redis3.0/src
[root@bhz004 src]# redis-trib.rb
1 create:創建一個集群環境host1:port1 ... hostN:portN(集群中的主從節點比例)
2 call:可以執行redis命令
3 add-node:將一個節點添加到集群里,第一個參數為新節點的ip:port,第二個參數為集群中任意一個已經存在的節點的ip:port
4 del-node:移除一個節點
5 reshard:重新分片
6 check:檢查集群狀態
(6)新增一個主節點7007(master)
步驟一:使用add-node命令:綠色為新增節點,紅色為已知存在節點
[root@bhz004 7001]#
/usr/local/redis3.0/src/redis-trib.rb add-node 192.168.1.171:7007 192.168.1.171:7001
輸出如下:
>>> Adding node 192.168.1.171:7007 to cluster 192.168.1.171:7001
Connecting to node 192.168.1.171:7001: OK
Connecting to node 192.168.1.171:7006: OK
Connecting to node 192.168.1.171:7005: OK
Connecting to node 192.168.1.171:7004: OK
Connecting to node 192.168.1.171:7002: OK
Connecting to node 192.168.1.171:7003: OK
>>> Performing Cluster Check (using node 192.168.1.171:7001)
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
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.
Connecting to node 192.168.1.171:7007: OK
>>> Send CLUSTER MEET to node 192.168.1.171:7007 to make it join the cluster.
[OK] New node added correctly.
步驟二:查看集群狀態:
[root@bhz004 src]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.171 -p 7001
192.168.1.171:7001> cluster nodes
注意:當添加節點成功以后,新增的節點不會有任何數據,因為它沒有分配任何的slot(hash槽)。我們需要為新節點手工分配slot。
(7) 為7007分配slot槽。
步驟一:使用redis-trib命令,找到集群中的任意一個主節點(紅色位置表現集群中的任意一個主節點),對其進行重新分片工作。
[root@bhz004 7001]# /usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.171:7001
輸出如下:
>>> Performing Cluster Check (using node 192.168.1.171:7001)
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots: (0 slots) master
0 additional replica(s)
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
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.
(提示一)
How many slots do you want to move (from 1 to 16384)? 200
(提示二)
What is the receiving node ID? 382634a4025778c040b7213453fd42a709f79e28
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:all
Ready to move 200 slots.
Source nodes:
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10923-16383 (5461 slots) master
1 additional replica(s)
Destination node:
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots: (0 slots) master
0 additional replica(s)
Resharding plan:(分片執行計划日志)
Moving slot 5461 from 8aac82b63d42a1989528cd3906579863a5774e77
...
Moving slot 0 from 614d0def75663f2620b6402a017014b57c912dad
...
Moving slot 10923 from 83df08875c7707878756364039df0a4c8658f272
...
(提示三)
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 65 from 192.168.1.171:7001 to 192.168.1.171:7007:
...
Moving slot 10923 from 192.168.1.171:7003 to 192.168.1.171:7007:
...
Moving slot 5527 from 192.168.1.171:7002 to 192.168.1.171:7007:
...
1提示一:是希望你需要多少個槽移動到新的節點上,可以自己設置,比如200個槽。
2提示二:是你需要把這200個slot槽移動到那個節點上去(需要指定節點id),並且下個提示是輸入all為從所有主節點(7001 7002 7003)中分別抽取響應的槽數(一共為200個槽到指定的新節點中!,並且會打印執行分片的計划。)
3提示三:輸入yes確認開始執行分片任務。在最后我們再次看一下集群狀態:
如上圖所示,現在我們的7007已經有slot槽了,也就是說可以在7007上進行讀寫數據啦!到此為止我們的7007已經加入到集群中啦,並且是主節點(Master)
(8) 添加從節點(7008)到集群中去。
步驟一:還是需要執行add-node命令:
[root@bhz004 7001]#
/usr/local/redis3.0/src/redis-trib.rb add-node 192.168.1.171:7008 192.168.1.171:7001
提示添加成功后我們繼續看一下集群的狀態:
如圖所示,還是一個master節點,沒有被分配任何的slot槽。
步驟二:我們需要執行replicate命令來指定當前節點(從節點)的主節點id為哪個。
首先需要登錄新加的7008節點的客戶端,然后使用集群命令進行操作,把當前的7008(slave)節點指定到一個主節點下(這里使用之前創建的7007主節點,紅色表示節點id)
[root@bhz004 ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.1.171 -p 7008
192.168.1.171:7008> cluster replicate 382634a4025778c040b7213453fd42a709f79e28
192.168.1.171:7008> OK(提示OK則操作成功)
我們繼續看一下當前集群的狀態,如下圖:我們已經成功的把7008放到7007這個主節點下面了,到此為止我們已經成功的添加完一個從節點了。
(9) 我們可以對集群進行操作,來驗證下是否可以進行讀寫(當然可以)。
(10) 我們現在嘗試刪除一個節點(7008 slave)
步驟一:刪除從節點7008,輸入del-node命令,指定刪除節點ip和端口,以及節點id(紅色為7008節點id)
[root@bhz004 7001]# /usr/local/redis3.0/src/redis-trib.rb
del-node 192.168.1.171:7008 97b0e0115326833724eb0ffe1d0574ee34618e9f
輸出如下:
>>> Removing node 97b0e0115326833724eb0ffe1d0574ee34618e9f from cluster 192.168.1.171:7008
Connecting to node 192.168.1.171:7008: OK
Connecting to node 192.168.1.171:7003: OK
Connecting to node 192.168.1.171:7006: OK
Connecting to node 192.168.1.171:7002: OK
Connecting to node 192.168.1.171:7005: OK
Connecting to node 192.168.1.171:7001: OK
Connecting to node 192.168.1.171:7004: OK
Connecting to node 192.168.1.171:7007: OK
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
步驟二:再次查看一下集群狀態,如下圖所示,我們已經成功的移除了7008 slave節點,另外我們發現移除一個節點以后,當前節點的服務進程也會隨之銷毀。可以使用ps命令查看當前的服務(ps -el | grep redis),發現少了一個運行的server,也就是剛移除的7008從節點。
(11) 最后,我們嘗試刪除之前加入的主節點7007,這個步驟會相對比較麻煩一些,因為主節點的里面是有分配了slot槽的,所以我們這里必須先把7007里的slot槽放入到其他的可用主節點中去,然后再進行移除節點操作才行,不然會出現數據丟失問題。
步驟一:刪除7007(master)節點之前,我們需要先把其全部的數據(slot槽)移動到其他節點上去(目前只能把master的數據遷移到一個節點上,暫時做不了平均分配功能)。
[root@bhz004 7001]# /usr/local/redis3.0/src/redis-trib.rb reshard 192.168.1.171:7007
輸出如下:
>>> Performing Cluster Check (using node 192.168.1.171:7007)
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots:0-65,5461-5527,10923-10988 (199 slots) master
0 additional replica(s)
S: fa299e41c173fa807ba04684c2f5e5e185d5f7d0 192.168.1.171:7006
slots: (0 slots) slave
replicates 83df08875c7707878756364039df0a4c8658f272
S: a69b98937844c6050ee5885266ccccb185a3f36a 192.168.1.171:7004
slots: (0 slots) slave
replicates 614d0def75663f2620b6402a017014b57c912dad
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:66-5460 (5395 slots) master
1 additional replica(s)
M: 8aac82b63d42a1989528cd3906579863a5774e77 192.168.1.171:7002
slots:5528-10922 (5395 slots) master
1 additional replica(s)
S: adb99506ddccad332e09258565f2e5f4f456a150 192.168.1.171:7005
slots: (0 slots) slave
replicates 8aac82b63d42a1989528cd3906579863a5774e77
M: 83df08875c7707878756364039df0a4c8658f272 192.168.1.171:7003
slots:10989-16383 (5395 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.
How many slots do you want to move (from 1 to 16384)? 199
(注釋:這里不會是正好200個槽)
What is the receiving node ID? 614d0def75663f2620b6402a017014b57c912dad
(注釋:這里是需要把數據移動到哪?7001的主節點id)
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:382634a4025778c040b7213453fd42a709f79e28
(注釋:這里是需要數據源,也就是我們的7007節點id)
Source node #2:done
(注釋:這里直接輸入done 開始生成遷移計划)
Ready to move 199 slots.
Source nodes:
M: 382634a4025778c040b7213453fd42a709f79e28 192.168.1.171:7007
slots:0-65,5461-5527,10923-10988 (199 slots) master
0 additional replica(s)
Destination node:
M: 614d0def75663f2620b6402a017014b57c912dad 192.168.1.171:7001
slots:66-5460 (5395 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 0 from 382634a4025778c040b7213453fd42a709f79e28
...
Do you want to proceed with the proposed reshard plan (yes/no)? Yes
(注釋:這里輸入yes開始遷移)
Moving slot 0 from 192.168.1.171:7007 to 192.168.1.171:7001:
...
到此為止我們已經成功的把7007主節點的數據遷移到7001上去了,我們可以看一下現在的集群狀態如下圖,你會發現7007下面已經沒有任何數據(slot)槽了,證明遷移成功!
步驟二:最后我們直接使用del-node命令刪除7007主節點即可(紅色表示7007的節點id)。
[root@bhz004 7001]# /usr/local/redis3.0/src/redis-trib.rb del-node
192.168.1.171:7007 382634a4025778c040b7213453fd42a709f79e28
輸出如下:
>>> Removing node 382634a4025778c040b7213453fd42a709f79e28 from cluster 192.168.1.171:7007
Connecting to node 192.168.1.171:7007: OK
Connecting to node 192.168.1.171:7006: OK
Connecting to node 192.168.1.171:7004: OK
Connecting to node 192.168.1.171:7001: OK
Connecting to node 192.168.1.171:7002: OK
Connecting to node 192.168.1.171:7005: OK
Connecting to node 192.168.1.171:7003: OK
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node。
最后:我們查看集群狀態,一切還原為最初始狀態啦!OK 結束!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:redis.properties" />
<context:component-scan base-package="com.x.redis.dao">
</context:component-scan>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="hostport1" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7770" />
</bean>
<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7771" />
</bean>
<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7772" />
</bean>
<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7773" />
</bean>
<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7774" />
</bean>
<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="10.16.68.92" />
<constructor-arg name="port" value="7775" />
</bean>
<bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<ref bean="hostport1" />
<ref bean="hostport2" />
<ref bean="hostport3" />
<ref bean="hostport4" />
<ref bean="hostport5" />
<ref bean="hostport6" />
</set>
</constructor-arg>
<constructor-arg name="timeout" value="6000" />
<constructor-arg name="poolConfig">
<ref bean="jedisPoolConfig" />
</constructor-arg>
</bean>
</beans>