use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.
the class RedisSentinelBuilder method build.
public JedisSentinelPool build() {
if (sentinelPool == null) {
while (true) {
try {
LOCK.tryLock(10, TimeUnit.MILLISECONDS);
if (sentinelPool == null) {
/**
* http请求返回的结果是空的;
*/
String response = HttpUtils.doGet(String.format(ConstUtils.REDIS_SENTINEL_URL, appId));
if (response == null || response.isEmpty()) {
logger.warn("get response from remote server error, appId: {}, continue...", appId);
continue;
}
/**
* http请求返回的结果是无效的;
*/
ObjectMapper mapper = new ObjectMapper();
JsonNode heartbeatInfo = null;
try {
heartbeatInfo = mapper.readTree(response);
} catch (Exception e) {
logger.error("heartbeat error, appId: {}. continue...", appId, e);
}
if (heartbeatInfo == null) {
logger.error("get sentinel info for appId: {} error. continue...", appId);
continue;
}
/** 检查客户端版本 **/
if (heartbeatInfo.get("status").intValue() == ClientStatusEnum.ERROR.getStatus()) {
throw new IllegalStateException(heartbeatInfo.get("message").textValue());
} else if (heartbeatInfo.get("status").intValue() == ClientStatusEnum.WARN.getStatus()) {
logger.warn(heartbeatInfo.get("message").textValue());
} else {
logger.info(heartbeatInfo.get("message").textValue());
}
/**
* 有效的请求:取出masterName和sentinels,并创建JedisSentinelPool的实例;
*/
String masterName = heartbeatInfo.get("masterName").asText();
String sentinels = heartbeatInfo.get("sentinels").asText();
Set<String> sentinelSet = new HashSet<String>();
for (String sentinelStr : sentinels.split(" ")) {
String[] sentinelArr = sentinelStr.split(":");
if (sentinelArr.length == 2) {
sentinelSet.add(sentinelStr);
}
}
//收集上报数据
// ClientDataCollectReportExecutor.getInstance();
sentinelPool = new JedisSentinelPool(masterName, sentinelSet, poolConfig, connectionTimeout, soTimeout, null, Protocol.DEFAULT_DATABASE);
return sentinelPool;
}
} catch (Throwable e) {
//容错
logger.error("error in build, appId: {}", appId, e);
} finally {
LOCK.unlock();
}
try {
//活锁
TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
}
return sentinelPool;
}
use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.
the class JedisSentinelPoolTest method initializeWithNotAvailableSentinelsShouldThrowException.
@Test(expected = JedisConnectionException.class)
public void initializeWithNotAvailableSentinelsShouldThrowException() {
Set<String> wrongSentinels = new HashSet<String>();
wrongSentinels.add(new HostAndPort("localhost", 65432).toString());
wrongSentinels.add(new HostAndPort("localhost", 65431).toString());
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
pool.destroy();
}
use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.
the class JedisSentinelPoolTest method customClientName.
@Test
public void customClientName() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 0, "my_shiny_client_name");
Jedis jedis = pool.getResource();
try {
assertEquals("my_shiny_client_name", jedis.clientGetname());
} finally {
jedis.close();
pool.destroy();
}
assertTrue(pool.isClosed());
}
use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.
the class JedisSentinelPoolTest method checkCloseableConnections.
@Test
public void checkCloseableConnections() throws Exception {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
pool.close();
assertTrue(pool.isClosed());
}
use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.
the class JedisSentinelPoolTest method returnResourceShouldResetState.
@Test
public void returnResourceShouldResetState() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
Jedis jedis = pool.getResource();
Jedis jedis2 = null;
try {
jedis.set("hello", "jedis");
Transaction t = jedis.multi();
t.set("hello", "world");
jedis.close();
jedis2 = pool.getResource();
assertTrue(jedis == jedis2);
assertEquals("jedis", jedis2.get("hello"));
} catch (JedisConnectionException e) {
if (jedis2 != null) {
jedis2 = null;
}
} finally {
jedis2.close();
pool.destroy();
}
}
Aggregations