java操做redis cluster集羣可以使用jredisphp
php要操做redis cluster集羣有兩種方式:java
一、使用phpredis擴展,這是個c擴展,性能更高,可是phpredis2.x擴展不行,需升級phpredis到3.0,但這個方案參考資料不多git
二、使用predis,純php開發,使用了命名空間,須要php5.3+,靈活性高github
我用的是predis,下載地址https://github.com/nrk/predis...redis
下載好後重命名爲predis,算法
server1:192.168.1.198
server2:192.168.1.199服務器
predis.phptcp
<?php require 'predis/autoload.php';//引入predis相關包 //redis實例 $servers = array( 'tcp://192.168.1.198:7000', 'tcp://192.168.1.198:7001', 'tcp://192.168.1.198:7002', 'tcp://192.168.1.199:7003', 'tcp://192.168.1.199:7004', 'tcp://192.168.1.199:7005', ); $client = new Predis\Client($servers, array('cluster' => 'redis')); $client->set("name1", "11"); $client->set("name2", "22"); $client->set("name3", "33"); $name1 = $client->get('name1'); $name2 = $client->get('name2'); $name3 = $client->get('name3'); var_dump($name1, $name2, $name3);die;
name1,name2,name3是3個key,按照算法分配到3個slot上,有可能分到3臺服務器上
首先運行predis.php查看結果:性能
而後登陸到redis客戶端進行集羣驗證:ui
server1
[root@localhost src]# redis-cli -c -p 7000 127.0.0.1:7000> get name1 -> Redirected to slot [12933] located at 192.168.1.199:7004 "11" 192.168.1.199:7004> get name2 -> Redirected to slot [742] located at 192.168.1.199:7003 "22" 192.168.1.199:7003> get name3 "33" 192.168.1.199:7003>
server2
[root@localhost src]# redis-cli -c -p 7003 127.0.0.1:7003> get name1 -> Redirected to slot [12933] located at 192.168.1.199:7004 "11" 192.168.1.199:7004> get name2 -> Redirected to slot [742] located at 192.168.1.199:7003 "22" 192.168.1.199:7003> get name3 "33" 192.168.1.199:7003>
能夠看到數據分佈在各個服務器上,能夠根據ps -ef | grep redis,殺掉其中幾個redis實例,再看效果