时间:2021-07-01 10:21:17 帮助过:20人阅读
java操作redis cluster集群可使用jredis
php要操作redis cluster集群有两种方式:
1、使用phpredis扩展,这是个c扩展,性能更高,但是phpredis2.x扩展不行,需升级phpredis到3.0,但这个方案参考资料很少
2、使用predis,纯php开发,使用了命名空间,需要php5.3+,灵活性高
我用的是predis,下载地址https://github.com/nrk/predis...
下载好后重命名为predis,
server1:192.168.1.198
server2:192.168.1.199
predis.php
<?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客户端进行集群验证:
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实例,再看效果
以上就是php操作redis cluster集群成功的实例讲解的详细内容,更多请关注Gxl网其它相关文章!