CacheCloud CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone、Redis Sentinel、Redis Cluster)自动部署、解决Redis实例碎片化现象、提供完善统计、监控、运维功能、减少运维成本和误操作,提高机器的利用率,提供灵活的伸缩性,提供方便的接入客户端。
改造RedisConnectionFactory
/**
* 根据缓存策略的不同,RedisConnectionFactory不同
* 示例是单机模式。
*
* @return
*/@Beanpublic RedisConnectionFactory redisConnectionFactory() {while(true) {try{ LOCK.tryLock(100, TimeUnit.MILLISECONDS);/**
* 心跳返回的请求为空;
*/Stringresponse = 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实例;
*/Stringinstance = jsonObject.getString("standalone");String[] instanceArr = instance.split(":");if(instanceArr.length !=2) {continue; }//收集上报数据ClientDataCollectReportExecutor.getInstance("http://localhost:5005/cachecloud/client/reportData.json");Stringpassword = jsonObject.getString("password");Stringhost = instanceArr[0];Stringport = instanceArr[1]; JedisConnectionFactory jedisConnectionFactory =newJedisConnectionFactory(); jedisConnectionFactory.setPassword(password); jedisConnectionFactory.setHostName(host); jedisConnectionFactory.setPort(Integer.parseInt(port));returnjedisConnectionFactory; }catch(InterruptedException e) { logger.error("error in build().", e); } }}
改造 jedis-2.9.0
Connection.java
/** * 命令捕获,异常保存 *@paramcmd *@paramargs */publicvoidsendCommand(finalProtocolCommand cmd,finalbyte[]... 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;throwex; }}
JedisClusterCommand.java
privateTrunWithRetries(byte[] key,intattempts,booleantryRandomNode,booleanasking){if(attempts <=0) { JedisClusterMaxRedirectionsException exception =newJedisClusterMaxRedirectionsException("Too many Cluster redirections? key="+ SafeEncoder.encode(key));//收集UsefulDataCollector.collectException(exception,"", System.currentTimeMillis(), ClientExceptionType.REDIS_CLUSTER);throwexception; }}
更新spring-boot-starter-data-redis依赖
<!--Redis-->org.springframework.bootspring-boot-starter-data-redisjedisredis.clientscom.sohu.tvcachecloud-open-client-redis1.0-SNAPSHOTjedisredis.clients<!--上步改造后编译的jar-->com.github.pigpig-cache-cloud-jedis2.9.1
部署服务war
这一步直接参考 cachecloud 的文档即可
网友评论