CacheCloud CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone、Redis Sentinel、Redis Cluster)自动部署、解决Redis实例碎片化现象、提供完善统计、监控、运维功能、减少运维成本和误操作,提高机器的利用率,提供灵活的伸缩性,提供方便的接入客户端。
改造RedisConnectionFactory
/**
* 根据缓存策略的不同,RedisConnectionFactory不同
* 示例是单机模式。
*
* @return
*/
@Bean
public RedisConnectionFactory redisConnectionFactory() {
while (true) {
try {
LOCK.tryLock(100, TimeUnit.MILLISECONDS);
/**
* 心跳返回的请求为空;
*/
String response = HttpUtils.doGet("http://localhost:5005/cache/client/redis/standalone/10000.json?clientVersion=1.0-SNAPSHOT");
if (response == null || response.isEmpty()) {
continue;
}
JSONObject jsonObject = null;
try {
jsonObject = JSONObject.parseObject(response);
} catch (Exception e) {
}
if (jsonObject == null) {
continue;
}
/**
* 从心跳中提取HostAndPort,构造JedisPool实例;
*/
String instance = jsonObject.getString("standalone");
String[] instanceArr = instance.split(":");
if (instanceArr.length != 2) {
continue;
}
//收集上报数据
ClientDataCollectReportExecutor.getInstance("http://localhost:5005/cachecloud/client/reportData.json");
String password = jsonObject.getString("password");
String host = instanceArr[0];
String port = instanceArr[1];
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
jedisConnectionFactory.setPassword(password);
jedisConnectionFactory.setHostName(host);
jedisConnectionFactory.setPort(Integer.parseInt(port));
return jedisConnectionFactory;
} catch (InterruptedException e) {
logger.error("error in build().", e);
}
}
}
改造 jedis-2.9.0
Connection.java
/**
* 命令捕获,异常保存
* @param cmd
* @param args
*/
public void sendCommand(final ProtocolCommand cmd, final byte[]... args) {
try {
//统计开始
UsefulDataModel costModel = UsefulDataModel.getCostModel(threadLocal);
costModel.setCommand(cmd.toString().toLowerCase());
costModel.setStartTime(System.currentTimeMillis());
connect();
Protocol.sendCommand(outputStream, cmd, args);
} catch (JedisConnectionException ex) {
UsefulDataCollector.collectException(ex, getHostPort(), System.currentTimeMillis());
broken = true;
throw ex;
}
}
JedisClusterCommand.java
private T runWithRetries(byte[] key, int attempts, boolean tryRandomNode, boolean asking) {
if (attempts <= 0) {
JedisClusterMaxRedirectionsException exception = new JedisClusterMaxRedirectionsException("Too many Cluster redirections? key=" + SafeEncoder.encode(key));
//收集
UsefulDataCollector.collectException(exception, "", System.currentTimeMillis(), ClientExceptionType.REDIS_CLUSTER);
throw exception;
}
}
更新spring-boot-starter-data-redis依赖
<!--Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<artifactId>jedis</artifactId>
<groupId>redis.clients</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sohu.tv</groupId>
<artifactId>cachecloud-open-client-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>jedis</artifactId>
<groupId>redis.clients</groupId>
</exclusion>
</exclusions>
</dependency>
<!--上步改造后编译的jar-->
<dependency>
<groupId>com.github.pig</groupId>
<artifactId>pig-cache-cloud-jedis</artifactId>
<version>2.9.1</version>
</dependency>
部署服务war
这一步直接参考 cachecloud 的文档即可
---------------------