我是靠谱客的博主 美好蜡烛,这篇文章主要介绍基于spring的多redis数据源配置,现在分享给大家,希望可以做个参考。

spring bean

复制代码
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
<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>

使用

复制代码
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
@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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部