use of redis.clients.jedis.JedisCluster 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.JedisCluster in project e3mall by colg-cloud.
the class JedisTest method testJedisCluster.
/**
* redis 集群
*/
@Test
public void testJedisCluster() {
// 创建一个JedisCluster对象,有一个参数nodes是一个set类型,set中包含若干个HostAndPort对象
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.21.103", 7000));
nodes.add(new HostAndPort("192.168.21.103", 7001));
nodes.add(new HostAndPort("192.168.21.103", 7002));
nodes.add(new HostAndPort("192.168.21.103", 7003));
nodes.add(new HostAndPort("192.168.21.103", 7004));
nodes.add(new HostAndPort("192.168.21.103", 7005));
JedisCluster jedisCluster = new JedisCluster(nodes);
// 直接使用JedisCluster对象操作redis,操作完不用关闭,自带连接池
jedisCluster.set("test", "123");
System.out.println(jedisCluster.get("test"));
// 系统关闭前,关闭JedisCluster对象
try {
jedisCluster.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of redis.clients.jedis.JedisCluster in project cachecloud by sohutv.
the class RedisClusterBuilder method build.
public JedisCluster build() {
if (jedisCluster == null) {
while (true) {
try {
lock.tryLock(10, TimeUnit.SECONDS);
if (jedisCluster != null) {
return jedisCluster;
}
String url = String.format(ConstUtils.REDIS_CLUSTER_URL, String.valueOf(appId));
String response = HttpUtils.doGet(url);
JSONObject jsonObject = null;
try {
jsonObject = JSONObject.parseObject(response);
} catch (Exception e) {
logger.error("remote build error, appId: {}", appId, e);
}
if (jsonObject == null) {
logger.error("get cluster info for appId: {} error. continue...", appId);
continue;
}
int status = jsonObject.getIntValue("status");
String message = jsonObject.getString("message");
/**
* 检查客户端版本 *
*/
if (status == ClientStatusEnum.ERROR.getStatus()) {
throw new IllegalStateException(message);
} else if (status == ClientStatusEnum.WARN.getStatus()) {
logger.warn(message);
} else {
logger.info(message);
}
Set<HostAndPort> nodeList = new HashSet<HostAndPort>();
// 形如 ip1:port1,ip2:port2,ip3:port3
String nodeInfo = jsonObject.getString("shardInfo");
// 为了兼容,如果允许直接nodeInfo.split(" ")
nodeInfo = nodeInfo.replace(" ", ",");
String[] nodeArray = nodeInfo.split(",");
for (String node : nodeArray) {
String[] ipAndPort = node.split(":");
if (ipAndPort.length < 2) {
continue;
}
String ip = ipAndPort[0];
int port = Integer.parseInt(ipAndPort[1]);
nodeList.add(new HostAndPort(ip, port));
}
// 收集上报数据
if (clientStatIsOpen) {
ClientDataCollectReportExecutor.getInstance();
}
String password = jsonObject.getString("password");
if (StringUtil.isBlank(password)) {
jedisCluster = new JedisCluster(nodeList, connectionTimeout, soTimeout, maxRedirections, jedisPoolConfig);
} else {
jedisCluster = new JedisCluster(nodeList, connectionTimeout, soTimeout, maxRedirections, password, jedisPoolConfig);
}
return jedisCluster;
} catch (Throwable e) {
logger.error(e.getMessage(), e);
} finally {
lock.unlock();
}
try {
// 活锁
TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
} else {
return jedisCluster;
}
}
use of redis.clients.jedis.JedisCluster in project weicoder by wdcode.
the class JedisBuilder method buildCluster.
/**
* 构建Jedis集群
*
* @param name 名称
* @return Jedis集群
*/
public static JedisCluster buildCluster(String name) {
// 实例化Jedis配置
GenericObjectPoolConfig<Connection> config = new GenericObjectPoolConfig<Connection>();
// 设置属性
config.setMaxTotal(RedisParams.getMaxTotal(name));
config.setMaxIdle(RedisParams.getMaxIdle(name));
config.setMaxWait(Duration.ofMillis(RedisParams.getMaxWait(name)));
// 服务器节点
Set<HostAndPort> nodes = Sets.newSet();
for (String server : RedisParams.getCluster(name)) {
String[] s = StringUtil.split(server, StringConstants.COLON);
nodes.add(new HostAndPort(s[0], W.C.toInt(s[1])));
}
// 生成JedisCluster
Logs.info("redis init cluster nodes={}", nodes);
return new JedisCluster(nodes, RedisParams.getTimeOut(name), RedisParams.getTimeOut(name), 5, RedisParams.getPassword(name), config);
}
Aggregations