springboot 接入 cachecloud redis 实践

        最近项目中需要接入  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文件

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

        
            com.sohu.tv
            cachecloud-open-client-basic
            1.0-SNAPSHOT
        

        
            com.sohu.tv
            cachecloud-open-common
            1.0-SNAPSHOT
        
   
   
         
            org.springframework.boot
            spring-boot-starter-data-redis
            
                
                    jedis
                    redis.clients
                
            
        
        
            org.redisson
            redisson
            3.9.0
        

准备配置文件  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> 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);
            }
        }
    }
}
import com.shunwang.buss.dispatchPay.provider.config.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.net.UnknownHostException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;


@Configuration
public class RedisConfig {

    /**
     * JedisPoolConfig 连接池
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig(RedisProperties properties) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 最大空闲数
        jedisPoolConfig.setMaxIdle(20);
        // 连接池的最大数据库连接数
        jedisPoolConfig.setMaxTotal(20);
        // 最大建立连接等待时间
        jedisPoolConfig.setMaxWaitMillis(3000);
        return jedisPoolConfig;
    }

    /**
     * 配置redis的哨兵
     */
    @Bean
    public RedisSentinelConfiguration sentinelConfiguration(RedisProperties properties) {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        // 配置redis的哨兵sentinel
        Set redisNodeSet = properties.getSentinelSet().stream()
                .map(pair -> new RedisNode(pair.getLeft(), Integer.parseInt(pair.getRight())))
                .collect(Collectors.toSet());
        redisSentinelConfiguration.setSentinels(redisNodeSet);
        redisSentinelConfiguration.setMaster(properties.getMasterName());
        return redisSentinelConfiguration;
    }

    /**
     * 配置工厂
     */
    @Bean
    public RedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig, RedisSentinelConfiguration sentinelConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig, jedisPoolConfig);
        return jedisConnectionFactory;
    }


    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
        // 设置值(value)的序列化采用FastJsonRedisSerializer。
        template.setValueSerializer(fastJsonRedisSerializer);
        template.setHashValueSerializer(fastJsonRedisSerializer);
        // 设置键(key)的序列化采用StringRedisSerializer。
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    /**
     * Redisson 配置
     */
    @Bean
    public RedissonClient redissonClient(RedisProperties properties) {
        Config config = new Config();
        List newNodes = properties.getSentinelSet().stream()
                .map(pa -> "redis://" + pa.getLeft() + ":" + pa.getRight()).collect(toList());
        SentinelServersConfig serverConfig = config.useSentinelServers()
                .addSentinelAddress(newNodes.toArray(new String[newNodes.size()]))
                .setMasterName(properties.getMasterName())
                .setReadMode(ReadMode.SLAVE);

        if (StringUtils.isNotBlank(properties.getPassword())){
            serverConfig.setPassword(properties.getPassword());
        }
        return Redisson.create(config);
    }
} 
  

       到这里我们已经在Spring中 生成了RedisTemplate 和  RedissonClient 对象,无论是基本数据结构操作  还是分布式锁  都已经轻松支持了,具体使用就不展开了。

      第一次写博客,写的不周到的地方 还请多多见谅。

 

你可能感兴趣的:(springboot 接入 cachecloud redis 实践)