原文: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的包:(如果你还在用手动加jar包的方式,那建议你赶紧学一下maven,用上后会让你有种便秘一星期后突然通便的清爽)
[html]view plaincopy
- <dependency>
- <groupId>org.redisson</groupId>
- <artifactId>redisson</artifactId>
- <version>2.8.2</version>
- </dependency></span>
接下来新建redisson.xml文件:(可参考wiki:redisson配置教程)
[html]view plaincopy
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:redisson="http://redisson.org/schema/redisson"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://redisson.org/schema/redisson
- http://redisson.org/schema/redisson/redisson.xsd">
- <!--
- 单台redis机器配置
- <redisson:clientid="redissonClient">
- <redisson:single-serveraddress="192.168.2.100:7000"connection-pool-size="30"/>
- </redisson:client>
- -->
- <!--redis集群配置-->
- <redisson:clientid="redissonClient">
- <redisson:cluster-serversscan-interval="10000"><!--//scan-interval:集群状态扫描间隔时间,单位是毫秒-->
- <redisson:node-addressvalue="192.168.2.100:7000"></redisson:node-address>
- <redisson:node-addressvalue="192.168.2.100:7001"></redisson:node-address>
- <redisson:node-addressvalue="192.168.2.100:7002"></redisson:node-address>
- <redisson:node-addressvalue="192.168.2.100:7003"></redisson:node-address>
- <redisson:node-addressvalue="192.168.2.100:7004"></redisson:node-address>
- <redisson:node-addressvalue="192.168.2.100:7005"></redisson:node-address>
- </redisson:cluster-servers>
- </redisson:client>
- </beans>
然后在Application.xml中引入redisson.xml:<import resource="classpath:/spring/redisson.xml" /> (路径记得自己改哦~)
至此我们redisson的环境就配置好了。接下来就是怎么操作它来操作redis:
首先新建一个工具类RedissonUtils:
[java]view plaincopy
packagecom.basic.common.utils.redis;
- importjava.util.Map;
- importorg.redisson.api.RAtomicLong;
- importorg.redisson.api.RBucket;
- importorg.redisson.api.RCountDownLatch;
- importorg.redisson.api.RDeque;
- importorg.redisson.api.RList;
- importorg.redisson.api.RLock;
- importorg.redisson.api.RMap;
- importorg.redisson.api.RQueue;
- importorg.redisson.api.RSet;
- importorg.redisson.api.RSortedSet;
- importorg.redisson.api.RTopic;
- importorg.redisson.api.RedissonClient;
- importorg.springframework.stereotype.Service;
- importcom.basic.common.utils.generator.CollectionObjectConvert;
- @Service
- publicclassRedissonUtils{
- /**
- *获取字符串对象
- *@paramredisson
- *@paramt
- *@paramobjectName
- *@return
- */
- publicstatic<T>RBucket<T>getRBucket(RedissonClientredissonClient,StringobjectName){
- RBucket<T>bucket=redissonClient.getBucket(objectName);
- returnbucket;
- }
- /**
- *获取Map对象
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<K,V>RMap<K,V>getRMap(RedissonClientredissonClient,StringobjectName){
- RMap<K,V>map=redissonClient.getMap(objectName);
- returnmap;
- }
- /**
- *获取有序集合
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<V>RSortedSet<V>getRSortedSet(RedissonClientredissonClient,StringobjectName){
- RSortedSet<V>sortedSet=redissonClient.getSortedSet(objectName);
- returnsortedSet;
- }
- /**
- *获取集合
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<V>RSet<V>getRSet(RedissonClientredissonClient,StringobjectName){
- RSet<V>rSet=redissonClient.getSet(objectName);
- returnrSet;
- }
- /**
- *获取列表
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<V>RList<V>getRList(RedissonClientredissonClient,StringobjectName){
- RList<V>rList=redissonClient.getList(objectName);
- returnrList;
- }
- /**
- *获取队列
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<V>RQueue<V>getRQueue(RedissonClientredissonClient,StringobjectName){
- RQueue<V>rQueue=redissonClient.getQueue(objectName);
- returnrQueue;
- }
- /**
- *获取双端队列
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<V>RDeque<V>getRDeque(RedissonClientredissonClient,StringobjectName){
- RDeque<V>rDeque=redissonClient.getDeque(objectName);
- returnrDeque;
- }
- /**
- *此方法不可用在Redisson1.2中
- *在1.2.2版本中可用
- *@paramredisson
- *@paramobjectName
- *@return
- */
- /**
- public<V>RBlockingQueue<V>getRBlockingQueue(Redissonredisson,StringobjectName){
- RBlockingQueuerb=redisson.getBlockingQueue(objectName);
- returnrb;
- }*/
- /**
- *获取锁
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstaticRLockgetRLock(RedissonClientredissonClient,StringobjectName){
- RLockrLock=redissonClient.getLock(objectName);
- returnrLock;
- }
- /**
- *获取原子数
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstaticRAtomicLonggetRAtomicLong(RedissonClientredissonClient,StringobjectName){
- RAtomicLongrAtomicLong=redissonClient.getAtomicLong(objectName);
- returnrAtomicLong;
- }
- /**
- *获取记数锁
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstaticRCountDownLatchgetRCountDownLatch(RedissonClientredissonClient,StringobjectName){
- RCountDownLatchrCountDownLatch=redissonClient.getCountDownLatch(objectName);
- returnrCountDownLatch;
- }
- /**
- *获取消息的Topic
- *@paramredisson
- *@paramobjectName
- *@return
- */
- publicstatic<M>RTopic<M>getRTopic(RedissonClientredissonClient,StringobjectName){
- RTopic<M>rTopic=redissonClient.getTopic(objectName);
- returnrTopic;
- }
- }
然后我们需要在调用RedissonUtils的类中依赖进RedissonClient
@Autowired
private RedissonClient redisson;
只后便可以对redis进行操作啦
RMap<String, Object> map = RedissonUtils.getRMap(redisson,key);
map.put("key","value"); 调用到这个语句的同时就已经对redis中的数据进行了修改。
注意!redis最好是3.0以上的,官方发布的版本,不然会出现莫名其妙的错误