Reputation: 9432
I am trying to configure my Laravel apllication to accept multiple redis nodes but I get an exception:
exception: "Predis\Response\ServerException"
file: "/home/vagrant/code/gryphon-laravel/vendor/predis/predis/src/Client.php"
line: 370
message: "MOVED 9576 10.225.25.33:6380"
Code that generates the Exception:
$user = Redis::get('anything');
.env
REDIS_CLUSTER=redis
REDIS_CLIENT=predis
CACHE_DRIVER=redis
QUEUE_DRIVER=redis
BROADCAST_DRIVER=redis
Config: database.php
[...]
'redis' => [
'cluster' => true,
'client' => env('REDIS_CLIENT', 'predis'),
'clusters' => [
'first' => [
[
'host' => env('REDIS_HOST', 'localhost'),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
]
],
'second'=> [
[
'host' => env('REDIS_HOST_2', 'localhost'),
'port' => env('REDIS_PORT_2', 6379),
'database' => 0,
]
],
'third'=> [
[
'host' => env('REDIS_HOST_3', 'localhost'),
'port' => env('REDIS_PORT_3', 6379),
'database' => 0,
],
]
],
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
'parameters' => [
'password' => env('REDIS_PASSWORD', null),
],
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
]
],
[...]
I have searched and searched but have found nothing...
Docs on this side is a bit vague :
If your application is utilizing a cluster of Redis servers, you should define these clusters within a clusters key of your Redis configuration:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis'), 'clusters' => [ 'default' => [ [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], ], ],
],
Upvotes: 2
Views: 2013
Reputation: 504
Predis has been abandoned by the package's original author and may be removed from Laravel in a future release.
So, I recommend to install php-ext phpredis
.env:
REDIS_CLIENT=phpredis
Upvotes: 1
Reputation: 546
File config/database.php
$redisConnStr = env('REDIS_CONNECTION', '127.0.0.1:6379');
$redis = [
'client' => 'predis',
];
$redisConnections = explode(',', $redisConnStr);
if (count($redisConnections) === 1) {
$params = explode(':', $redisConnections[0]);
$redis = array_merge(
$redis,
[
'cluster' => env('REDIS_CLUSTER', false),
'default' => [
'host' => $params[0] ?? '127.0.0.1',
'password' => env('REDIS_PASSWORD', null),
'port' => (int)($params[1] ?? 6379),
'database' => env('REDIS_DATABASE', 0),
]
]
);
}
else {
$redisCacheCluster = [];
foreach ($redisConnections as $conn){
$params = explode(':', $conn);
$redisCacheCluster[] = [
'host' => $params[0] ?? '127.0.0.1',
'password' => env('REDIS_PASSWORD', null),
'port' => (int)($params[1] ?? 6379),
];
}
$redis = array_merge(
$redis,
[
'options' => ['cluster' => 'redis'],
'clusters' => [
'default' => $redisCacheCluster,
]
]
);
}
'redis' => $redis,
File config/cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
File .env
REDIS_CONNECTION=127.0.0.1:6379, //all ips redis clusters
Upvotes: 1