搭建集群:http://www.redis.cn/topics/cluster-tutorial.html
redis-cluster-proxy:https://github.com/RedisLabs/redis-cluster-proxy
集群最少为3个节点,所以需要复制3份redis配置文件,每个文件更改下面几个地方:
注意上面三个目录都需要给够写权限
$redis-server ./redis_6379.conf
$redis-server ./redis_6378.conf
$redis-server ./redis_6377.conf
$ps -ef | grep redis-server
501 40954 1 0 3:53下午 ?? 0:47.00 redis-server 127.0.0.1:6379 [cluster]
501 40956 1 0 3:53下午 ?? 0:45.68 redis-server 127.0.0.1:6378 [cluster]
501 41001 1 0 3:57下午 ?? 0:45.08 redis-server 127.0.0.1:6377 [cluster]
创建集群
redis-cli --cluster create 127.0.0.1:6377 127.0.0.1:6378 127.0.0.1:6379
如果遇到错误:CLUSTERDOWN Hash slot not served,则需要修复错误中指定的实例的集群slot
$redis-cli --cluster fix 127.0.0.1:6379
如果遇到错误:[ERR] Node is not empty. Either the node already knows other nodes ,则需要清空对应节点的数据,并将持久化文件删除
redis-cli -p 6379 flushdb
成功
$redis-cli --cluster create 127.0.0.1:6377 127.0.0.1:6378 127.0.0.1:6379
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 60e52412e798c15a8596490b24e81bc67e1e8b98 127.0.0.1:6377
slots:[0-5460] (5461 slots) master
M: 0b9545dfaf4eb476755699f287e7187daa5eae05 127.0.0.1:6378
slots:[5461-10922] (5462 slots) master
M: 781384b4ac76cad9f8a2b7a6c82afb0afdfa2a54 127.0.0.1:6379
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:6377)
M: 60e52412e798c15a8596490b24e81bc67e1e8b98 127.0.0.1:6377
slots:[0-5460] (5461 slots) master
M: 781384b4ac76cad9f8a2b7a6c82afb0afdfa2a54 127.0.0.1:6379
slots:[10923-16383] (5461 slots) master
M: 0b9545dfaf4eb476755699f287e7187daa5eae05 127.0.0.1:6378
slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
更改以下配置:
entry-point 127.0.0.1:6377
entry-point 127.0.0.1:6378
entry-point 127.0.0.1:6379
port 7777
bind 0.0.0.0
threads 8
daemonize yes
enable-cross-slot yes #开启跨node查询
$redis-cluster-proxy -c ./proxy.conf
$ps -ef | grep redis
501 43215 1 0 8:57下午 ?? 0:02.90 redis-server 127.0.0.1:6377 [cluster]
501 43217 1 0 8:57下午 ?? 0:02.89 redis-server 127.0.0.1:6379 [cluster]
501 43219 1 0 8:57下午 ?? 0:02.89 redis-server 127.0.0.1:6378 [cluster]
501 43383 1 0 9:07下午 ?? 0:00.03 redis-cluster-proxy -c ./proxy.conf
此时可以支持普通集群不支持的mget等命令获取不同节点上的数据,proxy将汇总多个命令的结果。
先确认以下数据分布在不同节点上:
$redis-cli -p 6379
127.0.0.1:6379> keys *
1) "why"
$redis-cli -p 6378
127.0.0.1:6378> keys *
(empty list or set)
$redis-cli -p 6377
127.0.0.1:6377> keys *
1) "jzm"
代码如下:
connect('127.0.0.1', 7777, 10);
$redis->set('why', 'why');
$redis->set('jzm', 'jzm');
$res = $redis->mget(['why', 'jzm']);
var_dump($res);die;
结果:
$php redis-cluster-proxy.php
array(2) {
[0]=>
string(3) "why"
[1]=>
string(3) "jzm"
}
如果 enable-cross-slot 设置为默认的no,则返回false
$php redis-cluster-proxy.php
bool(false)