Redis集群~StackExchange.redis链接Sentinel服务器并订阅相关事件(原创)

回到目录html

对于redis-sentinel我在以前的文章中已经说过,它是一个仲裁者,当主master挂了后,它将在全部slave服务器中进行选举,选举的原则固然能够看它的官方文章,这与咱们使用者没有什么关系,而对于sentinel来讲,它在进行主从切换时,会触发相关事件,这是和咱们开发人员有关系的,如当+switch-master事件被触发时,说明当前Sentinal已经完成了一次主从的切换,并全部服务已经正常运转了。linux

下面是我这几天做的测试,对于Twemproxy代理和Sentinal哨兵都已经成功使用stackExchange.redis进行了链接,并正常访问了,固然Sentinel只公开了几个redis命令,这个你们要清梦,不明白的能够看个人这篇文章《Redis集群~windows下搭建Sentinel环境及它对主从模式的实际意义》。redis

链接普通的redis服务器

  ConnectionMultiplexer conn = ConnectionMultiplexer.Connect("127.0.0.1:6379");
  ConfigurationOptions option = new ConfigurationOptions();
  option.EndPoints.Add("127.0.0.1", 6379);
  ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(option);

链接TW代理服务器

    ConfigurationOptions twOption = new ConfigurationOptions();
    twOption.EndPoints.Add("127.0.0.1", 22122);
    twOption.EndPoints.Add("127.0.0.1", 22123);
    twOption.Proxy = Proxy.Twemproxy;//代理的类型
    ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(twOption);

链接Sentinal仲裁哨兵服务器

           //链接sentinel服务器
            ConfigurationOptions sentinelConfig = new ConfigurationOptions();
            sentinelConfig.ServiceName = "master1";
            sentinelConfig.EndPoints.Add("192.168.2.3", 26379);
            sentinelConfig.EndPoints.Add("192.168.2.3", 26380);
            sentinelConfig.TieBreaker = "";//这行在sentinel模式必须加上
            sentinelConfig.CommandMap = CommandMap.Sentinel;
            // Need Version 3.0 for the INFO command?
            sentinelConfig.DefaultVersion = new Version(3, 0);
            ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(sentinelConfig);

有了上面的代码后,咱们能够成功的链接一个sentinel服务器,对这个链接的实际意义在于:当一个主从进行切换后,若是它外层有Twemproxy代理,咱们能够在这个时机(+switch-master事件)通知你的Twemproxy代理服务器,并更新它的配置文件里的master服务器的地址,而后从起你的Twemproxy服务,这样你的主从切换才算真正完成。windows

对于Twemproxy-sentinal-master/slave主从切换实现图

咱们能够使用.netcore开发一个跨平台的程序,将它放在linux的tw代理服务器上,使用dotnet run去运行它,而后当收到由sentinel发来的+switch-master事件时,将更新tw配置文件并从起它的服务。服务器

但愿你们对技术多一点深度的研究,找不到答案就看看它的源代码,可能会有意外的收获!post

欢迎你们关注大叔博客!测试

回到目录spa

相关文章
相关标签/搜索