redis-cluster是redis官方写的集群功能,目前还未加入稳定版,据说会在3.0上线的时候加入,但是开发版已经包含该功能
开发版的下载方式是git clone https://github.com/antirez/redis.git,前提是安装了git
安装配置过程参考官方已经出的教程http://redis.io/topics/cluster-tutorial,全英文
1、源码下载,make
2、新建6个instance,3个master,3个slave;
新建实例之前要改一下redis.conf的内容,先改端口号,再改下面的cluster配置项
3、src下面的ruby脚本,建立集群: --replicas 1的意思是每个master有1个slave。
./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
|
如果脚本报错:
custom_require.rb:36:in `require': cannot load such file -- redis (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from ./redis-trib.rb:25:in `<main>'
那么说明Ruby的redis接口没有安装,可以通过sudo gem install redis 进行安装。
可能会询问是否保存设置
Can I set the above configuration? (type 'yes' to accept):
回答yes
再输出一连串信息后,会显示OK
[OK] All 16384 slots covered.
这样redis-cluster集群就启动了
查看集群目前状况:
$ redis-cli -c -p 7000
127.0.0.1:7000> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:0 cluster_stats_messages_sent:8770 cluster_stats_messages_received:8770
测试存值取值:
127.0.0.1:7000> set foo bar
-> Redirected to slot [12182] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7000
OK
127.0.0.1:7000> get foo
-> Redirected to slot [12182] located at 127.0.0.1:7002
"bar"
127.0.0.1:7000> get hello
-> Redirected to slot [866] located at 127.0.0.1:7000
"world"