回到目录
对于redis-sentinel我在之前的文章中已经说过,它是一个仲裁者,当主master挂了后,它将在所有slave服务器中进行选举,选举的原则当然可以看它的官方文章,这与我们使用者没有什么关系,而对于sentinel来说,它在进行主从切换时,会触相关事件,这是和我们开发人员有关系的,如当+switch-master事件被触发时,说明当前sentinal已经完成了一次主从的切换,并所有服务已经正常运转了。
下面是我这几天作的测试,对于TW代理和Sentinal哨兵都已经成功使用stackExchange.redis进行了连接,并正常访问了,当然sentinel只公开了几个redis命令,这个大家要清梦,不明白的可以看我的这篇文章《 Redis集群~windows下搭建Sentinel环境及它对主从模式的实际意义 》。
连接普通的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服务器,对这个连接的实际意义在于:当一个主从进行切换后,如果它外层有TW代理,我们可以在这个时机(+switch-master事件)通知你的tw代理服务器,并更新tw配置的master服务器的地址,然后从起你的tw服务,这样你的主从切换才算真正完成。
对于tw-sentinal-master/slave主从切换实现图我们可以使用.netcore开发一个跨平台的程序,将它放在linux的tw代理服务器上,使用dotnet run去运行它,然后当收到由sentinel发来的 +switch-master 事件时,将更新tw配置文件并从起它的服务。

希望大家对技术多一点深度的研究,找不到答案就看看它的源代码,可能会有意外的收获!
欢迎大家关注大叔博客!
回到目录