为什么使用Laravelredis集群会出现异常?

Why do I get an exception when using Laravel redis cluster?

我正在尝试配置我的 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这边有点含糊:

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,
        ],
    ],
],

],

文件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

Predis 已被包的原始作者放弃,可能会在将来的版本中从 Laravel 中删除。

所以,我推荐安装php-ext phpredis

.env:

REDIS_CLIENT=phpredis