2
votes

I have a Redis cluster with 1 master(ip: 192.168.56.101) and 2 slaves(ip: 192.168.56.102, 192.168.56.103), I use jedis to connect to master read and write data.

JedisPool pool = new JedisPool(new JedisPoolConfig(), "192.168.56.101");   

One day, my master node die, so jedis can not connect to cluster. Could you please help me, how to connect cluster if the host connect died? Thank you

1

1 Answers

0
votes

Sentinels need to be installed for each node on your cluster. Sentinels take care of failover. For more information on sentinels, http://redis.io/topics/sentinel

The basic idea is, when one of the redis nodes go down, redis sentinels coordinate among themselves and upgrade any of the slaves to master. Following sample code should work.

JedisSentinelPool runs a background polling thread to get the master node which would be returned by pool.getResource();

JedisSentinelPool pool = new JedisSentinelPool(String masterName, Set<String> sentinels);
Jedis jedis = null;
try{
   jedis = pool.getResource();
} catch(Exception e){ 
   //log exception
} finally {
  pool.returnResource(jedis);
}