连接到非本地的 redis 集群时出现 phpredis 的 RedisClusterException

RedisClusterException with phpredis when connecting to redis cluster which is not local

我正在使用 phpredis(基于 https://github.com/phpredis/phpredis 25.02.2016 构建)和 php 5.5.9。使用单个 redis 实例(版本 3.0.7)(远程和本地 redis 实例)成功测试扩展。

这是连接到已配置的 redis 集群的代码(没有哨兵,仅通过配置)。

$cluster = new \RedisCluster(NULL, 
         array("192.168.127.203:7000", "192.168.127.203:7001", "192.168.127.203:7002"));
$cluster->setOption(RedisCluster::OPT_SLAVE_FAILOVER, RedisCluster::FAILOVER_ERROR);
var_dump($cluster->_masters());
var_dump($cluster->get('foo1'));

当我们在与redis 实例相同的服务器上执行此代码时,我们成功获得了所有master 和foo1 的值。但是当我们在另一个webserver上执行代码时,我们从集群中得到了以下masters:

array (size=3)
  0 => 
    array (size=2)
      0 => string '127.0.0.1' (length=9)
      1 => int 7005
  1 => 
    array (size=2)
      0 => string '127.0.0.1' (length=9)
      1 => int 7000
  2 => 
    array (size=2)
      0 => string '127.0.0.1' (length=9)
      1 => int 7001

并且获取该值将失败,并出现带有消息 "Can't communicate with any node in the cluster".

的 RedisClusterException

我不确定这是库中的错误还是我以错误的方式使用了库。代码与文档中的代码相同。我认为一个问题是我们使用本地主机 ip 而不是远程 ip 获得主人。

感谢您的帮助。

终于找到问题所在了。 客户端库中没有任何问题,只是集群配置错误。

为了创建,使用了文档中的以下片段:

./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

当然正确的命令是:

./redis-trib.rb create --replicas 1 192.168.127.203:7000 192.168.127.203:7001 \
192.168.127.203:7002 192.168.127.203:7003 192.168.127.203:7004 192.168.127.203:7005

如果有人遇到类似问题,仅供参考。