pom引入jar包
<!-- redis start -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- redis end -->
spring 配置
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<bean id="jedis.shardInfo" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.ip}" />
<constructor-arg index="1" value="${redis.port}" />
<constructor-arg index="2" value="${redis.timeout}"
type="int" />
</bean>
<bean id="jedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<ref bean="jedis.shardInfo" />
</list>
</constructor-arg>
</bean>
<bean id="redisUtil" class="com.redis.RedisUtil">
<property name="shardedJedisPool" ref="jedisPool" />
</bean>
redis.properties配置文件
redis.ip=127.0.0.1
redis.port=6379
redis.timeout=10000
#最大分配的对象数
redis.pool.maxActive=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=50
#当池内没有返回对象时,最大等待时间
redis.pool.maxWait=2000
#当调用borrow Object方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
RedisUtil.java工具类
/**
*Class Description:
*
*/
package com.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* 连接和使用redis资源的工具类
*
* @author
*
*/
public class RedisUtil {
private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
private static int connCount = 0;// 当前连接的个数
/**
* 切片客户端链接池
*/
private ShardedJedisPool shardedJedisPool;
/**
* 获取切片客户端链接池
*
* @return conn
*/
public ShardedJedis getConnFromShardedPool() {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
} catch (Exception e) {
log.error("Get ShardedJedis from ShardedJedisPool failed...", e);
e.printStackTrace();
}
return shardedJedis;
}
/**
* @功能:获取安全切片客户端链接池
*
* @return conn
*/
public ShardedJedis getSafeConnFromShardedPool() {
// 获取不到连接时等待一秒继续尝试,最多尝试20次
int maxTryCount = 20;
ShardedJedis shardedJedis = null;
for (int i = 0; i < maxTryCount; i++) {
try {
shardedJedis = shardedJedisPool.getResource();
break;
} catch (Exception e) {
// 超过最多的尝试次数则退出
if (i == maxTryCount) {
break;
}
log.error(
"==========>>Get ShardedJedis from ShardedJedisPool failed.,第"
+ (i + 1) + "次尝试,最多尝试" + maxTryCount
+ ",如超过最多的尝试次数则退出执行。", e);
e.printStackTrace();
try {
// 出现异常等待1秒钟再次尝试
Thread.sleep(1000);
} catch (Exception e2) {
log.error("", e);
}
}
}
if (shardedJedis != null) {
synchronized (this) {
connCount++;
log.info("Get ShardedJedis from ShardedJedisPool success.... 当前使用连接数="
+ connCount);
}
}
return shardedJedis;
}
/**
* 关闭切片客户端链接
*
* @param conn
*/
public void closeConnShardedJedis(ShardedJedis shardedJedis) {
if (null != shardedJedis) {
try {
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
log.error("Close shardedJedis connection failed.");
e.printStackTrace();
}
}
synchronized (this) {
connCount--;
log.info("==========>>Close ShardedJedis from ShardedJedisPool success.... 当前使用连接数="
+ connCount);
}
}
/**
* 设置数据
*
* @param conn
*/
public boolean setData(String key, String value) {
try {
ShardedJedis jedis = shardedJedisPool.getResource();
jedis.set(key, value);
shardedJedisPool.returnResource(jedis);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 获取数据
*
* @param conn
*/
public String getData(String key) {
String value = null;
try {
ShardedJedis jedis = shardedJedisPool.getResource();
value = jedis.get(key);
shardedJedisPool.returnResource(jedis);
return value;
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
* 获取切片客户端链接池
*
* @return 数据源
*/
public ShardedJedisPool getShardedJedisPool() {
return shardedJedisPool;
}
/**
* 设置切片客户端链接池
*
* @return 数据源
*/
public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
}
}
使用方法
@Autowired
private RedisUtil redisUtil;
public static void main(String[] args) {
ShardedJedis sj = null;
try {
sj = redisUtil.getSafeConnFromShardedPool();
sj.set("testkey","testvalue");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sj != null) {
redisUtil.closeConnShardedJedis(sj);
}
}
}