Launch 3 nodes on ports 7379 to 7381 with cluster enabled.
Add command CLUSTER NODES
This commit is contained in:
@@ -9,83 +9,106 @@ import redis.clients.jedis.Protocol;
|
||||
public class HostAndPortUtil {
|
||||
private static List<HostAndPort> redisHostAndPortList = new ArrayList<HostAndPort>();
|
||||
private static List<HostAndPort> sentinelHostAndPortList = new ArrayList<HostAndPort>();
|
||||
private static List<HostAndPort> clusterHostAndPortList = new ArrayList<HostAndPort>();
|
||||
|
||||
static {
|
||||
|
||||
HostAndPort defaulthnp1 = new HostAndPort("localhost", Protocol.DEFAULT_PORT);
|
||||
redisHostAndPortList.add(defaulthnp1);
|
||||
|
||||
HostAndPort defaulthnp2 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 1);
|
||||
redisHostAndPortList.add(defaulthnp2);
|
||||
|
||||
HostAndPort defaulthnp3 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 2);
|
||||
redisHostAndPortList.add(defaulthnp3);
|
||||
|
||||
HostAndPort defaulthnp4 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 3);
|
||||
redisHostAndPortList.add(defaulthnp4);
|
||||
|
||||
HostAndPort defaulthnp5 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 4);
|
||||
redisHostAndPortList.add(defaulthnp5);
|
||||
|
||||
HostAndPort defaulthnp6 = new HostAndPort("localhost", Protocol.DEFAULT_PORT + 5);
|
||||
redisHostAndPortList.add(defaulthnp6);
|
||||
|
||||
HostAndPort defaulthnp7 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT);
|
||||
sentinelHostAndPortList.add(defaulthnp7);
|
||||
|
||||
HostAndPort defaulthnp8 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 1);
|
||||
sentinelHostAndPortList.add(defaulthnp8);
|
||||
|
||||
HostAndPort defaulthnp9 = new HostAndPort("localhost", Protocol.DEFAULT_SENTINEL_PORT + 2);
|
||||
sentinelHostAndPortList.add(defaulthnp9);
|
||||
HostAndPort defaulthnp1 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_PORT);
|
||||
redisHostAndPortList.add(defaulthnp1);
|
||||
|
||||
String envRedisHosts = System.getProperty("redis-hosts");
|
||||
String envSentinelHosts = System.getProperty("sentinel-hosts");
|
||||
|
||||
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
|
||||
sentinelHostAndPortList = parseHosts(envSentinelHosts, sentinelHostAndPortList);
|
||||
HostAndPort defaulthnp2 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_PORT + 1);
|
||||
redisHostAndPortList.add(defaulthnp2);
|
||||
|
||||
HostAndPort defaulthnp3 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_PORT + 2);
|
||||
redisHostAndPortList.add(defaulthnp3);
|
||||
|
||||
HostAndPort defaulthnp4 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_PORT + 3);
|
||||
redisHostAndPortList.add(defaulthnp4);
|
||||
|
||||
HostAndPort defaulthnp5 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_PORT + 4);
|
||||
redisHostAndPortList.add(defaulthnp5);
|
||||
|
||||
HostAndPort defaulthnp6 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_PORT + 5);
|
||||
redisHostAndPortList.add(defaulthnp6);
|
||||
|
||||
HostAndPort defaulthnp7 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_SENTINEL_PORT);
|
||||
sentinelHostAndPortList.add(defaulthnp7);
|
||||
|
||||
HostAndPort defaulthnp8 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_SENTINEL_PORT + 1);
|
||||
sentinelHostAndPortList.add(defaulthnp8);
|
||||
|
||||
HostAndPort defaulthnp9 = new HostAndPort("localhost",
|
||||
Protocol.DEFAULT_SENTINEL_PORT + 2);
|
||||
sentinelHostAndPortList.add(defaulthnp9);
|
||||
|
||||
clusterHostAndPortList.add(new HostAndPort("localhost", 7379));
|
||||
clusterHostAndPortList.add(new HostAndPort("localhost", 7380));
|
||||
clusterHostAndPortList.add(new HostAndPort("localhost", 7381));
|
||||
|
||||
String envRedisHosts = System.getProperty("redis-hosts");
|
||||
String envSentinelHosts = System.getProperty("sentinel-hosts");
|
||||
String envClusterHosts = System.getProperty("cluster-hosts");
|
||||
|
||||
redisHostAndPortList = parseHosts(envRedisHosts, redisHostAndPortList);
|
||||
sentinelHostAndPortList = parseHosts(envSentinelHosts,
|
||||
sentinelHostAndPortList);
|
||||
clusterHostAndPortList = parseHosts(envClusterHosts,
|
||||
clusterHostAndPortList);
|
||||
}
|
||||
|
||||
public static List<HostAndPort> parseHosts(String envHosts, List<HostAndPort> existingHostsAndPorts) {
|
||||
|
||||
if (null != envHosts && 0 < envHosts.length()) {
|
||||
|
||||
String[] hostDefs = envHosts.split(",");
|
||||
|
||||
if (null != hostDefs && 2 <= hostDefs.length) {
|
||||
|
||||
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(hostDefs.length);
|
||||
|
||||
for (String hostDef : hostDefs) {
|
||||
|
||||
String[] hostAndPort = hostDef.split(":");
|
||||
|
||||
if (null != hostAndPort && 2 == hostAndPort.length) {
|
||||
String host = hostAndPort[0];
|
||||
int port = Protocol.DEFAULT_PORT;
|
||||
|
||||
try {
|
||||
port = Integer.parseInt(hostAndPort[1]);
|
||||
} catch (final NumberFormatException nfe) {
|
||||
}
|
||||
|
||||
envHostsAndPorts.add(new HostAndPort(host, port));
|
||||
}
|
||||
}
|
||||
|
||||
return envHostsAndPorts;
|
||||
}
|
||||
}
|
||||
|
||||
return existingHostsAndPorts;
|
||||
public static List<HostAndPort> parseHosts(String envHosts,
|
||||
List<HostAndPort> existingHostsAndPorts) {
|
||||
|
||||
if (null != envHosts && 0 < envHosts.length()) {
|
||||
|
||||
String[] hostDefs = envHosts.split(",");
|
||||
|
||||
if (null != hostDefs && 2 <= hostDefs.length) {
|
||||
|
||||
List<HostAndPort> envHostsAndPorts = new ArrayList<HostAndPort>(
|
||||
hostDefs.length);
|
||||
|
||||
for (String hostDef : hostDefs) {
|
||||
|
||||
String[] hostAndPort = hostDef.split(":");
|
||||
|
||||
if (null != hostAndPort && 2 == hostAndPort.length) {
|
||||
String host = hostAndPort[0];
|
||||
int port = Protocol.DEFAULT_PORT;
|
||||
|
||||
try {
|
||||
port = Integer.parseInt(hostAndPort[1]);
|
||||
} catch (final NumberFormatException nfe) {
|
||||
}
|
||||
|
||||
envHostsAndPorts.add(new HostAndPort(host, port));
|
||||
}
|
||||
}
|
||||
|
||||
return envHostsAndPorts;
|
||||
}
|
||||
}
|
||||
|
||||
return existingHostsAndPorts;
|
||||
}
|
||||
|
||||
|
||||
public static List<HostAndPort> getRedisServers() {
|
||||
return redisHostAndPortList;
|
||||
}
|
||||
|
||||
public static List<HostAndPort> getSentinelServers() {
|
||||
return sentinelHostAndPortList;
|
||||
return redisHostAndPortList;
|
||||
}
|
||||
|
||||
public static List<HostAndPort> getSentinelServers() {
|
||||
return sentinelHostAndPortList;
|
||||
}
|
||||
|
||||
public static List<HostAndPort> getClusterServers() {
|
||||
return clusterHostAndPortList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package redis.clients.jedis.tests.commands;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import redis.clients.jedis.HostAndPort;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.tests.HostAndPortUtil;
|
||||
import redis.clients.jedis.tests.JedisTestBase;
|
||||
|
||||
public class ClusterCommandsTest extends JedisTestBase {
|
||||
private Jedis node1;
|
||||
private Jedis node2;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0);
|
||||
HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1);
|
||||
|
||||
node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
|
||||
node1.connect();
|
||||
node1.flushAll();
|
||||
|
||||
node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
|
||||
node2.connect();
|
||||
node2.flushAll();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
node1.disconnect();
|
||||
node2.disconnect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clusterNodes() {
|
||||
String nodes = node1.clusterNodes();
|
||||
assertEquals(1, nodes.split("\n").length);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user