php操作redis cluster集群
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php操作redis cluster集群相关的知识,希望对你有一定的参考价值。
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
[[email protected] 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 PredisClient($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客户端进行集群验证:
[[email protected] ~]# 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>
[[email protected] ~]# 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查不到数据的
[[email protected] ~]# 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
[[email protected] conf]# redis-server 20380.conf
[[email protected] conf]# redis-server 31680.conf
[[email protected] ~]# 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集群的主要内容,如果未能解决你的问题,请参考以下文章
java操作redis集群问题CLUSTERDOWN The cluster is down. Use CLUSTER INFO for more information