前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JedisHelper——Redis客户端简单封装

JedisHelper——Redis客户端简单封装

作者头像
会跳舞的机器人
发布2018-09-21 16:12:03
1.3K0
发布2018-09-21 16:12:03
举报
文章被收录于专栏:会跳舞的机器人

由于公司在不同的业务系统场景都有用到Redis,为了减少业务之间带来的相互影响,所以部署了多个Redis集群,JedisHelper就提供了获取不同业务集群Redis实例的方法。

代码语言:javascript
复制
package com.ylp.utils;

import com.ylp.common.tools.utils.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: 会跳舞的机器人
 * @date: 16/8/23 下午3:49
 * @description: Jedis客户端
 */
public class JedisHelper {
    private static int JEDISPOOL_MAXTOTAL = 300;
    private static int JEDISPOOL_MAXAIDLE = 60;
    private static int TIMEOUT = 5000;

    private static Logger logger = Logger.getLogger(JedisHelper.class);

    /**
     * 各个Redis集群的IP端口信息集合
     */
    private static Map<String, Set<HostAndPort>> map = new HashMap<String, Set<HostAndPort>>();

    /**
     * 其他Redis实例集合,用于后续的扩展
     */
    private static Map<String, JedisCluster> jedisClusterMap = new ConcurrentHashMap<String, JedisCluster>();

    /**
     * Redis核心业务集群实例
     */
    private static JedisCluster coreJedis = null;

    /**
     * Redis数据集群实例
     */
    private static JedisCluster dataJedis = null;

    /**
     * 禁止实例化
     */
    private JedisHelper() {

    }

    /**
     * Redis集群配置信息初始化...
     */
    static {
        // step1:先获取全部的Redis集群配置
        List<String> list = PropertiesUtil.getKeySet("redis.cluster");
        for (String str : list) {
            Set<HostAndPort> nodes = getHostAndPortSetByClusterName(str);
            map.put(str, nodes);
        }
        // step2:分别获取核心集群与数据集群
        String coreCluseterValue = PropertiesUtil.getProperty("redis.coreCluster");
        String dataClusterValue = PropertiesUtil.getProperty("redis.dataCluster");
        map.put("redis.coreCluster", map.get(coreCluseterValue));
        map.put("redis.dataCluster", map.get(dataClusterValue));
        // step3:初始化核心业务集群实例
        Set<HostAndPort> coreNodes = map.get("redis.coreCluster");
        coreJedis = getJedisClusterByNodes(coreNodes);
        // step4:初始化Redis数据集群实例
        Set<HostAndPort> dataNodes = map.get("redis.dataCluster");
        dataJedis = getJedisClusterByNodes(dataNodes);
    }


    /**
     * 获取Redis核心业务集群客户端
     *
     * @return
     */
    public static JedisCluster coreCluster() {

        return coreJedis;
    }

    /**
     * 获取Redis数据集群客户端
     *
     * @return
     */
    public static JedisCluster dataCluster() {

        return dataJedis;
    }

    /**
     * 根据指定的clusterName获取JedisCluster实例
     *
     * @param clusterName
     * @return
     */
    public synchronized static JedisCluster getCluster(String clusterName) {
        logger.info("clusterName:" + clusterName);
        if (StringUtils.isEmpty(clusterName)) {
            throw new IllegalArgumentException("clusterName不能为空");
        }
        Set<HostAndPort> hostAndPortSet = map.get(clusterName);
        if (hostAndPortSet == null) {
            throw new IllegalArgumentException("clusterName对应的集群配置信息不存在");
        }
        // 如果已经实例化,则直接返回
        if (jedisClusterMap.get(clusterName) != null) {
            return jedisClusterMap.get(clusterName);
        }
        // 根据集群名获取集群IP端口信息
        Set<HostAndPort> nodes = map.get(clusterName);
        JedisCluster jedisCluster = getJedisClusterByNodes(nodes);
        // 实例化后放入jedisClusterMap
        jedisClusterMap.put(clusterName, jedisCluster);

        return jedisCluster;
    }

    /**
     * 根据集群节点nodes以及默认的配置信息获取JedisCluster
     *
     * @param nodes
     * @return
     */
    private static JedisCluster getJedisClusterByNodes(Set<HostAndPort> nodes) {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(JEDISPOOL_MAXTOTAL);
        config.setMaxIdle(JEDISPOOL_MAXAIDLE);
        // 根据集群名获取集群IP端口信息
        JedisCluster jedisCluster = new JedisCluster(nodes, TIMEOUT, 100, config);
        return jedisCluster;
    }

    /**
     * 根据配置文件中的配置信息,获取Set<HostAndPort>
     *
     * @param clusterName
     * @return
     */
    private static Set<HostAndPort> getHostAndPortSetByClusterName(String clusterName) {
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        // 获取配置信息
        String result = PropertiesUtil.getProperty(clusterName);
        if (StringUtils.isEmpty(result)) {
            return nodes;
        }
        // 解析
        String[] arry = result.split(" ");
        String host;
        String port;
        for (String str : arry) {
            host = str.substring(0, str.indexOf(":"));
            port = str.substring(str.indexOf(":") + 1);
            HostAndPort hostAndPort = new HostAndPort(host, Integer.valueOf(port));
            nodes.add(hostAndPort);
        }
        return nodes;
    }

}

配置信息格式:

代码语言:javascript
复制
# Redis核心业务集群
redis.coreCluster=redis.cluster1
# Redis数据集群
redis.dataCluster=redis.cluster2
redis.cluster1=172.16.8.161:7001 172.16.8.162:7002 172.16.8.163:7003 172.16.8.164:7004 172.16.8.165:7005 172.16.8.166:7006
redis.cluster2=172.16.8.161:7001 172.16.8.162:7002 172.16.8.163:7003 172.16.8.164:7004 172.16.8.165:7005 172.16.8.166:7006
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档