Laravel 5的redis哨兵(sentinel)支持

Laravel支持redis sentinel需要依赖几个第三方扩展,但是因为扩展的作者没有来得及更新(开源通病),所有在laravel 5以后这几个第三方扩展大多有兼容性问题,几经周折终于支持laravle 5。在此贡献出来已免后来者走弯路:

修改composer.json:

...

"require": { 
 "geniuslinchao/laravel-PSRedis": ">=1.2.10",
"geniuslinchao/psredis": ">=1.2.1"
    },
    "repositories": [
        {
                "type": "vcs",
                "url": "https://github.com/geniuslinchao/laravel-PSRedis"
        },
        {
                "type": "vcs",
                "url": "https://github.com/geniuslinchao/PSRedis"
        }
     ],

...

修改config/app.php:

'providers' => [
...
        // 'IlluminateRedisRedisServiceProvider', # comment this out
        IndatusLaravelPSRedisLaravelPSRedisServiceProvider::class,
    ]
配置config/database.php:

'redis' => [

        /** the name of the redis node set */
        'nodeSetName' => 'sentinel-name',

        'cluster' => false,

        /** Array of sentinels */
        'masters' => [
            [
                'host' => '192.168.1.1',
                'port' => '26379',
            ],
            [
                'host' => '192.168.1.2',
                'port' => '26379',
            ]
        ],

        /** how long to wait and try again if we fail to connect to master */
        'backoff-strategy' => [
            'max-attempts' => 10, // the maximum-number of attempt possible to find master
            'wait-time' => 500,   // miliseconds to wait for the next attempt
            'increment' => 1.5, // multiplier used to increment the back off time on each try
        ]
    ];
然后执行 composer update即可


请参考https://github.com/geniuslinchao/laravel-PSRedis,如有问题欢迎联系我。