ServiceStack.Redis.Sentinel的用法 [英] ServiceStack.Redis.Sentinel Usage

查看:528
本文介绍了ServiceStack.Redis.Sentinel的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行ServiceStack的许可版本,并尝试在Google Cloud Compute上获取哨兵群集设置.

I'm running a licensed version of ServiceStack and trying to get a sentinel cluster setup on Google Cloud Compute.

该群集基本上是GCE的单击部署Redis解决方案-3台服务器.这是我用来初始化的代码...

The cluster is basically GCE's click-to-deploy redis solution - 3 servers. Here is the code i'm using to initialize...

var hosts = Settings.Redis.Host.Split(';');
var sentinel = new ServiceStack.Redis.RedisSentinel(hosts, "master");
redis = sentinel.Setup();

container.Register<IRedisClientsManager>(redis);
container.Register<ICacheClient>(redis.GetCacheClient());

客户端工作正常-但一旦我关闭其中一个Redis实例,一切都会摇摇欲坠.客户端抱怨无法连接到丢失的实例.另外,即使我备份了实例-它处于只读模式,所以一切仍然失败.一旦处于这种状态,似乎就无法恢复...

The client works fine - but once i shut down one of the redis instances everything craps the bed. The client complains about not being able to connect to the missing instance. Additionally, even when i bring the instance back up - it is in READ ONLY mode, so everything still fails. There doesn't seem to be a way to recover once you are in this state...

我做错什么了吗? RedisSentinal客户端不知道新主服务器是谁的某些原因吗?我将所有3个主机IP地址都喂了...

Am i doing something wrong? Is there some reason that RedisSentinal client doesn't figure out who the new master is? I feed it all 3 host IP addresses...

推荐答案

您应该只将Redis Sentinel服务器的主机提供给RedisSentinel,因为它会从Sentinel主机获取其他主/从Redis服务器的活动列表.

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.

最近在最新的 v4.0.37 中添加了对RedisSentinel的某些更改,该更改现在为

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);

启动RedisSentinel将连接到Sentinel主机并返回具有活动

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();

然后您可以使用以下方法在IOC中进行注册:

Which you can then register in the IOC with:

container.Register<IRedisClientsManager>(redisManager);

然后,RedisSentinel应该侦听Sentinel主机上的主/从更改,并相应地对 redisManager 进行故障转移.然后,处置池中的现有连接,并用新池替换新配置的主机.如果再次使用池外的任何活动连接,它们都会抛出连接异常,下次从池中检索RedisClient时,它将使用新主机进行配置.

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.

下面是一个示例,说明如何使用新的回调来内省RedisServer事件:

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);
    },                
};

也可以通过配置登录ServiceStack 来启用这些事件的日志记录:

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

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

还有一个附加的显式FailoverToSentinelHosts(),可用于强制RedisSentinel重新查找并故障转移到最新的主/从主机,例如:

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();

新的主机在返回的sentinelInfo中可用:

The new hosts are available in the returned sentinelInfo:

"Failed over to read/write: {0}, read-only: {1}".Print(
    sentinelInfo.RedisMasters, sentinelInfo.RedisSlaves);

这篇关于ServiceStack.Redis.Sentinel的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