即使有了主從複製,每個數據庫都要保存整個集羣中的所有數據,容易形成木桶效應。Redis3.0版本的一大特性就是集羣(Cluster),接下來我們一起學習集羣。
1.添加6479,6480,6481三個端口(可以參考我的另一篇博客redis主從複製)
2.在redis.conf配置文件中,開啓集羣,cluster-enabledyes
3.指定集羣的配置文件,cluster-config-file"nodes-xxxx.conf"
4.創建集羣
4.1環境安裝(ruby)
因爲redis-trib.rb是有ruby語言編寫的所以需要安裝ruby環境。
yum -y install zlib ruby rubygems
gem install redis
手動安裝:
rz上傳redis-3.2.1.gem
gem install -l redis-3.2.1.gem
4.2 創建集羣
首先,進入redis的安裝包路徑下:cd /usr/local/src/redis/redis-3.0.2
[root@localhost src]# ls
adlist.c crc64.h mkreleasehdr.sh redis-cli.c sort.o
adlist.h crc64.o multi.c redis-cli.o sparkline.c
adlist.o db.c multi.o redis.h sparkline.h
ae.c db.o networking.c redis.o sparkline.o
ae_epoll.c debug.c networking.o redis-sentinel syncio.c
ae_evport.c debug.o notify.c redis-server syncio.o
ae.h dict.c notify.o redis-trib.rb testhelp.h
ae_kqueue.c dict.h object.c release.c t_hash.c
ae.o dict.o object.o release.h t_hash.o
ae_select.c endianconv.c pqsort.c release.o t_list.c
anet.c endianconv.h pqsort.h replication.c t_list.o
anet.h endianconv.o pqsort.o replication.o t_set.c
anet.o fmacros.h pubsub.c rio.c t_set.o
aof.c help.h pubsub.o rio.h t_string.c
aof.o hyperloglog.c rand.c rio.o t_string.o
asciilogo.h hyperloglog.o rand.h scripting.c t_zset.c
bio.c intset.c rand.o scripting.o t_zset.o
bio.h intset.h rdb.c sds.c util.c
bio.o intset.o rdb.h sds.h util.h
bitops.c latency.c rdb.o sds.o util.o
bitops.o latency.h redisassert.h sentinel.c valgrind.sup
blocked.c latency.o redis-benchmark sentinel.o version.h
blocked.o lzf_c.c redis-benchmark.c setproctitle.c ziplist.c
執行命令: ./redis-trib.rb create --replicas 0 192.168.2.86:6479192.168.2.86:6480 192.168.2.86:6481 之後輸入yes確認
--replicas 0:指定了從數據的數量爲0
注意:這裏不能使用127.0.0.1,否則在Jedis客戶端使用時無法連接到!
(不好意思啊,因爲我已經裝好了,所以借用了一下網上的圖)
4.3 測試
[root@localhost /]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>
這裏請注意,如果你完全按照我的步驟來的,直接輸入redis-cli是不行的,它默認是6379,所以你得輸入redis-cli -p 6479
127.0.0.1:6479> set abc 123
(error) MOVED 7638 192.168.2.86:6480
127.0.0.1:6479>
因爲abc的hash槽信息是在6480上,現在使用redis-cli連接的6479,無法完成set操作,需要客戶端跟蹤重定向。
redis-cli -c -p 6479
[root@localhost /]# redis-cli -c -p 6479
127.0.0.1:6479> set abc 123
-> Redirected to slot [7638] located at 192.168.2.86:6480
OK
192.168.2.86:6480>
這裏它由6479跳到6480了,我們回到6479看下有沒有abc的數據
[root@localhost /]# redis-cli -c -p 6479
127.0.0.1:6479> get abc
-> Redirected to slot [7638] located at 192.168.2.86:6480
"123"
192.168.2.86:6480>
依然重定向到6480,不過已經能夠得到數據了
在這裏需要解釋下,它重定向到6480,是因爲設置abc的值時,首先計算了abc的插槽值,它是在6480端口的插槽值之間的