w1w 发表于 2018-11-2 10:13:27

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
  

  # cat /data/www/facx195.com/predis.php
  


  

  name1,name2,name3是3个key,按照算法分配到3个slot上,有可能分到3台服务器上
  首先运行predis.php查看结果:
  如图:

  然后登录到redis客户端进行集群验证:
  # redis-cli -h 192.168.2.106 -p 20380 -c
  

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

  
# redis-cli -h 192.168.2.107 -p 31680 -c
  
192.168.2.107:31680> get name4
  
-> Redirected to slot located at 192.168.2.107:20380
  
"44"
  
192.168.2.107:20380> get name6
  
-> Redirected to slot located at 192.168.2.107:31680
  
"66"
  
192.168.2.107:31680> get name5
  
-> Redirected to slot located at 192.168.2.106:20380
  
"55"
  
192.168.2.106:20380>
  

  登录99机器上的redis,报错,原因是99机器的上redis一直是关闭的,但是这样并不会在重启99机器上的redis实例后登录99机器上的redis查不到数据的
  

# 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
  

# redis-server 20380.conf  
# redis-server 31680.conf
  

  
# redis-cli -h 192.168.2.99 -p 31680 -c
  
192.168.2.99:31680> get name5
  
-> Redirected to slot located at 192.168.2.106:20380
  
"55"
  
192.168.2.106:20380> get name6
  
-> Redirected to slot located at 192.168.2.107:31680
  
"66"
  
192.168.2.107:31680> get name4
  
-> Redirected to slot located at 192.168.2.107:20380
  
"44"
  
192.168.2.107:20380>
  

  参考资料:
  https://blog.csdn.net/nuli888/article/details/52136918


页: [1]
查看完整版本: php操作redis cluster集群