參考文檔
搭建集羣:http://www.redis.cn/topics/cluster-tutorial.html
redis-cluster-proxy:https://github.com/RedisLabs/redis-cluster-proxy
搭建集羣
集羣最少爲3個節點,所以需要複製3份redis配置文件,每個文件更改下面幾個地方:
- cluster-enabled:yes
- port:端口號
- pidfile:/var/run/redis-6379.pid
- logfile:"/usr/local/var/log/redis/redis-6379.log"
- dir:/usr/local/var/db/redis/redis-6379
注意上面三個目錄都需要給夠寫權限
$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.
通過 redis-cluster-proxy 啓動代理
更改以下配置:
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"
代碼如下:
<?php
$redis = new Redis();
$redis->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)