php操作redis cluster集群成功的实例讲解
来源:不言
发布时间:2019-01-10 10:56:19
阅读量:858
本篇文章给大家带来的内容是关于php操作redis cluster集群成功的实例讲解,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php
require 'predis/autoload.php' ;
$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
1 2 3 4 5 6 7 8 9 10 | [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
1 2 3 4 5 6 7 8 9 10 | [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实例,再看效果