最近项目中需要接入 Redis CacheCloud, CacheCloud是一个开源的 Redis 运维监控云平台,功能十分强大,支持Redis 实例自动部署、扩容、碎片管理、统计、监控等功能, 特别是支持单机、sentinel 、cluster三种模式的自动部署,搭建redis集群一步到位轻松搞定。
java项目中 接入 CacheCloud redis的方式主要有两种。
第一种就是在 CacheCloud 上创建好redis实例后将对应的IP,端口直接配置以配置形式应用到项目中,优点是通用性好,原有项目改造成本低,不过万一后期CacheCloud上对redis进行管理扩容,那只能手动把每个项目的redis配置都改一遍了。
第二种CacheCloud 上创建好实例后有一个对应的appId,程序调用CacheCloud 平台的rest接口通过 appId获取redis相关配置,将程序中的redis配置 统一交给CacheCloud平台去管理维护,后期管理和扩容及其方便,不过程序改造成本比较高。
现在采用第二种方式接入,工程采用springboot,redis采用哨兵模式,redis客户端主要用spring-data-redis和redisson, 接入流程如下:
添加配置到pom.xml文件
- <!--cachecloud 相关jar包-->
- <dependency>
- <groupId>com.sohu.tv</groupId>
- <artifactId>cachecloud-open-client-redis</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <dependency>
- <groupId>com.sohu.tv</groupId>
- <artifactId>cachecloud-open-client-basic</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <dependency>
- <groupId>com.sohu.tv</groupId>
- <artifactId>cachecloud-open-common</artifactId>
- <version>1.0-SNAPSHOT</version>
- </dependency>
-
- <!--spring redis 和 redisson-->
- <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>org.redisson</groupId>
- <artifactId>redisson</artifactId>
- <version>3.9.0</version>
- </dependency>
准备配置文件 cacheCloudClient.properties,启动项目时 VM参数追加 -Dcachecloud.config= 配置文件路径
- http_conn_timeout = 3000
- http_socket_timeout = 5000
- client_version = 1.0-SNAPSHOT
- domain_url = http://192.168.33.221:8585 #cachecloud实际路径
- redis_cluster_suffix = /cache/client/redis/cluster/%s.json?clientVersion=
- redis_sentinel_suffix = /cache/client/redis/sentinel/%s.json?clientVersion=
- redis_standalone_suffix = /cache/client/redis/standalone/%s.json?clientVersion=
- cachecloud_report_url = /cachecloud/client/reportData.json
基本思路是先通过cachecloud的restapi接口获取并解析redis节点的配置信息,然后就可以按照传统的访问redis的方式进行初始化,获取RedisTemplate对象。
java代码如下:
- import com.alibaba.fastjson.JSONObject;
- import com.sohu.tv.cachecloud.client.basic.heartbeat.ClientStatusEnum;
- import com.sohu.tv.cachecloud.client.basic.util.ConstUtils;
- import com.sohu.tv.cachecloud.client.basic.util.HttpUtils;
- import com.sohu.tv.cachecloud.client.jedis.stat.ClientDataCollectReportExecutor;
- import lombok.Getter;
- import lombok.Setter;
- import org.apache.commons.lang3.tuple.Pair;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
-
- import javax.annotation.PostConstruct;
- import java.util.HashSet;
- import java.util.Random;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- @Component
- public class RedisProperties {
-
- public static Logger logger = LoggerFactory.getLogger(RedisProperties.class);
-
- /**
- * 构建锁
- */
- private static final Lock LOCK = new ReentrantLock();
-
- @Value("${cacheCloud.appId}") //cahcecloud 开通redis实例 应用id
- private Integer appId;
-
- @Getter
- @Setter
- private String masterName;
-
- @Getter
- @Setter
- private Set<Pair<String, String>> sentinelSet = new HashSet<>();
-
- private Boolean clientStatIsOpen=true;
-
- @Getter
- @Setter
- private String password;
-
- private Boolean getConfigSuccess = false;
-
- @PostConstruct
- public void init() {
-
- while (true) {
- try {
- LOCK.tryLock(10, TimeUnit.MILLISECONDS);
- if (!getConfigSuccess) {
- /**
- * http请求返回的结果是空的;
- */
- String response = HttpUtils.doGet(String.format(ConstUtils.REDIS_SENTINEL_URL, appId));
- if (response == null || response.isEmpty()) {
- logger.warn("get response from remote server error, appId: {}, continue...", appId);
- continue;
- }
-
- /**
- * http请求返回的结果是无效的;
- */
- JSONObject jsonObject = null;
- try {
- jsonObject = JSONObject.parseObject(response);
- } catch (Exception e) {
- logger.error("heartbeat error, appId: {}. continue...", appId, e);
- }
- if (jsonObject == null) {
- logger.error("get sentinel info for appId: {} error. continue...", appId);
- continue;
- }
- int status = jsonObject.getIntValue("status");
- String message = jsonObject.getString("message");
-
- /** 检查客户端版本 **/
- if (status == ClientStatusEnum.ERROR.getStatus()) {
- throw new IllegalStateException(message);
- } else if (status == ClientStatusEnum.WARN.getStatus()) {
- logger.warn(message);
- } else {
- logger.info(message);
- }
-
- /**
- * 有效的请求:取出masterName和sentinels;
- */
- masterName = jsonObject.getString("masterName");
- String sentinels = jsonObject.getString("sentinels");
- for (String sentinelStr : sentinels.split(" ")) {
- String[] sentinelArr = sentinelStr.split(":");
- if (sentinelArr.length == 2) {
- sentinelSet.add(Pair.of(sentinelArr[0], sentinelArr[1]));
- }
- }
-
- //收集上报数据
- if (clientStatIsOpen) {
- ClientDataCollectReportExecutor.getInstance();
- }
- password = jsonObject.getString("password");
- getConfigSuccess = true;
- return;
- }
- } catch (Throwable e) {//容错
- logger.error("error in build, appId: {}", appId, e);
- } finally {
- LOCK.unlock();
- }
- try {
- TimeUnit.MILLISECONDS.sleep(200 + new Random().nextInt(1000));//活锁
- } catch (InterruptedException e) {
- logger.error(e.getMessage(), e);
- }
- }
- }
- }
到这里我们已经在Spring中 生成了RedisTemplate 和 RedissonClient 对象,无论是基本数据结构操作 还是分布式锁 都已经轻松支持了,具体使用就不展开了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。