回到目錄html
對於redis-sentinel我在以前的文章中已經說過,它是一個仲裁者,當主master掛了後,它將在全部slave服務器中進行選舉,選舉的原則固然能夠看它的官方文章,這與咱們使用者沒有什麼關係,而對於sentinel來講,它在進行主從切換時,會觸發相關事件,這是和咱們開發人員有關係的,如當+switch-master事件被觸發時,說明當前Sentinal已經完成了一次主從的切換,並全部服務已經正常運轉了。linux
下面是我這幾天做的測試,對於Twemproxy代理和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);
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);
//鏈接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
咱們能夠使用.netcore開發一個跨平臺的程序,將它放在linux的tw代理服務器上,使用dotnet run去運行它,而後當收到由sentinel發來的+switch-master事件時,將更新tw配置文件並從起它的服務。服務器
但願你們對技術多一點深度的研究,找不到答案就看看它的源代碼,可能會有意外的收穫!post
歡迎你們關注大叔博客!測試
回到目錄spa