概述
spring bean
<bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="testWhileIdle" value="true" />
<!-- <property name="maxActive" value="${redis.pool.maxActive}" /> -->
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- <property name="maxWait" value="${redis.pool.maxWait}" /> -->
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
</bean>
<bean id="jedisPool_xx" class="redis.clients.jedis.JedisPool"
destroy-method="destroy">
<constructor-arg ref="jedisConfig" />
<constructor-arg value="${xx.redis.ip}" />
<constructor-arg type="int" value="${xx.redis.port}" />
<constructor-arg value="100000" type="int" />
<constructor-arg value="${xx.redis.passwd}" />
</bean>
<bean id="jedisPool_yy" class="redis.clients.jedis.JedisPool"
destroy-method="destroy">
<constructor-arg ref="jedisConfig" />
<constructor-arg value="${yy.redis.ip}" />
<constructor-arg type="int" value="${yy.redis.port}" />
<constructor-arg value="100000" type="int" />
<constructor-arg value="${yy.redis.passwd}" />
</bean>
<!-- 将上方配置的jedisPool加入muyilJedisPoolMap,记住Map的Key,代码中需要通过这个Key进行操作不同redis服务器 -->
<bean id="mutilJedisPoolMap" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="xxjedisPool" value-ref="jedisPool_xx" />
<entry key="yyjedisPool" value-ref="jedisPool_yy" />
</map>
</constructor-arg>
</bean>
使用
@Resource(name = "mutilJedisPoolMap")
public Map<String, JedisPool> mutilJedisPoolMap;
/**
* 方法描述:从连接池获取jedis对象
*/
public Jedis getResource(String redisPoolKey){
logger.debug("[MutilJedisUtil:getResource]: get jedis Resource from Pool...");
Jedis jedis = null;//声明jedis对象
// int cycleTimes = 0;//出现异常已经循环获取的次数
try{
jedis = mutilJedisPoolMap.get(redisPoolKey).getResource();//从pool中获取jedis对象
if(jedis == null){
logger.error("[MutilJedisUtil:getResource]:get jedis object failed.message:n");
return null;
}
}catch (JedisConnectionException ex) {
logger.error("[MutilJedisUtil:getResource]:get jedis object failed.message:n"+ExceptionUtil.ExceptionToString(ex));
}
//获取对象如果不为空则返回
if(jedis != null){
logger.debug("[MutilJedisUtil:getResource]: get jedis Resource from Pool success.");
}else{
logger.error("[MutilJedisUtil:getResource]:get jedis object failed.if redis server is runing,please check the configration and Network connection.");
}
return jedis;
}
/**
* 方法描述:使用完毕后将jedis对象归还连接池
*/
public void returnResource(String redisPoolKey,Jedis jedis){
try{
if(jedis != null)
this.mutilJedisPoolMap.get(redisPoolKey).returnResourceObject(jedis);//归还对象至pool
logger.debug("[MutilJedisUtil:returnResource]: return jedis Resource to Pool ...");
}catch(JedisConnectionException ex){
//归还失败,强制销毁该链接
//this.jedisPool.returnResourceObject(jedis);//returnBrokenResource(jedis);
logger.error("[MutilJedisUtil:returnResource]:" + ExceptionUtil.ExceptionToString(ex));
}
}
最后
以上就是美好蜡烛为你收集整理的基于spring的多redis数据源配置的全部内容,希望文章能够帮你解决基于spring的多redis数据源配置所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复