由于公司在不同的業(yè)務(wù)系統(tǒng)場景都有用到Redis肘习,為了減少業(yè)務(wù)之間帶來的相互影響,所以部署了多個Redis集群,JedisHelper就提供了獲取不同業(yè)務(wù)集群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<String, Set<HostAndPort>> map = new HashMap<String, Set<HostAndPort>>();
/**
* 其他Redis實例集合,用于后續(xù)的擴展
*/
private static Map<String, JedisCluster> jedisClusterMap = new ConcurrentHashMap<String, JedisCluster>();
/**
* Redis核心業(yè)務(wù)集群實例
*/
private static JedisCluster coreJedis = null;
/**
* Redis數(shù)據(jù)集群實例
*/
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:分別獲取核心集群與數(shù)據(jù)集群
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:初始化核心業(yè)務(wù)集群實例
Set<HostAndPort> coreNodes = map.get("redis.coreCluster");
coreJedis = getJedisClusterByNodes(coreNodes);
// step4:初始化Redis數(shù)據(jù)集群實例
Set<HostAndPort> dataNodes = map.get("redis.dataCluster");
dataJedis = getJedisClusterByNodes(dataNodes);
}
/**
* 獲取Redis核心業(yè)務(wù)集群客戶端
*
* @return
*/
public static JedisCluster coreCluster() {
return coreJedis;
}
/**
* 獲取Redis數(shù)據(jù)集群客戶端
*
* @return
*/
public static JedisCluster dataCluster() {
return dataJedis;
}
/**
* 根據(jù)指定的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對應(yīng)的集群配置信息不存在");
}
// 如果已經(jīng)實例化,則直接返回
if (jedisClusterMap.get(clusterName) != null) {
return jedisClusterMap.get(clusterName);
}
// 根據(jù)集群名獲取集群IP端口信息
Set<HostAndPort> nodes = map.get(clusterName);
JedisCluster jedisCluster = getJedisClusterByNodes(nodes);
// 實例化后放入jedisClusterMap
jedisClusterMap.put(clusterName, jedisCluster);
return jedisCluster;
}
/**
* 根據(jù)集群節(jié)點nodes以及默認(rèn)的配置信息獲取JedisCluster
*
* @param nodes
* @return
*/
private static JedisCluster getJedisClusterByNodes(Set<HostAndPort> nodes) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(JEDISPOOL_MAXTOTAL);
config.setMaxIdle(JEDISPOOL_MAXAIDLE);
// 根據(jù)集群名獲取集群IP端口信息
JedisCluster jedisCluster = new JedisCluster(nodes, TIMEOUT, 100, config);
return jedisCluster;
}
/**
* 根據(jù)配置文件中的配置信息,獲取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;
}
}
配置信息格式:
# Redis核心業(yè)務(wù)集群
redis.coreCluster=redis.cluster1
# Redis數(shù)據(jù)集群
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