2

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?

Community
  • 1
  • 1
Tom
  • 510
  • 1
  • 7
  • 24
  • Note that pub/sub isn't guaranteed messaging. These events can be lost. I suggest using a library that supports Sentinel: ServiceStack,Redis (paid) or csredis (doesn't support redis 3 yet) – Bruno Garcia May 05 '16 at 15:04

1 Answers1

0

In this line:

ConnectionMultiplexer cm = ConnectionMultiplexer.Connect(configOpt);

Instead of using the Connect() method, just use SentinelConnect(url) and instead of URL, just provide sentinel ip:ports. For instance:

"172.78.25.163:26379,172.78.25.162:26379,172.78.25.161:26379"
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77