ServiceStack.Redis.Sentinel Usage

放肆的年华 提交于 2019-12-03 09:10:36

You should only be supplying the host of the Redis Sentinel Server to RedisSentinel as it gets the active list of other master/slave redis servers from the Sentinel host.

Some changes to RedisSentinel were recently added in the latest v4.0.37 that's now available on MyGet which includes extra logging and callbacks of Redis Sentinel events. The new v4.0.37 API looks like:

var sentinel = new RedisSentinel(sentinelHost, masterName);

Starting the RedisSentinel will connect to the Sentinel Host and return a pre-configured RedisClientManager (i.e. redis connection pool) with the active

var redisManager = sentinel.Start();

Which you can then register in the IOC with:

container.Register<IRedisClientsManager>(redisManager);

The RedisSentinel should then listen to master/slave changes from the Sentinel hosts and failover the redisManager accordingly. The existing connections in the pool are then disposed and replaced with a new pool for the newly configured hosts. Any active connections outside of the pool they'll throw connection exceptions if used again, the next time the RedisClient is retrieved from the pool it will be configured with the new hosts.

Callbacks and Logging

Here's an example of how you can use the new callbacks to introspect the RedisServer events:

var sentinel = new RedisSentinel(sentinelHost, masterName)
{
    OnFailover = manager => 
    {
        "Redis Managers were Failed Over to new hosts".Print();
    },
    OnWorkerError = ex =>
    {
        "Worker error: {0}".Print(ex);
    },
    OnSentinelMessageReceived = (channel, msg) =>
    {
        "Received '{0}' on channel '{1}' from Sentinel".Print(channel, msg);
    },                
};

Logging of these events can also be enabled by configuring Logging in ServiceStack:

LogManager.LogFactory = new ConsoleLogFactory(debugEnabled:false);

There's also an additional explicit FailoverToSentinelHosts() that can be used to force RedisSentinel to re-lookup and failover to the latest master/slave hosts, e.g:

var sentinelInfo = sentinel.FailoverToSentinelHosts();

The new hosts are available in the returned sentinelInfo:

"Failed over to read/write: {0}, read-only: {1}".Print(
    sentinelInfo.RedisMasters, sentinelInfo.RedisSlaves);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!