Redis缓存接入监控、运维平台CacheCloud

CacheCloud CacheCloud提供一个Redis云管理平台:实现多种类型(Redis Standalone、Redis Sentinel、Redis Cluster)自动部署、解决Redis实例碎片化现象、提供完善统计、监控、运维功能、减少运维成本和误操作,提高机器的利用率,提供灵活的伸缩性,提供方便的接入客户端。

Redis缓存接入监控、运维平台CacheCloud_第1张图片

Redis缓存接入监控、运维平台CacheCloud_第2张图片

改造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依赖

 

    org.springframework.boot
    spring-boot-starter-data-redis
    
        
            jedis
            redis.clients
        
    


    com.sohu.tv
    cachecloud-open-client-redis
    1.0-SNAPSHOT
    
        
            jedis
            redis.clients
        
    



    com.github.pig
    pig-cache-cloud-jedis
    2.9.1

部署服务war

这一步直接参考 cachecloud 的文档即可

你可能感兴趣的:(java)