Skip to content

Need an example of how to create a predis client with clustering AND replication #579

@abbood

Description

@abbood

The docs provides examples with either or, but not both

Basically I'm creating a predis client with the following parameters:


$parameters = [
   // masters
    'localhost:7000?alias=master', 
    'localhost:7001?alias=master', 
    'localhost:7002?alias=master', 

    // slaves
    'localhost:7003', 
    'localhost:7004', 
    'localhost:7005', 
]

$options    = ['cluster' => 'redis', 'replication' => true];
           
$client = new Predis\Client($parameters, $options);

But when it creates the connection in predis/createConnection, i get this in the debugger:

- 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 = (null)
   |
   ▾ $connection->slaves = (array [6])
    \
     ▸ $connection->slaves['slave-localhost?alias=master:7000'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $connection->slaves['slave-localhost?alias=master:7001'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $connection->slaves['slave-localhost?alias=master:7002'] = (Predis\Connection\StreamConnection [4])
     |
     ▸ $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 [4])
   |
   ▸ $parameters[1] = (array [4])
   |
   ▸ $parameters[2] = (array [4])
   |
   ▸ $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 = (null)
   |
   ▸ $replication->slaves = (array [6])
   |
   ⬦ $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)
  /

basically it's parsing all my hosts as slaves, instead of acknowledging the ?alias=master part

ideas?

note: I'm actually feeding these params from a modified version of laravel redis config like so:

    '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,
                        ],
                        [
                            'host' => env('REDIS_SHARD_2_HOST', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_2_PORT', 6379),
                            'database' => 0,
                        ],
                        [
                            'host' => env('REDIS_SHARD_3_HOST', '127.0.01'),
                            'password' => env('REDIS_PASSWORD', null),
                            'port' => env('REDIS_SHARD_3_PORT', 6379),
                            'database' => 0,
                        ],
                        // 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,
        ],
    ]

with my env file:


REDIS_SHARD_1_HOST=localhost?alias=master
REDIS_SHARD_2_HOST=localhost?alias=master
REDIS_SHARD_3_HOST=localhost?alias=master

REDIS_SHARD_1_HOST_REPLICA=localhost
REDIS_SHARD_2_HOST_REPLICA=localhost
REDIS_SHARD_3_HOST_REPLICA=localhost

REDIS_SHARD_1_PORT=7000
REDIS_SHARD_2_PORT=7001
REDIS_SHARD_3_PORT=7002

REDIS_SHARD_1_PORT_REPLICA=7003
REDIS_SHARD_2_PORT_REPLICA=7004
REDIS_SHARD_3_PORT_REPLICA=7005

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions