use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class ClusterBinaryJedisCommandsTest method setUp.
@Before
public void setUp() throws InterruptedException {
node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
node1.connect();
node1.flushAll();
node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
node2.connect();
node2.flushAll();
node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
node3.connect();
node3.flushAll();
// ---- configure cluster
// add nodes to cluster
node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
// split available slots across the three nodes
int slotsPerNode = JedisCluster.HASHSLOTS / 3;
int[] node1Slots = new int[slotsPerNode];
int[] node2Slots = new int[slotsPerNode + 1];
int[] node3Slots = new int[slotsPerNode];
for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) {
if (i < slotsPerNode) {
node1Slots[slot1++] = i;
} else if (i > slotsPerNode * 2) {
node3Slots[slot3++] = i;
} else {
node2Slots[slot2++] = i;
}
}
node1.clusterAddSlots(node1Slots);
node2.clusterAddSlots(node2Slots);
node3.clusterAddSlots(node3Slots);
waitForClusterReady();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
jedisCluster = new JedisCluster(jedisClusterNode);
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class ClusterScriptingCommandsTest method setUp.
@Before
public void setUp() throws InterruptedException {
node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort());
node1.connect();
node1.flushAll();
node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort());
node2.connect();
node2.flushAll();
node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort());
node3.connect();
node3.flushAll();
// ---- configure cluster
// add nodes to cluster
node1.clusterMeet("127.0.0.1", nodeInfo2.getPort());
node1.clusterMeet("127.0.0.1", nodeInfo3.getPort());
// split available slots across the three nodes
int slotsPerNode = JedisCluster.HASHSLOTS / 3;
int[] node1Slots = new int[slotsPerNode];
int[] node2Slots = new int[slotsPerNode + 1];
int[] node3Slots = new int[slotsPerNode];
for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) {
if (i < slotsPerNode) {
node1Slots[slot1++] = i;
} else if (i > slotsPerNode * 2) {
node3Slots[slot3++] = i;
} else {
node2Slots[slot2++] = i;
}
}
node1.clusterAddSlots(node1Slots);
node2.clusterAddSlots(node2Slots);
node3.clusterAddSlots(node3Slots);
waitForClusterReady();
jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
jedisCluster = new JedisCluster(jedisClusterNode);
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class HostAndPortUtil method parseHosts.
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;
}
use of redis.clients.jedis.HostAndPort in project druid by druid-io.
the class RedisCacheFactory method create.
public static Cache create(final RedisCacheConfig config) {
if (config.getCluster() != null && StringUtils.isNotBlank(config.getCluster().getNodes())) {
Set<HostAndPort> nodes = Arrays.stream(config.getCluster().getNodes().split(",")).map(String::trim).filter(StringUtils::isNotBlank).map(hostAndPort -> {
int index = hostAndPort.indexOf(':');
if (index <= 0 || index == hostAndPort.length()) {
throw new IAE("Invalid redis cluster configuration: %s", hostAndPort);
}
int port;
try {
port = Integer.parseInt(hostAndPort.substring(index + 1));
} catch (NumberFormatException e) {
throw new IAE("Invalid port in %s", hostAndPort);
}
if (port <= 0 || port > 65535) {
throw new IAE("Invalid port in %s", hostAndPort);
}
return new HostAndPort(hostAndPort.substring(0, index), port);
}).collect(Collectors.toSet());
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(config.getMaxTotalConnections());
poolConfig.setMaxIdle(config.getMaxIdleConnections());
poolConfig.setMinIdle(config.getMinIdleConnections());
JedisCluster cluster;
if (config.getPassword() != null) {
cluster = new JedisCluster(nodes, // connection timeout
config.getTimeout().getMillisecondsAsInt(), // read timeout
config.getTimeout().getMillisecondsAsInt(), config.getCluster().getMaxRedirection(), config.getPassword().getPassword(), poolConfig);
} else {
cluster = new JedisCluster(nodes, // connection timeout and read timeout
config.getTimeout().getMillisecondsAsInt(), config.getCluster().getMaxRedirection(), poolConfig);
}
return new RedisClusterCache(cluster, config);
} else {
if (StringUtils.isBlank(config.getHost())) {
throw new IAE("Invalid redis configuration. no redis server or cluster configured.");
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(config.getMaxTotalConnections());
poolConfig.setMaxIdle(config.getMaxIdleConnections());
poolConfig.setMinIdle(config.getMinIdleConnections());
return new RedisStandaloneCache(new JedisPool(poolConfig, config.getHost(), config.getPort(), // connection timeout and read timeout
config.getTimeout().getMillisecondsAsInt(), config.getPassword() == null ? null : config.getPassword().getPassword(), config.getDatabase(), null), config);
}
}
use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.
the class ExceptionsTest method askData.
@Test
public void askData() {
HostAndPort hap = new HostAndPort("", 0);
int slot = -1;
try {
throw new JedisAskDataException(MESSAGE, hap, slot);
} catch (Exception e) {
assertSame(JedisAskDataException.class, e.getClass());
assertEquals(MESSAGE, e.getMessage());
assertNull(e.getCause());
}
try {
throw new JedisAskDataException(CAUSE, hap, slot);
} catch (Exception e) {
assertSame(JedisAskDataException.class, e.getClass());
assertEquals(CAUSE, e.getCause());
assertEquals(CAUSE.toString(), e.getMessage());
}
try {
throw new JedisAskDataException(MESSAGE, CAUSE, hap, slot);
} catch (Exception e) {
assertSame(JedisAskDataException.class, e.getClass());
assertEquals(MESSAGE, e.getMessage());
assertEquals(CAUSE, e.getCause());
}
}
Aggregations