【发布时间】:2018-10-19 02:34:21
【问题描述】:
如何配置 Redis 哨兵? Redis 可以使用 laravel 配置轻松独立配置,但是使用 sentinel 时如何配置没有记录在任何地方?
redit: 上有一个类似的问题,但没有帮助。
【问题讨论】:
标签: caching laravel-5.5 configure redis-sentinel
如何配置 Redis 哨兵? Redis 可以使用 laravel 配置轻松独立配置,但是使用 sentinel 时如何配置没有记录在任何地方?
redit: 上有一个类似的问题,但没有帮助。
【问题讨论】:
标签: caching laravel-5.5 configure redis-sentinel
在 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');
【讨论】: