java端在使用jedispool 连接redis的时候,在高并发的时候经常死锁,或报连接异常,JedisConnectionException,或者getResource 异常等各种问题
在使用jedispool 的时候一定要注意两点
1。 在获取 jedisPool和jedis的时候加上线程同步,保证不要创建过多的jedispool 和 jedis
2。 用完Jedis实例后需要返还给JedisPool
整理了一下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
166
167
168
169
170
171
172
173
|
package com.caspar.util;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* Redis 工具类
* @author caspar
*
*/
public class RedisUtil {
protected static ReentrantLock lockPool = new ReentrantLock();
protected static ReentrantLock lockJedis = new ReentrantLock();
protected static Logger logger = Logger.getLogger(RedisUtil. class );
//Redis服务器IP
private static String ADDR_ARRAY = FileUtil.getPropertyValue( "/properties/redis.properties" , "server" );
//Redis的端口号
private static int PORT = FileUtil.getPropertyValueInt( "/properties/redis.properties" , "port" );
//访问密码
// private static String AUTH = FileUtil.getPropertyValue("/properties/redis.properties", "auth");
//可用连接实例的最大数目,默认值为8;
//如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
private static int MAX_ACTIVE = FileUtil.getPropertyValueInt( "/properties/redis.properties" , "max_active" );;
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
private static int MAX_IDLE = FileUtil.getPropertyValueInt( "/properties/redis.properties" , "max_idle" );;
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
private static int MAX_WAIT = FileUtil.getPropertyValueInt( "/properties/redis.properties" , "max_wait" );;
//超时时间
private static int TIMEOUT = FileUtil.getPropertyValueInt( "/properties/redis.properties" , "timeout" );;
//在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
private static boolean TEST_ON_BORROW = FileUtil.getPropertyValueBoolean( "/properties/redis.properties" , "test_on_borrow" );;
private static JedisPool jedisPool = null ;
/**
* redis过期时间,以秒为单位
*/
public final static int EXRP_HOUR = 60 * 60 ; //一小时
public final static int EXRP_DAY = 60 * 60 * 24 ; //一天
public final static int EXRP_MONTH = 60 * 60 * 24 * 30 ; //一个月
/**
* 初始化Redis连接池
*/
private static void initialPool(){
try {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR_ARRAY.split( "," )[ 0 ], PORT, TIMEOUT);
} catch (Exception e) {
logger.error( "First create JedisPool error : " +e);
try {
//如果第一个IP异常,则访问第二个IP
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool = new JedisPool(config, ADDR_ARRAY.split( "," )[ 1 ], PORT, TIMEOUT);
} catch (Exception e2){
logger.error( "Second create JedisPool error : " +e2);
}
}
}
/**
* 在多线程环境同步初始化
*/
private static void poolInit() {
//断言 ,当前锁是否已经锁住,如果锁住了,就啥也不干,没锁的话就执行下面步骤
assert ! lockPool.isHeldByCurrentThread();
lockPool.lock();
try {
if (jedisPool == null ) {
initialPool();
}
} catch (Exception e){
e.printStackTrace();
} finally {
lockPool.unlock();
}
}
public static Jedis getJedis() {
//断言 ,当前锁是否已经锁住,如果锁住了,就啥也不干,没锁的话就执行下面步骤
assert ! lockJedis.isHeldByCurrentThread();
lockJedis.lock();
if (jedisPool == null ) {
poolInit();
}
Jedis jedis = null ;
try {
if (jedisPool != null ) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
logger.error( "Get jedis error : " +e);
} finally {
returnResource(jedis);
lockJedis.unlock();
}
return jedis;
}
/**
* 释放jedis资源
* @param jedis
*/
public static void returnResource( final Jedis jedis) {
if (jedis != null && jedisPool != null ) {
jedisPool.returnResource(jedis);
}
}
/**
* 设置 String
* @param key
* @param value
*/
public synchronized static void setString(String key ,String value){
try {
value = StringUtil.isEmpty(value) ? "" : value;
getJedis().set(key,value);
} catch (Exception e) {
logger.error( "Set key error : " +e);
}
}
/**
* 设置 过期时间
* @param key
* @param seconds 以秒为单位
* @param value
*/
public synchronized static void setString(String key , int seconds,String value){
try {
value = StringUtil.isEmpty(value) ? "" : value;
getJedis().setex(key, seconds, value);
} catch (Exception e) {
logger.error( "Set keyex error : " +e);
}
}
/**
* 获取String值
* @param key
* @return value
*/
public synchronized static String getString(String key){
if (getJedis() == null || !getJedis().exists(key)){
return null ;
}
return getJedis().get(key);
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/tuposky/article/details/45340183
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-26 62
-
2025-05-25 68
-
2025-06-04 34
-
ASP.NET MVC @Helper辅助方法和@functons自定义函数的使用方法
2025-05-29 27 -
比较全面的C 、Java、JavaScript中的正则表达式详解
2025-05-29 24
热门评论