1. 程式人生 > >php操作redis cluster集群

php操作redis cluster集群

技術分享 數據 ron req github master 分配 key locate

php要操作redis cluster集群有兩種方式:

1、使用phpredis擴展,這是個c擴展,性能更高,但是phpredis2.x擴展不行,需升級phpredis到3.0,但這個方案參考資料很少

2、使用predis,純php開發,使用了命名空間,需要php5.3+,靈活性高

我用的是predis,下載地址https://github.com/nrk/predis/zipball/master
下載後的軟件包為:
nrk-predis-v1.1.0-65-gd72f067.zip
上傳到服務器上,解壓後:
unzip nrk-predis-v1.1.0-65-gd72f067.zip
下載好後重命名為predis,

mv nrk-predis-d72f067 predis
mv predis /data/www/facx195.com

註意,我的環境是php,所以在lnmp環境中也沒必要裝php的phpredis擴展也是可以在瀏覽器請求php文件來獲取redis中的數據的

redis的實例環境為:

192.168.2.106:20380
192.168.2.106:31680
192.168.2.107:20380
192.168.2.107:31680
192.168.2.99:20380
192.168.2.99:31680

[root@MQ2-S1 wwwlogs]# cat /data/www/facx195.com/predis.php

<?php
require ‘/data/www/fx195.com/predis/autoload.php‘;//引入predis相關包
//redis實例
$servers = array(
    ‘tcp://192.168.2.99:20380‘,
    ‘tcp://192.168.2.106:31680‘,
    ‘tcp://192.168.2.107:20380‘,
    ‘tcp://192.168.2.99:31680‘,
    ‘tcp://192.168.2.106:20380‘,
    ‘tcp://192.168.2.107:31680‘,
);

$client = new Predis\Client($servers, array(‘cluster‘ => ‘redis‘));

$client->set("name4", "44");
$client->set("name5", "55");
$client->set("name6", "66");

$name1 = $client->get(‘name4‘);
$name2 = $client->get(‘name5‘);
$name3 = $client->get(‘name6‘);
var_dump($name1, $name2, $name3);die;
?>

name1,name2,name3是3個key,按照算法分配到3個slot上,有可能分到3臺服務器上

首先運行predis.php查看結果:

如圖:
技術分享圖片

然後登錄到redis客戶端進行集群驗證:

[root@MQ-M ~]# redis-cli -h 192.168.2.106 -p 20380 -c

192.168.2.106:20380> get name4
-> Redirected to slot [8736] located at 192.168.2.107:20380
"44"
192.168.2.107:20380> get name5
-> Redirected to slot [12801] located at 192.168.2.106:20380
"55"
192.168.2.106:20380> get name6
-> Redirected to slot [610] located at 192.168.2.107:31680
"66"
192.168.2.107:31680> 

[root@MQ-M ~]# redis-cli -h 192.168.2.107 -p 31680 -c
192.168.2.107:31680> get name4
-> Redirected to slot [8736] located at 192.168.2.107:20380
"44"
192.168.2.107:20380> get name6
-> Redirected to slot [610] located at 192.168.2.107:31680
"66"
192.168.2.107:31680> get name5
-> Redirected to slot [12801] located at 192.168.2.106:20380
"55"
192.168.2.106:20380> 

登錄99機器上的redis,報錯,原因是99機器的上redis一直是關閉的,但是這樣並不會在重啟99機器上的redis實例後登錄99機器上的redis查不到數據的

[root@MQ-M ~]# redis-cli -h 192.168.2.99 -p 31680 -c
Could not connect to Redis at 192.168.2.99:31680: Connection refused
Could not connect to Redis at 192.168.2.99:31680: Connection refused
not connected> get name6
Could not connect to Redis at 192.168.2.99:31680: Connection refused
not connected> 

啟動192.168.2.99上的2臺redis

[root@MQ2-S1 conf]# redis-server 20380.conf 
[root@MQ2-S1 conf]# redis-server 31680.conf 

[root@MQ-M ~]# redis-cli -h 192.168.2.99 -p 31680 -c
192.168.2.99:31680> get name5
-> Redirected to slot [12801] located at 192.168.2.106:20380
"55"
192.168.2.106:20380> get name6
-> Redirected to slot [610] located at 192.168.2.107:31680
"66"
192.168.2.107:31680> get name4
-> Redirected to slot [8736] located at 192.168.2.107:20380
"44"
192.168.2.107:20380> 

參考資料:
https://blog.csdn.net/nuli888/article/details/52136918

php操作redis cluster集群