在 Laravel 5.5 中配置 Redis Sentinel

Configure Redis Sentinel in Laravel 5.5

如何配置Redis哨兵? Redis 可以使用 laravel 配置轻松地独立配置,但是当使用哨兵时,如何配置在任何地方都没有记录?

redit: 上有人提出了一个类似的问题,但没有帮助。

Laravel 5.5中可以这样做:

参考:https://github.com/laravel/framework/pull/18850#issue-116339448

database.php:

'redis' => [

        'client' => 'predis',

        // Keep Default as is you want to use both redis and sentinel for different service(cache, queue)'
        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

        // Create a custom connection to use redis sentinel
        'cache_sentinel' => [
            // Set the Sentinel Host from Environment (optinal you can hardcode if want to use in prod only)
            env('CACHE_REDIS_SENTINEL_1'),
            env('CACHE_REDIS_SENTINEL_2'),
            env('CACHE_REDIS_SENTINEL_3'),
            'options' => [
                'replication' => 'sentinel',
                'service' => 'cachemaster'),
                'parameters' => [
                    'password' => env('REDIS_PASSWORD', null),
                    'database' => 0,
                ],
            ],
        ],
    ],

```

在您要使用的服务中指定 redis 连接。例如,如果缓存需要 redis sentinal 可以创建新的缓存连接以使用上面的 sentinal 连接,如下所示:

'stores' = [

    //Keep default too as is
    'redis' => [
        'driver'     => 'redis',
        'connection' => 'default',
    ],

    // create own cache connection
    'sentinel_redis' => [
        'driver'     => 'redis',
        'connection' => 'cache_sentinel',
    ],

并且在 Laravel 应用程序中,您可以通过 Cache Facade 轻松使用:

Cache::store('sentinel_redis')->get('key');