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


    改造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(http://www.my516.com);
    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 的文档即可
    ---------------------

  • 相关阅读:
    Eclipse和MyEclipse常出现的错误总结
    JS中的showModelDialog详解和实例
    form中的action,submit="return save()"以及ajax提交方法
    清除上传文件input标签中type="file"的value值
    上传文件时并顺便将文件压缩时出现文件名乱码以及文件内容乱码解决方案
    python 知识
    centos 安装docker
    docker 配置国内镜像
    git 操作命令
    react build dist发布nginx时的配置
  • 原文地址:https://www.cnblogs.com/ly570/p/11167528.html
Copyright © 2020-2023  润新知