如何创建一个predis客户端可以做到这些集群和复制?(How to create a predis

2019-10-31 10:23发布

注意:这是一个跟进这个问题

我需要能够做到既Redis的集群和复制。 这是配置文件我在laravel:


    'redis' => [

        'clustered' => [
            'client' => 'predis',
            'cluster' => true,
            'options' => [ 'cluster' => 'redis',
                           'replication' => true 
                         ],
            'clusters' => [
                        // masters
                        [
                            'host' => env('REDIS_SHARD_1_HOST', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_1_PORT', 6379),
                            'database' => 0,
                            'alias' => 'master',
                        ],
                        [
                            'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_2_PORT', 6379),
                            'database' => 0,
                            'alias' => 'master',
                        ],
                        [
                            'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_3_PORT', 6379),
                            'database' => 0,
                            'alias' => 'master',
                        ],
                        // slaves (replicas)
                        [
                            'host' => env('REDIS_SHARD_1_HOST_REPLICA', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_1_PORT_REPLICA', 6379),
                            'database' => 0,
                        ],
                        [
                            'host' => env('REDIS_SHARD_2_HOST_REPLICA', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_2_PORT_REPLICA', 6379),
                            'database' => 0,
                        ],
                        [
                            'host' => env('REDIS_SHARD_3_HOST_REPLICA', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_3_PORT_REPLICA', 6379),
                            'database' => 0,
                        ]
            ],
        ], 

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port'     => 6379,
            'database' => 0,
            'cluster' => false,
        ],
    ]

];

但在我的调试器,即获取predis创建的连接有3个奴隶和上次主:

[ *Locals ] [ Superglobals ] [ User defined constants ]

- Locals at /Users/Shared/dev/php/toters-api/vendor/predis/predis/src/Client.php:138

 ▾ $connection = (Predis\Connection\Aggregate\MasterSlaveReplication [6])
  \
   ▸ $connection->strategy = (Predis\Replication\ReplicationStrategy [3])
   |
   ▾ $connection->master = (Predis\Connection\StreamConnection [4])
    \
     ⬦ $connection->master->*Predis\Connection\AbstractConnection*resource = (null)
     |
     ⬦ $connection->master->*Predis\Connection\AbstractConnection*cachedId = (null)
     |
     ▸ $connection->master->parameters = (Predis\Connection\Parameters [2])
     |
     ▸ $connection->master->initCommands = (array [1])
    /
   ▾ $connection->slaves = (array [3])
    \
     ▸ $connection->slaves['slave-localhost:7003'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $connection->slaves['slave-localhost:7004'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $connection->slaves['slave-localhost:7005'] = (Predis\Connection\StreamConnection [4])
    /
   ⬦ $connection->current = (null)
   |
   ⬦ $connection->autoDiscovery = (bool) 0
   |
   ⬦ $connection->connectionFactory = (null)
  /
 ⬦ $initializer = (uninitialized)
 |
 ▾ $options = (Predis\Configuration\Options [3])
  \
   ▸ $options->input = (array [1])
   |
   ▸ $options->options = (array [2])
   |
   ▸ $options->handlers = (array [6])
  /
 ▾ $parameters = (array [6])
  \
   ▸ $parameters[0] = (array [5])
   |
   ▸ $parameters[1] = (array [5])
   |
   ▸ $parameters[2] = (array [5])
   |
   ▸ $parameters[3] = (array [4])
   |
   ▸ $parameters[4] = (array [4])
   |
   ▸ $parameters[5] = (array [4])
  /
 ▾ $replication = (Predis\Connection\Aggregate\MasterSlaveReplication [6])
  \
   ▸ $replication->strategy = (Predis\Replication\ReplicationStrategy [3])
   |
   ▾ $replication->master = (Predis\Connection\StreamConnection [4])
    \
     ⬦ $replication->master->*Predis\Connection\AbstractConnection*resource = (null)
     |
     ⬦ $replication->master->*Predis\Connection\AbstractConnection*cachedId = (null)
     |
     ▸ $replication->master->parameters = (Predis\Connection\Parameters [2])
     |
     ▸ $replication->master->initCommands = (array [1])
    /
   ▾ $replication->slaves = (array [3])
    \
     ▸ $replication->slaves['slave-localhost:7003'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $replication->slaves['slave-localhost:7004'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $replication->slaves['slave-localhost:7005'] = (Predis\Connection\StreamConnection [4])
    /
   ⬦ $replication->current = (null)
   |
   ⬦ $replication->autoDiscovery = (bool) 0
   |
   ⬦ $replication->connectionFactory = (null)
  /
 ▾ $this = (Predis\Client [3])
  \
   ⬦ $this->connection = (null)
   |
   ▸ $this->options = (Predis\Configuration\Options [3])
   |
   ⬦ $this->profile = (null)
  /

我怎样才能写入配置成具有在集群三个节点,具有一个复制品的每个节点?

文章来源: How to create a predis client that does both clustering and replication?