@SailorXiao
2016-08-26T00:33:02.000000Z
字数 2880
阅读 3574
redis
redis3.0
本文主要是在centos7上安装和配置redis集群实战
参考:
http://hot66hot.iteye.com/blog/2050676
集群教程:
http://redisdoc.com/topic/cluster-tutorial.html#id2
到redis官网redis.io或者redis中文官网www.redis.cn上(http://www.redis.cn/download.html)去下载最新的版本,我下载的是redis3.0.3版本,下载后,解压,make后,在src下已经生成redis-cli,redis-server等可执行文件
将redis-cli,redis-server,redis-trib.rb cp一个到/usr/bin下或者你目录的bin下
在这里用最简单的例子,启动3个redis实例,端口分别为7000,7002,7003,修改对应配置,并且进行启动
修改配置主要包括:
- 端口:修改对应成7000,7001,7002
- 对应的log,pid,data目录
- 集群配置:
cluster-enabled yes
cluster-config-file nodes-700x.conf #集群在启动时会自动生成
cluster-node-timeout 5000
启动redis集群
./redis-trib.rb create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002
启动集群时,会有如下一些提示:
Creating cluster
Connecting to node 127.0.0.1:7000: OK
Connecting to node 127.0.0.1:7001: OK
Connecting to node 127.0.0.1:7002: OK
Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
M: 36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002
slots:10923-16383 (5461 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:7000)
M: 36fce4bf6c829797c433c08b045a8c482e07fbc1 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 7e46925ca9781a403facf450aa0b4b8149ad9c27 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: e89c01d7cc2a83f072f66d0c2fb1f5cd92c66888 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
使用客户端连接集群
使用redis-cli客户端连接集群
[root@bogon bin]# ./redis-cli -c -p 7001
127.0.0.1:7001> set a b
-> Redirected to slot [15495] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set c d
-> Redirected to slot [7365] located at 127.0.0.1:7001
OK
其中redis-cli -c标志表示连接的是一个集群,通过set方式可以看出,会将key进行crc16后hash分片到不同的redis实例上,redis-cli会通过跟redis集群进行交互,把key基于hash分片,把请求自动转到不同的实例上
这个表示redis-trib.rb依赖的json_pure在机器中不存在:
- 到官网http://rpmfind.net/linux/RPM/opensuse/13.1/x86_64/rubygem-json_pure-1.8.0-2.1.2.x86_64.html,下载对应的rpm包
- json_pure的rpm包需要依赖其它rpm包,如下,如果安装json_pure失败,需要根据提示,下载依赖的rpm包并进行安装
/usr/bin/ruby
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(PayloadFilesHavePrefix) >= 4.0-1
ruby(abi) >= 2.0.0
rpmlib(PayloadIsLzma) >= 4.4.6-1