I have searched for this answer all over, but there is no one who has demonstrated how to successfully do this. So, I have taken some code from Redis failover with StackExchange / Sentinel from C# and created a simple code-block:
public static void Failover()
{
List<RedisConnection> redisConnections = _GetRedisConnections();
ConfigurationOptions configOpt = _CreateRedisConfiguration(CommandMap.Sentinel, null, redisConnections);
ConnectionMultiplexer cm = ConnectionMultiplexer.Connect(configOpt);
cm.GetSubscriber().Subscribe("+switch-master", WriteToConsole);
}
private List<RedisConnection> _GetRedisConnections()
{
List<RedisConnection> redisConnections = new List<RedisConnection>();
RedisConnection rc1 = new RedisConnection("localhost", 26379);
redisConnections.Add(rc1);
return redisConnections;
}
private ConfigurationOptions _CreateRedisConfiguration(CommandMap commandMap, string password, List<RedisConnection> connections)
{
ConfigurationOptions connection = new ConfigurationOptions
{
CommandMap = commandMap,
AbortOnConnectFail = true,
AllowAdmin = true,
TieBreaker = string.Empty
};
connections.ForEach(s => {connection.EndPoints.Add(s.Host, .Port);});
return connection;
}
private void WriteToConsole(RedisChannel channel, RedisValue value)
{
Debug.WriteLine("Hello!");
}
Here, Failover()
method is called on application start. In this method, I am simply creating a connection to the Sentinel server deployed to the current server. Then I try to subscribe to the "+switch-master" event of the Sentinel. The event handler WriteToConsole
is a simple method that just tells me that my application is actually listening to the event.
I want to eventually be able to add the logic inside the WriteToConsole
placeholder method to make the switch to the new master Redis server.
But this setup is not working. I do not see any effect on the application or a console dialogue pop up when I stop the master Redis server monitored by the Sentinel in question. What am I doing wrong? Am I in the right direction?