【问题标题】:How to java client connect Redis cluster when the node diedjava客户端如何在节点死亡时连接Redis集群
【发布时间】:2016-03-14 13:15:50
【问题描述】:

我有一个 Redis 集群,有 1 个 master(ip: 192.168.56.101)和 2 个 slave(ip: 192.168.56.102, 192.168.56.103),我使用 jedis 连接 master 读写数据。

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

有一天,我的主节点死了,所以 jedis 无法连接到集群。 你能帮我吗,如果主机连接死了,如何连接集群? 谢谢你

【问题讨论】:

    标签: java redis jedis


    【解决方案1】:

    需要为集群上的每个节点安装哨兵。哨兵负责故障转移。有关哨兵的更多信息,http://redis.io/topics/sentinel

    基本思路是,当其中一个redis节点宕机时,redis哨兵相互协调,将任意一个slave升级为master。 以下示例代码应该可以工作。

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-17
      • 2012-05-12
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 2014-06-19
      • 2020-10-21
      相关资源
      最近更新 更多