Redis cache access monitoring, operation and maintenance platform CacheCloud

CacheCloud CacheCloud Redis provides a cloud management platform: to achieve a variety of types (Redis Standalone, Redis Sentinel, Redis Cluster) automatically deploy to address the phenomenon of fragmentation Redis instance, to provide comprehensive statistics, monitoring, operation and maintenance function, reduce operation and maintenance costs and misuse improve the utilization of the machine, provides flexible scalability, provides convenient access client.

image

image

改造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);
        }

    }
}

Transformation 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;
    }
}

Update spring-boot-starter-data-redis-dependent

 <!--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>

Deployment Services war

This step is a direct reference to the documentation cachecloud

Guess you like

Origin blog.csdn.net/weixin_43770982/article/details/94736353