redisson整合spring

原文:http://blog.csdn.net/wang_keng/article/details/73549274

首先讲下什么是Redisson:Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。它不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式服务。(摘自redisson的wiki:wiki地址)

redisson提供的api均以面向对象的操作方式,将key-value封装成我们熟悉的集合或者对象,我们可以通过封装的api更方便的操作数据。同时它提供了多个实现了java.util.corrurnent接口的集合类,让我们能在线程安全的环境下操作数据。

其中redis的官网也将它纳入推荐使用的工具中,可见redisson已经逐渐上位。

redisson整合spring

废话不多说,现在开始讲下怎么整合redisson到spring:

首先引入redisson的包:(如果你还在用手动加jar包的方式,那建议你赶紧学一下maven,用上后会让你有种便秘一星期后突然通便的清爽)

[html]view plaincopy

  1. <dependency>
  2. <groupId>org.redisson</groupId>
  3. <artifactId>redisson</artifactId>
  4. <version>2.8.2</version>
  5. </dependency></span>

接下来新建redisson.xml文件:(可参考wiki:redisson配置教程)

[html]view plaincopy

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:redisson="http://redisson.org/schema/redisson"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://redisson.org/schema/redisson
  11. http://redisson.org/schema/redisson/redisson.xsd">
  12. <!--
  13. 单台redis机器配置
  14. <redisson:clientid="redissonClient">
  15. <redisson:single-serveraddress="192.168.2.100:7000"connection-pool-size="30"/>
  16. </redisson:client>
  17. -->
  18. <!--redis集群配置-->
  19. <redisson:clientid="redissonClient">
  20. <redisson:cluster-serversscan-interval="10000"><!--//scan-interval:集群状态扫描间隔时间,单位是毫秒-->
  21. <redisson:node-addressvalue="192.168.2.100:7000"></redisson:node-address>
  22. <redisson:node-addressvalue="192.168.2.100:7001"></redisson:node-address>
  23. <redisson:node-addressvalue="192.168.2.100:7002"></redisson:node-address>
  24. <redisson:node-addressvalue="192.168.2.100:7003"></redisson:node-address>
  25. <redisson:node-addressvalue="192.168.2.100:7004"></redisson:node-address>
  26. <redisson:node-addressvalue="192.168.2.100:7005"></redisson:node-address>
  27. </redisson:cluster-servers>
  28. </redisson:client>
  29. </beans>


然后在Application.xml中引入redisson.xml:<import resource="classpath:/spring/redisson.xml" /> (路径记得自己改哦~)

至此我们redisson的环境就配置好了。接下来就是怎么操作它来操作redis:

首先新建一个工具类RedissonUtils:

[java]view plaincopy

