码迷,mamicode.com
首页 > Web开发 > 详细

php操作redis cluster集群

时间:2018-07-08 23:17:30      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:查看   png   redis扩展   实例   lnmp   col   重启   分配   客户   

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集群

标签:查看   png   redis扩展   实例   lnmp   col   重启   分配   客户   

原文地址:http://blog.51cto.com/wujianwei/2138908

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!