use of redis.clients.jedis.HostAndPort in project YCSB by brianfrankcooper.
the class RedisClient method init.
public void init() throws DBException {
Properties props = getProperties();
int port;
String portString = props.getProperty(PORT_PROPERTY);
if (portString != null) {
port = Integer.parseInt(portString);
} else {
port = Protocol.DEFAULT_PORT;
}
String host = props.getProperty(HOST_PROPERTY);
boolean clusterEnabled = Boolean.parseBoolean(props.getProperty(CLUSTER_PROPERTY));
if (clusterEnabled) {
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
jedisClusterNodes.add(new HostAndPort(host, port));
jedis = new JedisCluster(jedisClusterNodes);
} else {
String redisTimeout = props.getProperty(TIMEOUT_PROPERTY);
if (redisTimeout != null) {
jedis = new Jedis(host, port, Integer.parseInt(redisTimeout));
} else {
jedis = new Jedis(host, port);
}
((Jedis) jedis).connect();
}
String password = props.getProperty(PASSWORD_PROPERTY);
if (password != null) {
((BasicCommands) jedis).auth(password);
}
}
use of redis.clients.jedis.HostAndPort in project incubator-dubbo-ops by apache.
the class RedisMetaDataCollector method init.
@Override
public void init() {
timeout = url.getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
password = url.getPassword();
if (url.getParameter(CLUSTER_KEY, false)) {
jedisClusterNodes = new HashSet<>();
String[] addresses = COMMA_SPLIT_PATTERN.split(url.getAddress());
for (String address : addresses) {
URL tmpUrl = url.setAddress(address);
jedisClusterNodes.add(new HostAndPort(tmpUrl.getHost(), tmpUrl.getPort()));
}
} else {
int database = url.getParameter(REDIS_DATABASE_KEY, 0);
pool = new JedisPool(new JedisPoolConfig(), url.getHost(), url.getPort(), timeout, url.getPassword(), database);
}
}
use of redis.clients.jedis.HostAndPort in project iris by chicc999.
the class JedisClusterFactory method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout, maxRedirections, genericObjectPoolConfig);
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class RedisSentinelTest method testSentinel.
@Test
public void testSentinel() {
JedisSentinelPool sentinelPool = ClientBuilder.redisSentinel(appId).setConnectionTimeout(2000).setSoTimeout(1000).build("");
HostAndPort currentHostMaster = sentinelPool.getCurrentHostMaster();
logger.info("current master: {}", currentHostMaster.toString());
Jedis jedis = sentinelPool.getResource();
for (int i = 0; i < 10; i++) {
jedis.lpush("mylist", "list-" + i);
}
jedis.close();
sentinelPool.destroy();
}
use of redis.clients.jedis.HostAndPort in project new-cloud by xie-summer.
the class RedisClusterBuilder method build.
public JedisCluster build(String url) {
if (jedisCluster == null) {
while (true) {
try {
lock.tryLock(10, TimeUnit.SECONDS);
if (jedisCluster != null) {
return jedisCluster;
}
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(url);
}
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;
}
}
Aggregations