我正在使用 StackExchange 库连接到 redis 服务器。这只是一个简单的代码,它只显示如何订阅事件而不是将其作为最终解决方案。每当哨兵选择新服务器时,您都会收到一个事件,因此您可以选择新服务器。
ConnectionMultiplexer multiplexer =
ConnectionMultiplexer.Connect(new ConfigurationOptions
{
CommandMap = CommandMap.Sentinel,
EndPoints = { { "127.0.0.1", 26379 }, { "127.0.0.1", 26380 } },
AllowAdmin = true,
TieBreaker = "",
ServiceName = "mymaster",
SyncTimeout = 5000
});
multiplexer.GetSubscriber().Subscribe("*", (c, m) =>
{
Debug.WriteLine("the message=" + m);
Debug.WriteLine("channel=" + c);
try
{
var sentinelServer = multiplexer.GetServer("127.0.0.1", 26379).SentinelGetMasterAddressByName("mymaster");
Debug.WriteLine("Current server=" + sentinelServer);
Debug.Flush();
}
catch (Exception)
{
var sentinelServer = multiplexer.GetServer("127.0.0.1", 26380).SentinelGetMasterAddressByName("mymaster");
Debug.WriteLine("Current server=" + sentinelServer );
Debug.Flush();
}
});