How to PUB/SUB sentinel

295 views
Skip to first unread message

Armache Antoni

unread,
Apr 5, 2020, 12:39:51 PM4/5/20
to lettuce-redis-client-users
Hi gyus,

I would like to susbribe on the (master sentinel) in order to receive events and message when master dies such as:

- connection down
- connection up
- switch to new master

i have the following code but when i stop master, i am not receiving events or message could someone show me what is wrong in my code:

TOPOLOGY:

+----+  +----+  +----+            
| M1 |  | R2 |  | R3 |
| S1 |--| S2 |--| S3 |  
+----+  +----+  +----+           

CODE:

// Lettuce
static public RedisClient redisClient;
static public RedisClient sentinelClient;
static public RedisAsyncCommands<String, String> asyncCommands = null;
static public StatefulRedisMasterSlaveConnection<String, String> redisConnection;
// Create Redis-URI
RedisURI redisUri = RedisURI.builder().withSentinel("192.168.158.200", 26379)
                    .withSentinel("192.168.158.201", 26379).withSentinel("192.168.158.202", 26379)
                    .withSentinelMasterId("mymaster").build();
// Create Sentinel-Client
sentinelClient = RedisClient.create(redisUri);
       
// Create Redis-Client
            redisClient = RedisClient.create();
            redisClient.setOptions(ClientOptions.builder().autoReconnect(true)
                    // .timeoutOptions(new TimeoutOptions())
                    /**
                     * DEFAULT: Accept commands when auto-reconnect is enabled, reject commands when
                     * auto-reconnect is disabled. ACCEPT_COMMANDS: Accept commands in disconnected
                     * state. REJECT_COMMANDS: Reject commands in disconnected state.
                     */
                    .disconnectedBehavior(DisconnectedBehavior.REJECT_COMMANDS).pingBeforeActivateConnection(true)
                    .build());
// Create Redis-Connection
            redisConnection = MasterSlave.connect(redisClient, new Utf8StringCodec(), redisUri);
            redisConnection.setReadFrom(ReadFrom.MASTER_PREFERRED);
             asyncCommands = redisConnection.async();
  
//The listener which is not working!!!!!!
    public static void addSentinelListennerChannel() {
        
        if (sentinelClient == null)
            return;

        // Subscribe: Sentinel -> check failover
   
    RedisPubSubAdapter<String, String> listener2 = new RedisPubSubAdapter<String, String>() {
            
            @Override
            public void message(String channel, String message) {
                
                if (channel.equals("+switch-master")) {
                    SmppTestingSbb.log.info("sentinel MASTER CHANGED");
                }
                
                if (channel.equals("+sdown")) {
                    SmppTestingSbb.log.info("sentinel MASTER SDOWN");
                }
                
                if (channel.equals("+odown")) {
                    SmppTestingSbb.log.info("sentinel MASTER ODOWN");
                }
            }
        };
   
        StatefulRedisPubSubConnection<String, String> pubsubConn2 = sentinelClient.connectPubSub();
        pubsubConn2.addListener(listener2);
        RedisPubSubAsyncCommands<String, String> async2 = pubsubConn2.async();
        async2.subscribe("+sdown");
        async2.subscribe("+odown");
        async2.subscribe("+switch-master");
            
   
        
    }           

Br.

Armache Antoni

unread,
Apr 5, 2020, 6:19:45 PM4/5/20
to lettuce-redis-client-users
Found the solution on own..... no need to bother guys.....

--
You received this message because you are subscribed to the Google Groups "lettuce-redis-client-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lettuce-redis-clien...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lettuce-redis-client-users/d0f56e06-d0bb-45ff-8661-5fc5025c845c%40googlegroups.com.

kanurich...@gmail.com

unread,
Jun 1, 2020, 6:22:56 PM6/1/20
to lettuce-redis-client-users
Can you please share the solution here as well
To unsubscribe from this group and stop receiving emails from it, send an email to lettuce-redis-client-users+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages