[英]How to java client connect Redis cluster when the node died
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. 我有一个Redis集群,其中有1个主机(ip:192.168.56.101)和2个从机(ip:192.168.56.102,192.168.56.103),我使用jedis连接到主机读取和写入数据。
JedisPool pool = new JedisPool(new JedisPoolConfig(), "192.168.56.101");
One day, my master node die, so jedis can not connect to cluster. 有一天,我的主节点死亡,因此jedis无法连接到集群。 Could you please help me, how to connect cluster if the host connect died?
您能帮我吗,如果主机连接中断,如何连接群集? Thank you
谢谢
Sentinels need to be installed for each node on your cluster. 需要为集群中的每个节点安装Sentinels。 Sentinels take care of failover.
哨兵负责故障转移。 For more information on sentinels, http://redis.io/topics/sentinel
有关前哨的更多信息,请访问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. 基本思想是,当Redis节点之一发生故障时,Redis前哨在它们之间进行协调,并将任何从属节点升级为主节点。 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.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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.