JedisHelper——Redis客户端简单封装

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

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> map = new HashMap>();

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

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

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

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

    }

    /**
     * Redis集群配置信息初始化...
     */
    static {
        // step1:先获取全部的Redis集群配置
        List list = PropertiesUtil.getKeySet("redis.cluster");
        for (String str : list) {
            Set 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 coreNodes = map.get("redis.coreCluster");
        coreJedis = getJedisClusterByNodes(coreNodes);
        // step4:初始化Redis数据集群实例
        Set 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 hostAndPortSet = map.get(clusterName);
        if (hostAndPortSet == null) {
            throw new IllegalArgumentException("clusterName对应的集群配置信息不存在");
        }
        // 如果已经实例化,则直接返回
        if (jedisClusterMap.get(clusterName) != null) {
            return jedisClusterMap.get(clusterName);
        }
        // 根据集群名获取集群IP端口信息
        Set 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 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
     *
     * @param clusterName
     * @return
     */
    private static Set getHostAndPortSetByClusterName(String clusterName) {
        Set nodes = new HashSet();
        // 获取配置信息
        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;
    }

}

配置信息格式:

# 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

你可能感兴趣的:(JedisHelper——Redis客户端简单封装)