简体   繁体   English

节点死亡时如何使用Java客户端连接Redis集群

[英]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.

相关问题 用于连接ElasticCache Redis缓存节点的Java客户端 - Java Client to connect ElasticCache Redis Cache Node Java如何使用jedis连接到Docker Redis集群实例? - How to connect to a Docker Redis cluster instance using jedis for Java? 如何将我的基于 Java 的 Cassandra 客户端连接到 Cassandra 集群,而不是特定节点? - How to connect my Java based Cassandra client to a Cassandra cluster, rather than a specific node? 如果群集中的第一个节点已关闭,Redis客户端(Redission)将停止工作 - Redis client (Redission) stops working if first node is down in the cluster 如何使用 lettuce 4.2 集群客户端将复杂的 java 对象推送到 Redis - How to push complex java objects to Redis using lettuce 4.2 Cluster client 使用Java连接到Docker上的多节点Couchbase集群 - Connect to a multi node Couchbase cluster on Docker in Java 从Java连接到AWS Elasticache(Redis集群)的正确方法是什么? - What is a proper way to connect to AWS Elasticache (Redis cluster) from Java? 如何在java中锁定应用程序的redis集群 - How to lock redis cluster for an application in java 如何使用Elasticsearch Node Client Java连接到远程服务器 - How to connect to remote server using Elasticsearch Node Client Java Lettuce 无法使用 SSL 连接到 Redis 集群,但可以通过将其视为独立节点来使用 SSL 连接到同一 Redis 服务器 - Lettuce can't connect to Redis Cluster using SSL but can connect to same Redis server using SSL by treating it as a Standalone node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM