我正在尝试将Laravel应用程序配置为接受多个Redis节点,但出现异常:
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"
生成异常的代码:
$user = Redis::get('anything');
.env
REDIS_CLUSTER=redis
REDIS_CLIENT=predis
CACHE_DRIVER=redis
QUEUE_DRIVER=redis
BROADCAST_DRIVER=redis
配置: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),
]
],
[...]
我已经搜索了,但是什么也没找到...
Docs在这方面有点模糊:
如果您的应用程序正在使用Redis服务器集群,则您 应该在Redis的clusters关键字中定义这些集群 配置:
'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, ], ], ],
],
答案 0 :(得分:1)
该软件包的原始作者已弃用Predis,在将来的发行版中可能会将其从Laravel中删除。
所以,我建议安装php-ext phpredis
.env:
REDIS_CLIENT=phpredis
答案 1 :(得分:0)
文件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,
文件config / cache.php
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
文件.env
REDIS_CONNECTION=127.0.0.1:6379, //all ips redis clusters