Add automatic discovery of cluster nodes
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.IllegalFormatException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -1192,4 +1194,8 @@ public class JedisCluster implements JedisCommands, BasicCommands {
|
||||
}
|
||||
}.run();
|
||||
}
|
||||
|
||||
public Map<String, JedisPool> getClusterNodes() {
|
||||
return connectionHandler.getNodes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,47 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public interface JedisClusterConnectionHandler {
|
||||
|
||||
public abstract class JedisClusterConnectionHandler {
|
||||
|
||||
Jedis getConnection();
|
||||
protected Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
|
||||
|
||||
abstract Jedis getConnection();
|
||||
|
||||
public JedisClusterConnectionHandler(Set<HostAndPort> nodes) {
|
||||
initializeSlotsCache(nodes);
|
||||
}
|
||||
|
||||
public Map<String, JedisPool> getNodes() {
|
||||
return nodes;
|
||||
}
|
||||
|
||||
private void initializeSlotsCache(Set<HostAndPort> nodes) {
|
||||
for (HostAndPort hostAndPort : nodes) {
|
||||
JedisPool jp = new JedisPool(hostAndPort.getHost(), hostAndPort.getPort());
|
||||
this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp);
|
||||
this.nodes.putAll(discoverClusterNodes(jp));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Map<? extends String, ? extends JedisPool> discoverClusterNodes(JedisPool jp) {
|
||||
Map<String, JedisPool> discoveredNodes = new HashMap<String, JedisPool>();
|
||||
String localNodes = jp.getResource().clusterNodes();
|
||||
for (String nodeInfo : localNodes.split("\n")) {
|
||||
HostAndPort node = getHostAndPortFromNodeLine(nodeInfo);
|
||||
JedisPool nodePool = new JedisPool(node.getHost(), node.getPort());
|
||||
discoveredNodes.put(node.getHost() + node.getPort(), nodePool);
|
||||
}
|
||||
return discoveredNodes;
|
||||
}
|
||||
|
||||
private HostAndPort getHostAndPortFromNodeLine(String nodeInfo) {
|
||||
String stringHostAndPort = nodeInfo.split(" ",3)[1];
|
||||
String[] arrayHostAndPort = stringHostAndPort.split(":");
|
||||
return new HostAndPort(arrayHostAndPort[0], Integer.valueOf(arrayHostAndPort[1]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,17 @@
|
||||
package redis.clients.jedis;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import redis.clients.util.Pool;
|
||||
|
||||
public class JedisRandomConnectionHandler implements JedisClusterConnectionHandler {
|
||||
public class JedisRandomConnectionHandler extends JedisClusterConnectionHandler {
|
||||
|
||||
private Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
|
||||
|
||||
public JedisRandomConnectionHandler(Set<HostAndPort> nodes) {
|
||||
initializeSlotsCache(nodes);
|
||||
super(nodes);
|
||||
}
|
||||
|
||||
private void initializeSlotsCache(Set<HostAndPort> nodes) {
|
||||
for (HostAndPort hostAndPort : nodes) {
|
||||
JedisPool jp = new JedisPool(hostAndPort.getHost(),
|
||||
hostAndPort.getPort());
|
||||
this.nodes.put(hostAndPort.getHost() + hostAndPort.getPort(), jp);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Jedis getConnection() {
|
||||
|
||||
Reference in New Issue
Block a user