JedisHelper——Redis客户端简单封装

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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;
}

}

配置信息格式:

1
2
3
4
5
6
# 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

如果您觉得有帮助到您,不妨考虑请作者喝杯咖啡鼓励一下。