packagecom.basic.common.utils.redis;

  1. importjava.util.Map;
  2. importorg.redisson.api.RAtomicLong;
  3. importorg.redisson.api.RBucket;
  4. importorg.redisson.api.RCountDownLatch;
  5. importorg.redisson.api.RDeque;
  6. importorg.redisson.api.RList;
  7. importorg.redisson.api.RLock;
  8. importorg.redisson.api.RMap;
  9. importorg.redisson.api.RQueue;
  10. importorg.redisson.api.RSet;
  11. importorg.redisson.api.RSortedSet;
  12. importorg.redisson.api.RTopic;
  13. importorg.redisson.api.RedissonClient;
  14. importorg.springframework.stereotype.Service;
  15. importcom.basic.common.utils.generator.CollectionObjectConvert;
  16. @Service
  17. publicclassRedissonUtils{
  18. /**
  19. *获取字符串对象
  20. *@paramredisson
  21. *@paramt
  22. *@paramobjectName
  23. *@return
  24. */
  25. publicstatic<T>RBucket<T>getRBucket(RedissonClientredissonClient,StringobjectName){
  26. RBucket<T>bucket=redissonClient.getBucket(objectName);
  27. returnbucket;
  28. }
  29. /**
  30. *获取Map对象
  31. *@paramredisson
  32. *@paramobjectName
  33. *@return
  34. */
  35. publicstatic<K,V>RMap<K,V>getRMap(RedissonClientredissonClient,StringobjectName){
  36. RMap<K,V>map=redissonClient.getMap(objectName);
  37. returnmap;
  38. }
  39. /**
  40. *获取有序集合
  41. *@paramredisson
  42. *@paramobjectName
  43. *@return
  44. */
  45. publicstatic<V>RSortedSet<V>getRSortedSet(RedissonClientredissonClient,StringobjectName){
  46. RSortedSet<V>sortedSet=redissonClient.getSortedSet(objectName);
  47. returnsortedSet;
  48. }
  49. /**
  50. *获取集合
  51. *@paramredisson
  52. *@paramobjectName
  53. *@return
  54. */
  55. publicstatic<V>RSet<V>getRSet(RedissonClientredissonClient,StringobjectName){
  56. RSet<V>rSet=redissonClient.getSet(objectName);
  57. returnrSet;
  58. }
  59. /**
  60. *获取列表
  61. *@paramredisson
  62. *@paramobjectName
  63. *@return
  64. */
  65. publicstatic<V>RList<V>getRList(RedissonClientredissonClient,StringobjectName){
  66. RList<V>rList=redissonClient.getList(objectName);
  67. returnrList;
  68. }
  69. /**
  70. *获取队列
  71. *@paramredisson
  72. *@paramobjectName
  73. *@return
  74. */
  75. publicstatic<V>RQueue<V>getRQueue(RedissonClientredissonClient,StringobjectName){
  76. RQueue<V>rQueue=redissonClient.getQueue(objectName);
  77. returnrQueue;
  78. }
  79. /**
  80. *获取双端队列
  81. *@paramredisson
  82. *@paramobjectName
  83. *@return
  84. */
  85. publicstatic<V>RDeque<V>getRDeque(RedissonClientredissonClient,StringobjectName){
  86. RDeque<V>rDeque=redissonClient.getDeque(objectName);
  87. returnrDeque;
  88. }
  89. /**
  90. *此方法不可用在Redisson1.2中
  91. *在1.2.2版本中可用
  92. *@paramredisson
  93. *@paramobjectName
  94. *@return
  95. */
  96. /**
  97. public<V>RBlockingQueue<V>getRBlockingQueue(Redissonredisson,StringobjectName){
  98. RBlockingQueuerb=redisson.getBlockingQueue(objectName);
  99. returnrb;
  100. }*/
  101. /**
  102. *获取锁
  103. *@paramredisson
  104. *@paramobjectName
  105. *@return
  106. */
  107. publicstaticRLockgetRLock(RedissonClientredissonClient,StringobjectName){
  108. RLockrLock=redissonClient.getLock(objectName);
  109. returnrLock;
  110. }
  111. /**
  112. *获取原子数
  113. *@paramredisson
  114. *@paramobjectName
  115. *@return
  116. */
  117. publicstaticRAtomicLonggetRAtomicLong(RedissonClientredissonClient,StringobjectName){
  118. RAtomicLongrAtomicLong=redissonClient.getAtomicLong(objectName);
  119. returnrAtomicLong;
  120. }
  121. /**
  122. *获取记数锁
  123. *@paramredisson
  124. *@paramobjectName
  125. *@return
  126. */
  127. publicstaticRCountDownLatchgetRCountDownLatch(RedissonClientredissonClient,StringobjectName){
  128. RCountDownLatchrCountDownLatch=redissonClient.getCountDownLatch(objectName);
  129. returnrCountDownLatch;
  130. }
  131. /**
  132. *获取消息的Topic
  133. *@paramredisson
  134. *@paramobjectName
  135. *@return
  136. */
  137. publicstatic<M>RTopic<M>getRTopic(RedissonClientredissonClient,StringobjectName){
  138. RTopic<M>rTopic=redissonClient.getTopic(objectName);
  139. returnrTopic;
  140. }
  141. }


然后我们需要在调用RedissonUtils的类中依赖进RedissonClient

@Autowired

private RedissonClient redisson;

只后便可以对redis进行操作啦

RMap<String, Object> map = RedissonUtils.getRMap(redisson,key);

map.put("key","value"); 调用到这个语句的同时就已经对redis中的数据进行了修改。

注意!redis最好是3.0以上的,官方发布的版本,不然会出现莫名其妙的错误

相关推荐