RedisUtil.java
2.35 KB
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
package com.xiniunet.service.railway.util;
import com.xiniunet.framework.util.SettingUtil;
import com.xiniunet.open.api.client.internal.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* @author
*/
public class RedisUtil {
private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
private static JedisPool jedisPool = null;
/**
* 初始化Redis连接池
*/
static {
try {
String redisHost = SettingUtil.getProperty("config.properties", "redis.host");
logger.error("==============host:{}", redisHost);
if (StringUtils.isEmpty(redisHost)) {
redisHost = "170.16.0.170";
}
String redisPassword = SettingUtil.getProperty("config.properties", "redis.password");
logger.error("==============password:{}", redisPassword);
if (StringUtils.isEmpty(redisPassword)) {
redisHost = "xiniunet";
}
int redisPort = 6379;
try {
redisPort = Integer.parseInt(SettingUtil.getProperty("config.properties", "redis.port"));
logger.error("===========port:{}", redisPort);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(8);
poolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(poolConfig, redisHost, redisPort, 10000, redisPassword);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Jedis实例
* @return
*/
public synchronized static Jedis getJedis() {
try {
if (jedisPool != null) {
logger.error("===========jedisPool:{}", jedisPool);
return jedisPool.getResource();
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}