Redis客户端redisson实战

    

redis 学习问题总结

http://aperise.iteye.com/blog/2310639

ehcache memcached redis 缓存技术总结

http://aperise.iteye.com/blog/2296219

redis-stat 离线安装

http://aperise.iteye.com/blog/2310254

redis  cluster 非ruby方式启动

http://aperise.iteye.com/blog/2310254

redis-sentinel安装部署

http://aperise.iteye.com/blog/2342693

spring-data-redis使用

 http://aperise.iteye.com/blog/2342615

redis客户端redisson实战

http://blog.csdn.net/zilong_zilong/article/details/78252037

redisson-2.10.4源代码分析

http://blog.csdn.net/zilong_zilong/article/details/78609423

tcmalloc jemalloc libc选择

http://blog.csdn.net/u010994304/article/details/49906819

 

 redis客户端redisson实战

1.前言

    Redisson是一个基于java编程框架netty进行扩展了的redis,想了解Redisson源码首先你必须熟悉netty网络编程框架html

    Redisson目前分开源版本商业版(Redisson PRO),因此选择的时候请谨慎(Map)和集(Set)数据分片功能仅限于Redisson PRO版本才有,另外Redis部署工具和集群管理工具功能仅限于Redisson PRO版本才有。关于商业版和开源版本的区别和商业版收费标准详见官网(https://redisson.pro/)  java

2.maven项目集成redisson

    根据本身JDK环境,JDK 1.8+以上请选择3.5.4版本,JDK 1.6+以上请选择2.10.4版本 node

<!-- JDK 1.8+ compatible -->  
<dependency>  
   <groupId>org.redisson</groupId>  
   <artifactId>redisson</artifactId>  
   <version>3.5.4</version>  
</dependency>    
  
<!-- JDK 1.6+ compatible -->  
<dependency>  
   <groupId>org.redisson</groupId>  
   <artifactId>redisson</artifactId>  
   <version>2.10.4</version>  
</dependency>

3.利用redisson API操做各类redis部署的服务端

    redis的部署方式有单节点部署、哨兵方式部署、集群方式部署3种方式,这3中方式都使用的是原生的redis;linux

    基于单节点部署为了保证数据的备份,通常会添加一个节点做为slave来备份master节点上的数据,这里就衍生出了主从部署方式;git

    云服务商像阿里云、微软云和亚马逊云都基于原生redis作了高可用部署,为了能链接云服务商的redis服务,这里redisson也提供了API操做方式。github

    下面以向redis服务端进行操做set key value为例进行说明如何使用redisson的APIredis

3.1 单节点部署方式(standalone)

(1)纯java操做spring

//建立配置  
Config config = new Config();  
  
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec  
//以前使用的spring-data-redis,用的客户端jedis,编码为org.springframework.data.redis.serializer.StringRedisSerializer  
//改用redisson后为了之间数据能兼容,这里修改编码为org.redisson.client.codec.StringCodec  
config.setCodec(new org.redisson.client.codec.StringCodec());  
  
//指定使用单节点部署方式  
config.useSingleServer().setAddress("redis://127.0.0.1:6379");  
  
//config.setPassword("password")//设置密码  
config.setConnectionPoolSize(500)//设置对于master节点的链接池中链接数最大为500  
config.setIdleConnectionTimeout(10000)//若是当前链接池里的链接数量超过了最小空闲链接数,而同时有链接空闲时间超过了该数值,那么这些链接将会自动被关闭,并从链接池里去掉。时间单位是毫秒。  
config.setConnectTimeout(30000)//同任何节点创建链接时的等待超时。时间单位是毫秒。  
config.setTimeout(3000)//等待节点回复命令的时间。该时间从命令发送成功时开始计时。  
config.setPingTimeout(30000)  
config.setReconnectionTimeout(3000)//当与某个节点的链接断开时,等待与其从新创建链接的时间间隔。时间单位是毫秒。  
  
//建立客户端(发现建立RedissonClient很是耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key不存在不要紧  
RBucket<String> keyObject = redisson.getBucket("key");  
//若是key存在,就设置key的值为新值value  
//若是key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown();

(2)spring集成操做
pom.xml编程

<!--redisson-->  
<dependency>  
    <groupId>org.redisson</groupId>  
    <artifactId>redisson</artifactId>  
    <version>2.10.4</version>  
</dependency>  
<!--spring-->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-core</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-beans</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>

spring-redisson.xmljson

 

<beans xmlns="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">    
    <bean id="stringCodec" class="org.redisson.client.codec.StringCodec"></bean>  
    <redisson:client id="standalone"   
                     name="aliasName1,aliasName2"  
                     codec-ref="stringCodec">    
        <redisson:single-server address="redis://127.0.0.1:6379"  
                                connection-pool-size="500"  
                                idle-connection-timeout="10000"  
                                connect-timeout="10000"  
                                timeout="3000"  
                                ping-timeout="30000"  
                                reconnection-timeout="30000"  
                                database="0"/>    
    </redisson:client>    
</beans>

 SpringRedissonTest.java

import org.redisson.api.RBucket;  
import org.redisson.api.RedissonClient;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class SpringRedissonTest {  
    public static void main(String[] args) {  
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-redisson.xml");  
        RedissonClient redisson = (RedissonClient) applicationContext.getBean("standalone");  
        // 首先获取redis中的key-value对象,key不存在不要紧  
        RBucket<String> keyObject = redisson.getBucket("key");  
        // 若是key存在,就设置key的值为新值value  
        // 若是key不存在,就设置key的值为value  
        keyObject.set("value");  
    }  
}

3.2 哨兵部署方式(sentinel)

(1)纯java操做

//建立配置  
Config config = new Config();  
  
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec  
//以前使用的spring-data-redis,用的客户端jedis,编码为org.springframework.data.redis.serializer.StringRedisSerializer  
//改用redisson后为了之间数据能兼容,这里修改编码为org.redisson.client.codec.StringCodec  
config.setCodec(new org.redisson.client.codec.StringCodec());  
  
//指定使用哨兵部署方式  
config.useSentinelServers()  
<span style="white-space:pre">  </span>//设置sentinel.conf配置里的sentinel别名  
<span style="white-space:pre">  </span>//好比sentinel.conf里配置为sentinel monitor my-sentinel-name 127.0.0.1 6379 2,那么这里就配置my-sentinel-name  
    .setMasterName("my-sentinel-name")  
    //这里设置sentinel节点的服务IP和端口,sentinel是采用Paxos拜占庭协议,通常sentinel至少3个节点  
    //记住这里不是配置redis节点的服务端口和IP,sentinel会本身把请求转发给后面monitor的redis节点  
    .addSentinelAddress("redis://127.0.0.1:26379")  
    .addSentinelAddress("redis://127.0.0.1:26389")  
    .addSentinelAddress("redis://127.0.0.1:26399");  
      
//config.setPassword("password")//设置密码  
config.setMasterConnectionPoolSize(500)//设置对于master节点的链接池中链接数最大为500  
config.setSlaveConnectionPoolSize(500)//设置对于slave节点的链接池中链接数最大为500  
config.setIdleConnectionTimeout(10000)//若是当前链接池里的链接数量超过了最小空闲链接数,而同时有链接空闲时间超过了该数值,那么这些链接将会自动被关闭,并从链接池里去掉。时间单位是毫秒。  
config.setConnectTimeout(30000)//同任何节点创建链接时的等待超时。时间单位是毫秒。  
config.setTimeout(3000)//等待节点回复命令的时间。该时间从命令发送成功时开始计时。  
config.setPingTimeout(30000)  
config.setReconnectionTimeout(3000)//当与某个节点的链接断开时,等待与其从新创建链接的时间间隔。时间单位是毫秒。  
  
//建立客户端(发现这一很是耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
//首先获取redis中的key-value对象,key不存在不要紧  
RBucket<String> keyObject = redisson.getBucket("key");  
//若是key存在,就设置key的值为新值value  
//若是key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown();

(2)spring集成操做
 pom.xml

<!--redisson-->  
<dependency>  
    <groupId>org.redisson</groupId>  
    <artifactId>redisson</artifactId>  
    <version>2.10.4</version>  
</dependency>  
<!--spring-->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-core</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-beans</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>

spring-redisson.xml

<beans xmlns="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">  
<bean id="stringCodec" class="org.redisson.client.codec.StringCodec"></bean>  
<redisson:client id="sentinel" codec-ref="stringCodec">  
    <redisson:sentinel-servers master-name="my-sentinel-name"  
                                slaveConnectionPoolSize="500"  
                                masterConnectionPoolSize="500"  
                                idle-connection-timeout="10000"  
                                connect-timeout="10000"  
                                timeout="3000"  
                                ping-timeout="1000"  
                                reconnection-timeout="3000"  
                                database="0">  
        <redisson:sentinel-address value="redis://127.0.0.1:26379" />  
        <redisson:sentinel-address value="redis://127.0.0.1:26389" />  
        <redisson:sentinel-address value="redis://127.0.0.1:26399" />  
    </redisson:sentinel-servers>  
</redisson:client>  
</beans>

SpringRedissonTest.java

import org.redisson.api.RBucket;  
import org.redisson.api.RedissonClient;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class SpringRedissonTest {  
    public static void main(String[] args) {  
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-redisson.xml");  
        RedissonClient redisson = (RedissonClient) applicationContext.getBean("sentinel");  
        // 首先获取redis中的key-value对象,key不存在不要紧  
        RBucket<String> keyObject = redisson.getBucket("key");  
        // 若是key存在,就设置key的值为新值value  
        // 若是key不存在,就设置key的值为value  
        keyObject.set("value");  
    }  
}

3.3 集群方式(cluster)

(1)纯java操做

//建立配置  
Config config = new Config();  
  
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec  
//以前使用的spring-data-redis,用的客户端jedis,编码为org.springframework.data.redis.serializer.StringRedisSerializer  
//改用redisson后为了之间数据能兼容,这里修改编码为org.redisson.client.codec.StringCodec  
config.setCodec(new org.redisson.client.codec.StringCodec());  
  
//指定使用集群部署方式  
config.useClusterServers()  
<span style="white-space:pre">  </span>// 集群状态扫描间隔时间,单位是毫秒  
    .setScanInterval(2000)   
    //cluster方式至少6个节点(3主3从,3主作sharding,3从用来保证主宕机后能够高可用)  
    .addNodeAddress("redis://127.0.0.1:6379" )  
    .addNodeAddress("redis://127.0.0.1:6380")  
    .addNodeAddress("redis://127.0.0.1:6381")  
    .addNodeAddress("redis://127.0.0.1:6382")  
    .addNodeAddress("redis://127.0.0.1:6383")  
    .addNodeAddress("redis://127.0.0.1:6384");  
      
//config.setPassword("password")//设置密码  
config.setMasterConnectionPoolSize(500)//设置对于master节点的链接池中链接数最大为500  
config.setSlaveConnectionPoolSize(500)//设置对于slave节点的链接池中链接数最大为500  
config.setIdleConnectionTimeout(10000)//若是当前链接池里的链接数量超过了最小空闲链接数,而同时有链接空闲时间超过了该数值,那么这些链接将会自动被关闭,并从链接池里去掉。时间单位是毫秒。  
config.setConnectTimeout(30000)//同任何节点创建链接时的等待超时。时间单位是毫秒。  
config.setTimeout(3000)//等待节点回复命令的时间。该时间从命令发送成功时开始计时。  
config.setPingTimeout(30000)  
config.setReconnectionTimeout(3000)//当与某个节点的链接断开时,等待与其从新创建链接的时间间隔。时间单位是毫秒。  
  
//建立客户端(发现建立RedissonClient很是耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key不存在不要紧  
RBucket<String> keyObject = redisson.getBucket("key");  
//若是key存在,就设置key的值为新值value  
//若是key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown();

(2)spring集成操做
pom.xml

<!--redisson-->  
<dependency>  
    <groupId>org.redisson</groupId>  
    <artifactId>redisson</artifactId>  
    <version>2.10.4</version>  
</dependency>  
<!--spring-->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-core</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-beans</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>

 spring-redisson.xml

<beans xmlns="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">  
    <bean id="stringCodec" class="org.redisson.client.codec.StringCodec"></bean>  
	<redisson:client id="cluster" codec-ref="stringCodec">  
	<redisson:cluster-servers slaveConnectionPoolSize="500"   
	masterConnectionPoolSize="500"   
	idle-connection-timeout="10000"    
	connect-timeout="10000"    
	timeout="3000"    
	ping-timeout="1000"    
	reconnection-timeout="3000"    
	database="0">    
	<redisson:node-address value="redis://127.0.0.1:6379" />  
	<redisson:node-address value="redis://127.0.0.1:6380" />  
	<redisson:node-address value="redis://127.0.0.1:6381" />  
	<redisson:node-address value="redis://127.0.0.1:6382" />  
	<redisson:node-address value="redis://127.0.0.1:6383" />  
	<redisson:node-address value="redis://127.0.0.1:6384" />  
	</redisson:cluster-servers>  
</redisson:client>  
</beans>

SpringRedissonTest.java

import org.redisson.api.RBucket;  
import org.redisson.api.RedissonClient;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class SpringRedissonTest {  
    public static void main(String[] args) {  
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-redisson.xml");  
        RedissonClient redisson = (RedissonClient) applicationContext.getBean("cluster");  
        // 首先获取redis中的key-value对象,key不存在不要紧  
        RBucket<String> keyObject = redisson.getBucket("key");  
        // 若是key存在,就设置key的值为新值value  
        // 若是key不存在,就设置key的值为value  
        keyObject.set("value");  
    }  
}

 3.4 主从部署方式(master/slave)

(1)纯java操做

//建立配置  
Config config = new Config();  
  
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec  
//以前使用的spring-data-redis,用的客户端jedis,编码为org.springframework.data.redis.serializer.StringRedisSerializer  
//改用redisson后为了之间数据能兼容,这里修改编码为org.redisson.client.codec.StringCodec  
config.setCodec(new org.redisson.client.codec.StringCodec());  
  
//指定使用主从部署方式  
config.useMasterSlaveServers()  
    //设置redis主节点  
    .setMasterAddress("redis://127.0.0.1:6379")  
    //设置redis从节点  
    .addSlaveAddress("redis://127.0.0.1:6380", "redis://127.0.0.1:6381");  
      
//config.setPassword("password")//设置密码  
config.setMasterConnectionPoolSize(500)//设置对于master节点的链接池中链接数最大为500  
config.setSlaveConnectionPoolSize(500)//设置对于slave节点的链接池中链接数最大为500  
config.setIdleConnectionTimeout(10000)//若是当前链接池里的链接数量超过了最小空闲链接数,而同时有链接空闲时间超过了该数值,那么这些链接将会自动被关闭,并从链接池里去掉。时间单位是毫秒。  
config.setConnectTimeout(30000)//同任何节点创建链接时的等待超时。时间单位是毫秒。  
config.setTimeout(3000)//等待节点回复命令的时间。该时间从命令发送成功时开始计时。  
config.setPingTimeout(30000)  
config.setReconnectionTimeout(3000)//当与某个节点的链接断开时,等待与其从新创建链接的时间间隔。时间单位是毫秒。  
  
//建立客户端(发现建立RedissonClient很是耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key不存在不要紧  
RBucket<String> keyObject = redisson.getBucket("key");  
//若是key存在,就设置key的值为新值value  
//若是key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown();

(2)spring集成操做
pom.xml

<!--redisson-->  
<dependency>  
    <groupId>org.redisson</groupId>  
    <artifactId>redisson</artifactId>  
    <version>2.10.4</version>  
</dependency>  
<!--spring-->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-core</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-beans</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>

 spring-redisson.xml

<beans xmlns="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">  
  <bean id="stringCodec" class="org.redisson.client.codec.StringCodec"></bean>    
	<redisson:client id="masterSlave" codec-ref="stringCodec">  
	<redisson:master-slave-servers master-address="redis://127.0.0.1:6379"  
	                                slaveConnectionPoolSize="500"    
	                                masterConnectionPoolSize="500"    
	                                idle-connection-timeout="10000"    
	                                connect-timeout="10000"    
	                                timeout="3000"    
	                                ping-timeout="1000"    
	                                reconnection-timeout="3000"    
	                                database="0">  
	<redisson:slave-address value="redis://127.0.0.1:6380" />  
	<redisson:slave-address value="redis://127.0.0.1:6381" />  
	</redisson:master-slave-servers>  
	</redisson:client>  
</beans>

SpringRedissonTest.java

import org.redisson.api.RBucket;  
import org.redisson.api.RedissonClient;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class SpringRedissonTest {  
    public static void main(String[] args) {  
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-redisson.xml");  
        RedissonClient redisson = (RedissonClient) applicationContext.getBean("masterSlave");  
        // 首先获取redis中的key-value对象,key不存在不要紧  
        RBucket<String> keyObject = redisson.getBucket("key");  
        // 若是key存在,就设置key的值为新值value  
        // 若是key不存在,就设置key的值为value  
        keyObject.set("value");  
    }  
}

 3.5 云托管部署方式

  这种方式主要解决redis提供商为云服务的提供商的redis链接,好比亚马逊云的AWS ElastiCache和微软云的Azure Redis 缓存

    (1)纯java操做

Config config = new Config();  
  
//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec  
//以前使用的spring-data-redis,用的客户端jedis,编码为org.springframework.data.redis.serializer.StringRedisSerializer  
//改用redisson后为了之间数据能兼容,这里修改编码为org.redisson.client.codec.StringCodec  
config.setCodec(new org.redisson.client.codec.StringCodec());  
  
config.useReplicatedServers()  
<span style="white-space:pre">  </span>// 主节点变化扫描间隔时间  
    .setScanInterval(2000)   
    //设置云服务商的redis服务IP和端口,目前支持亚马逊云的AWS ElastiCache和微软云的Azure Redis 缓存  
    .addNodeAddress("redis://123.57.221.104.1:6379")  
    .addNodeAddress("redis://123.57.221.105:6380")  
    .addNodeAddress("redis://123.57.221.106:6382");  
      
//config.setPassword("password")//设置密码  
config.setMasterConnectionPoolSize(500)//设置对于master节点的链接池中链接数最大为500  
config.setSlaveConnectionPoolSize(500)//设置对于slave节点的链接池中链接数最大为500  
config.setIdleConnectionTimeout(10000)//若是当前链接池里的链接数量超过了最小空闲链接数,而同时有链接空闲时间超过了该数值,那么这些链接将会自动被关闭,并从链接池里去掉。时间单位是毫秒。  
config.setConnectTimeout(30000)//同任何节点创建链接时的等待超时。时间单位是毫秒。  
config.setTimeout(3000)//等待节点回复命令的时间。该时间从命令发送成功时开始计时。  
config.setPingTimeout(30000)  
config.setReconnectionTimeout(3000)//当与某个节点的链接断开时,等待与其从新创建链接的时间间隔。时间单位是毫秒。  
  
//建立客户端(发现建立RedissonClient很是耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key不存在不要紧  
RBucket<String> keyObject = redisson.getBucket("key");  
//若是key存在,就设置key的值为新值value  
//若是key不存在,就设置key的值为value  
keyObject.set("value");  
  
//最后关闭RedissonClient  
redisson.shutdown();

(2)spring集成操做
pom.xml

<!--redisson-->  
<dependency>  
    <groupId>org.redisson</groupId>  
    <artifactId>redisson</artifactId>  
    <version>2.10.4</version>  
</dependency>  
<!--spring-->  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-core</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-beans</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>  
<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-context</artifactId>  
    <version>4.2.8.RELEASE</version>  
</dependency>

 spring-redisson.xml

<beans xmlns="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">  
  <bean id="stringCodec" class="org.redisson.client.codec.StringCodec"></bean>  
	<redisson:client id="cloud" codec-ref="stringCodec">  
	<redisson:replicated-servers slaveConnectionPoolSize="500"    
	                                masterConnectionPoolSize="500"    
	                                idle-connection-timeout="10000"    
	                                connect-timeout="10000"    
	                                timeout="3000"    
	                                ping-timeout="1000"    
	                                reconnection-timeout="3000"    
	                                database="0">>  
	<redisson:node-address value="redis://123.57.221.104:6379" />  
	<redisson:node-address value="redis://123.57.221.105:6380" />  
	<redisson:node-address value="redis://123.57.221.106:6381" />  
	</redisson:replicated-servers>  
	</redisson:client>  
</beans>

SpringRedissonTest.java

import org.redisson.api.RBucket;  
import org.redisson.api.RedissonClient;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
public class SpringRedissonTest {  
    public static void main(String[] args) {  
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-redisson.xml");  
        RedissonClient redisson = (RedissonClient) applicationContext.getBean("cloud");  
        // 首先获取redis中的key-value对象,key不存在不要紧  
        RBucket<String> keyObject = redisson.getBucket("key");  
        // 若是key存在,就设置key的值为新值value  
        // 若是key不存在,就设置key的值为value  
        keyObject.set("value");  
    }  
}

4.Redisson API与原生redis操做命令映射关系

redisson的客户端API操做和之前的jedis稍微不同的是你首先得获取到某个操做的key-value对象,而后再对其进行操做,好比我想设置key=time,value=20171013这个string的key-value对象,那么从下表首先查询到SET对应的对象为:

那么从API操做上你就须要首先获取到RBucket对象,而后调用其3个方法来分别执行set操做,以单节点部署方式举例以下:

//建立配置  
Config config = new Config();  
//指定使用单节点部署方式  
config.useSingleServer().setAddress("redis://127.0.0.1:6379");  
//建立客户端(发现建立RedissonClient很是耗时,基本在2秒-4秒左右)  
RedissonClient redisson = Redisson.create(config);  
  
//首先获取redis中的key-value对象,key=time不存在不要紧  
RBucket<String> keyObject = redisson.getBucket("time");  
//若是key=time存在,就设置key=time的值为新值20171013  
//若是key=time不存在,就设置key的值为20171013  
keyObject.set("20171013");  
  
//最后关闭RedissonClient  
redisson.shutdown();

至于其余的redis命令操做经过哪一个对象去操做,你能够经过以下表格查询:

Redis命令 Redisson对象方法
AUTH Config.setPassword();
BITCOUNT RBitSet.cardinality(), RBitSet.cardinalityAsync(), RBitSetReactive.cardinality()
BITOP RBitSet.or(), RBitSet.orAsync(), RBitSetReactive.or();
RBitSet.and(), RBitSet.andAsync(), RBitSetReactive.and();
RBitSet.not();
RBitSet.xor(), RBitSet.xorAsync(), RBitSetReactive.xor()
BITPOS RBitSet.length(), RBitSet.lengthAsync(), RBitSetReactive.length()
BLPOP RBlockingQueue.take(), RBlockingQueue.takeAsync(), RBlockingQueueReactive.take();
RBlockingQueue.poll(), RBlockingQueue.pollAsync(), RBlockingQueueReactive.poll();
RBlockingQueue.pollFromAny(), RBlockingQueue.pollFromAnyAsync(), RBlockingQueueReactive.pollFromAny();
BRPOP RBlockingDeque.takeLast(), RBlockingDeque.takeLastAsync(), RBlockingDequeReactive.takeLast();
BRPOPLPUSH RBlockingQueue.pollLastAndOfferFirstTo(), RBlockingQueue.pollLastAndOfferFirstToAsync(), RBlockingQueueReactive.pollLastAndOfferFirstTo();
CLIENT SETNAME Config.setClientName();
CLUSTER INFO ClusterNode.info();
CLUSTER KEYSLOT RKeys.getSlot(), RKeys.getSlotAsync(), RKeysReactive.getSlot();
CLUSTER NODES Used in ClusterConnectionManager
DBSIZE RKeys.count(), RKeys.countAsync(), RKeysReactive.count();
DECR RAtomicLong.decrementAndGet(), RAtomicLong.decrementAndGetAsync(), RAtomicLongReactive.decrementAndGetAsync();
DEL RObject.delete(), RObject.deleteAsync(), RObjectReactive.delete();
RKeys.delete(), RKeys.deleteAsync();
STRLEN RBucket.size(), RBucket.sizeAsync(), RBucketReactive.size();
EVAL RScript.eval(), RScript.evalAsync(), RScriptReactive.eval();
CLIENT REPLY RBatch.executeSkipResult();
EVALSHA RScript.evalSha(), RScript.evalShaAsync(), RScriptReactive.evalSha();
EXISTS RObject.isExists(), RObject.isExistsAsync(), RObjectReactive.isExists();
FLUSHALL RKeys.flushall(), RKeys.flushallAsync(), RKeysReactive.flushall();
FLUSHDB RKeys.flushdb(), RKeys.flushdbAsync(), RKeysReactive.flushdb();
GEOADD RGeo.add(), RGeo.addAsync(), RGeoReactive.add();
GEODIST RGeo.dist(), RGeo.distAsync(), RGeoReactive.dist();
GEOHASH RGeo.hash(), RGeo.hashAsync(), RGeoReactive.hash();
GEOPOS RGeo.pos(), RGeo.posAsync(), RGeoReactive.pos();
GEORADIUS RGeo.radius(), RGeo.radiusAsync(), RGeoReactive.radius();
RGeo.radiusWithDistance(), RGeo.radiusWithDistanceAsync(), RGeoReactive.radiusWithDistance();
RGeo.radiusWithPosition(), RGeo.radiusWithPositionAsync(), RGeoReactive.radiusWithPosition();
GEORADIUSBYMEMBER RGeo.radius(), RGeo.radiusAsync(), RGeoReactive.radius();
RGeo.radiusWithDistance(), RGeo.radiusWithDistanceAsync(), RGeoReactive.radiusWithDistance();
RGeo.radiusWithPosition(), RGeo.radiusWithPositionAsync(), RGeoReactive.radiusWithPosition();
GET RBucket.get(), RBucket.getAsync(), RBucketReactive.get();
GETBIT RBitSet.get(), RBitSet.getAsync(), RBitSetReactive.get();
GETSET RBucket.getAndSet(), RBucket.getAndSetAsync(), RBucketReactive.getAndSet();
RAtomicLong.getAndSet(), RAtomicLong.getAndSetAsync(), RAtomicLongReactive.getAndSet();
RAtomicDouble.getAndSet(), RAtomicDouble.getAndSetAsync(), RAtomicDoubleReactive.getAndSet();
HDEL RMap.fastRemove(), RMap.fastRemoveAsync(), RMapReactive.fastRemove();
HEXISTS RMap.containsKey(), RMap.containsKeyAsync(), RMapReactive.containsKey();
HGET RMap.get(), RMap.getAsync(), RMapReactive.get();
HSTRLEN RMap.valueSize(), RMap.valueSizeAsync(), RMapReactive.valueSize();
HGETALL RMap.readAllEntrySet(), RMap.readAllEntrySetAsync(), RMapReactive.readAllEntrySet();
HINCRBY RMap.addAndGet(), RMap.addAndGetAsync(), RMapReactive.addAndGet();
HINCRBYFLOAT RMap.addAndGet(), RMap.addAndGetAsync(), RMapReactive.addAndGet();
HKEYS RMap.readAllKeySet(), RMap.readAllKeySetAsync(), RMapReactive.readAllKeySet();
HLEN RMap.size(), RMap.sizeAsync(), RMapReactive.size();
HMGET RMap.getAll(), RMap.getAllAsync(), RMapReactive.getAll();
HMSET RMap.putAll(), RMap.putAllAsync(), RMapReactive.putAll();
HSET RMap.put(), RMap.putAsync(), RMapReactive.put();
HSETNX RMap.fastPutIfAbsent(), RMap.fastPutIfAbsentAsync, RMapReactive.fastPutIfAbsent();
HVALS RMap.readAllValues(), RMap.readAllValuesAsync(), RMapReactive.readAllValues();
INCR RAtomicLong.incrementAndGet(), RAtomicLong.incrementAndGetAsync(), RAtomicLongReactive.incrementAndGet();
INCRBY RAtomicLong.addAndGet(), RAtomicLong.addAndGetAsync(), RAtomicLongReactive.addAndGet();
KEYS RKeys.findKeysByPattern(), RKeys.findKeysByPatternAsync(), RKeysReactive.findKeysByPattern();
RedissonClient.findBuckets();
LINDEX RList.get(), RList.getAsync(), RListReactive.get();
LLEN RList.size(), RList.sizeAsync(), RListReactive.Size();
LPOP RQueue.poll(), RQueue.pollAsync(), RQueueReactive.poll();
LPUSH RDeque.addFirst(), RDeque.addFirstAsync();
RDequeReactive.addFirst(), RDeque.offerFirst(), RDeque.offerFirstAsync(), RDequeReactive.offerFirst();
LRANGE RList.readAll(), RList.readAllAsync(), RListReactive.readAll();
LREM RList.fastRemove(), RList.fastRemoveAsync(), RList.remove(), RList.removeAsync(), RListReactive.remove();
RDeque.removeFirstOccurrence(), RDeque.removeFirstOccurrenceAsync(), RDequeReactive.removeFirstOccurrence();
RDeque.removeLastOccurrence(), RDeque.removeLastOccurrenceAsync(), RDequeReactive.removeLastOccurrence();
LSET RList.fastSet(), RList.fastSetAsync(), RListReactive.fastSet();
LTRIM RList.trim(), RList.trimAsync(), RListReactive.trim();
LINSERT RList.addBefore(), RList.addBeforeAsync(), RList.addAfter(), RList.addAfterAsync(), RListReactive.addBefore(), RListReactive.addAfter();
MGET RedissonClient.loadBucketValues();
MIGRATE RObject.migrate(), RObject.migrateAsync();
MOVE RObject.move(), RObject.moveAsync();
MSET RedissonClient.saveBuckets();
PERSIST RExpirable.clearExpire(), RExpirable.clearExpireAsync(), RExpirableReactive.clearExpire();
PEXPIRE RExpirable.expire(), RExpirable.expireAsync(), RExpirableReactive.expire();
PEXPIREAT RExpirable.expireAt(), RExpirable.expireAtAsync(), RExpirableReactive.expireAt();
PFADD RHyperLogLog.add(), RHyperLogLog.addAsync(), RHyperLogLogReactive.add();
RHyperLogLog.addAll(), RHyperLogLog.addAllAsync(), RHyperLogLogReactive.addAll();
PFCOUNT RHyperLogLog.count(), RHyperLogLog.countAsync(), RHyperLogLogReactive.count();
RHyperLogLog.countWith(), RHyperLogLog.countWithAsync(), RHyperLogLogReactive.countWith();
PFMERGE RHyperLogLog.mergeWith(), RHyperLogLog.mergeWithAsync(), RHyperLogLogReactive.mergeWith();
PING Node.ping(); NodesGroup.pingAll();
PSUBSCRIBE RPatternTopic.addListener();
PTTL RExpirable.remainTimeToLive(), RExpirable.remainTimeToLiveAsync(), RExpirableReactive.remainTimeToLive();
PUBLISH RTopic.publish
PUNSUBSCRIBE RPatternTopic.removeListener();
RANDOMKEY RKeys.randomKey(), RKeys.randomKeyAsync(), RKeysReactive.randomKey();
RENAME RObject.rename(), RObject.renameAsync(), RObjectReactive.rename();
RENAMENX RObject.renamenx(), RObject.renamenxAsync(), RObjectReactive.renamenx();
RPOP RDeque.pollLast(), RDeque.pollLastAsync(), RDequeReactive.pollLast();
RDeque.removeLast(), RDeque.removeLastAsync(), RDequeReactive.removeLast();
RPOPLPUSH RDeque.pollLastAndOfferFirstTo(), RDeque.pollLastAndOfferFirstToAsync();
RPUSH RList.add(), RList.addAsync(), RListReactive.add();
SADD RSet.add(), RSet.addAsync(), RSetReactive.add();
SCARD RSet.size(), RSet.sizeAsync(), RSetReactive.size();
SCRIPT EXISTS RScript.scriptExists(), RScript.scriptExistsAsync(), RScriptReactive.scriptExists();
SCRIPT FLUSH RScript.scriptFlush(), RScript.scriptFlushAsync(), RScriptReactive.scriptFlush();
SCRIPT KILL RScript.scriptKill(), RScript.scriptKillAsync(), RScriptReactive.scriptKill();
SCRIPT LOAD RScript.scriptLoad(), RScript.scriptLoadAsync(), RScriptReactive.scriptLoad();
SDIFFSTORE RSet.diff(), RSet.diffAsync(), RSetReactive.diff();
SELECT Config.setDatabase();
SET RBucket.set(); RBucket.setAsync(); RBucketReactive.set();
SETBIT RBitSet.set(); RBitSet.setAsync(); RBitSet.clear(); RBitSet.clearAsync();
SETEX RBucket.set(); RBucket.setAsync(); RBucketReactive.set();
SETNX RBucket.trySet(); RBucket.trySetAsync(); RBucketReactive.trySet();
SISMEMBER RSet.contains(), RSet.containsAsync(), RSetReactive.contains();
SINTERSTORE RSet.intersection(), RSet.intersectionAsync(), RSetReactive.intersection();
SINTER RSet.readIntersection(), RSet.readIntersectionAsync(), RSetReactive.readIntersection();
SMEMBERS RSet.readAll(), RSet.readAllAsync(), RSetReactive.readAll();
SMOVE RSet.move(), RSet.moveAsync(), RSetReactive.move();
SPOP RSet.removeRandom(), RSet.removeRandomAsync(), RSetReactive.removeRandom();
SREM RSet.remove(), RSet.removeAsync(), RSetReactive.remove();
SUBSCRIBE RTopic.addListener(), RTopicReactive.addListener();
SUNION RSet.readUnion(), RSet.readUnionAsync(), RSetReactive.readUnion();
SUNIONSTORE RSet.union(), RSet.unionAsync(), RSetReactive.union();
TTL RExpirable.remainTimeToLive(), RExpirable.remainTimeToLiveAsync(), RExpirableReactive.remainTimeToLive();
UNSUBSCRIBE RTopic.removeListener(), RTopicReactive.removeListener();
ZADD RScoredSortedSet.add(), RScoredSortedSet.addAsync(), RScoredSortedSetReactive.add();
ZCARD RScoredSortedSet.size(), RScoredSortedSet.sizeAsync(), RScoredSortedSetReactive.size();
ZINCRBY RScoredSortedSet.addScore(), RScoredSortedSet.addScoreAsync(), RScoredSortedSetReactive.addScore();
ZLEXCOUNT RLexSortedSet.lexCount(), RLexSortedSet.lexCountAsync(), RLexSortedSetReactive.lexCount(); 
RLexSortedSet.lexCountHead(), RLexSortedSet.lexCountHeadAsync(), RLexSortedSetReactive.lexCountHead();
RLexSortedSet.lexCountTail(), RLexSortedSet.lexCountTailAsync(), RLexSortedSetReactive.lexCountTail();
ZRANGE RScoredSortedSet.valueRange(), RScoredSortedSet.valueRangeAsync(), RScoredSortedSetReactive.valueRange();
ZREVRANGE RScoredSortedSet.valueRangeReversed(), RScoredSortedSet.valueRangeReversedAsync(), RScoredSortedSetReactive.valueRangeReversed();
ZUNIONSTORE RScoredSortedSet.union(), RScoredSortedSet.unionAsync(), RScoredSortedSetReactive.union();
ZINTERSTORE RScoredSortedSet.intersection(), RScoredSortedSet.intersectionAsync(), RScoredSortedSetReactive.intersection();
ZRANGEBYLEX RLexSortedSet.lexRange(), RLexSortedSet.lexRangeAsync(), RLexSortedSetReactive.lexRange(); 
RLexSortedSet.lexRangeHead(), RLexSortedSet.lexRangeHeadAsync(), RLexSortedSetReactive.lexRangeHead();
RLexSortedSet.lexRangeTail(), RLexSortedSet.lexRangeTailAsync(), RLexSortedSetReactive.lexRangeTail();
ZRANGEBYSCORE RScoredSortedSet.valueRange(), RScoredSortedSet.valueRangeAsync(), RScoredSortedSetReactive.valueRange(); 
RScoredSortedSet.entryRange(), RScoredSortedSet.entryRangeAsync(), RScoredSortedSetReactive.entryRange();
TIME Node.time();
ZRANK RScoredSortedSet.rank(), RScoredSortedSet.rankAsync(), RScoredSortedSetReactive.rank();
ZREM RScoredSortedSet.remove(), RScoredSortedSet.removeAsync(), RScoredSortedSetReactive.remove();
RScoredSortedSet.removeAll(), RScoredSortedSet.removeAllAsync(), RScoredSortedSetReactive.removeAll();
ZREMRANGEBYLEX RLexSortedSet.removeRangeByLex(), RLexSortedSet.removeRangeByLexAsync(), RLexSortedSetReactive.removeRangeByLex(); 
RLexSortedSet.removeRangeHeadByLex(), RLexSortedSet.removeRangeHeadByLexAsync(), RLexSortedSetReactive.removeRangeHeadByLex();
RLexSortedSet.removeRangeTailByLex(), RLexSortedSet.removeRangeTailByLexAsync(), RLexSortedSetReactive.removeRangeTailByLex();
ZREMRANGEBYLEX RScoredSortedSet.removeRangeByRank(), RScoredSortedSet.removeRangeByRankAsync(), RScoredSortedSetReactive.removeRangeByRank();
ZREMRANGEBYSCORE RScoredSortedSet.removeRangeByScore(), RScoredSortedSet.removeRangeByScoreAsync(), RScoredSortedSetReactive.removeRangeByScore();
ZREVRANGEBYSCORE RScoredSortedSet.entryRangeReversed(), RScoredSortedSet.entryRangeReversedAsync(), RScoredSortedSetReactive.entryRangeReversed(), RScoredSortedSet.valueRangeReversed(), RScoredSortedSet.valueRangeReversedAsync(), RScoredSortedSetReactive.valueRangeReversed();
ZREVRANK RScoredSortedSet.revRank(), RScoredSortedSet.revRankAsync(), RScoredSortedSetReactive.revRank();
ZSCORE RScoredSortedSet.getScore(), RScoredSortedSet.getScoreAsync(), RScoredSortedSetReactive.getScore();
SCAN RKeys.getKeys(), RKeysReactive.getKeys();
SSCAN RSet.iterator(), RSetReactive.iterator();
HSCAN RMap.keySet().iterator(), RMap.values().iterator(), RMap.entrySet().iterator(), RMapReactive.keyIterator(), RMapReactive.valueIterator(), RMapReactive.entryIterator();
ZSCAN RScoredSortedSet.iterator(), RScoredSortedSetReactive.iterator();

5.Redisson的同步和异步操做API

    Redisson框架提供的几乎全部对象都包含了同步和异步相互匹配的方法。这些对象均可以经过RedissonClient接口获取。同时还为大部分Redisson对象提供了知足异步流处理标准的程序接口RedissonReactiveClient。

5.1 同步执行方式

// 同步执行方式  
RedissonClient client = Redisson.create(config);  
RAtomicLong longObject = client.getAtomicLong('myLong');  
longObject.compareAndSet(3, 401);

5.2 异步执行方式

// 异步执行方式  
RedissonClient client = Redisson.create(config);  
RAtomicLong longObject = client.getAtomicLong('myLong');  
longObject.compareAndSetAsync(3, 401);

5.3 异步流执行方式

// 异步流执行方式  
RedissonReactiveClient client = Redisson.createReactive(config);  
RAtomicLongReactive longObject = client.getAtomicLong('myLong');  
longObject.compareAndSet(3, 401);

6.redisson支持的配置方式

    6.1 直接操做程序API

        Redisson程序化的配置方法是经过构建Config对象实例来实现的。例如:

Config config = new Config();  
config.setUseLinuxNativeEpoll(true);  
config.useClusterServers()  
      //能够用"rediss://"来启用SSL链接  
      .addNodeAddress("redis://127.0.0.1:7181");

6.2 经过JSON文件配置

        Redisson的配置文件能够是JSON格式。 能够经过调用Config.fromJSON方法并指定一个File实例来实现读取JSON格式的配置:

Config config = Config.fromJSON(new File("config-file.json"));  
RedissonClient redisson = Redisson.create(config);

6.3 经过YAML文件配置

        Redisson的配置文件能够是YAML格式。 能够经过调用config.fromYAML方法并指定一个File实例来实现读取YAML格式的配置:

Config config = Config.fromYAML(new File("config-file.yaml"));  
RedissonClient redisson = Redisson.create(config);

6.4 经过Spring XML配置

        Redisson为Spring框架提供了一套经过命名空间来配置实例的方式。一个Redisson的实例能够经过这样的方式来配置:

<beans xmlns="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">  
<!-- 配置分类1:netty相关-->  
<!-- 配置分类2:redis服务端IP和端口-->  
<!-- 配置分类3:redisson客户端负载均衡-->  
<!-- 配置分类4:发布和订阅链接池配置-->  
<!-- 配置分类5:链接池配置-->  
<!-- 配置分类6:超时设置-->  
<!-- 配置分类7:失败重试配置-->  
<!-- 配置分类8:redis库和密码设置-->  
<!-- 配置分类9:SSL相关设置-->  
<!-- 配置分类10:特有的配置 -->  
<redisson:client>  
    <!-- 单节点部署方式  -->  
    <redisson:single-server ... />  
    <!-- 主从部署方式  -->  
    <redisson:master-slave-servers ... />  
    <!-- 哨兵部署方式 -->  
    <redisson:sentinel-servers ... />  
    <!-- 集群部署方式 -->  
    <redisson:cluster-servers ... />  
    <!-- 云部署方式 -->  
    <redisson:replicated-servers ... />  
</redisson:client>  
</beans>

更多的使用方法请前往第三方框架整合文档了解。 举例单节点redis的详细配置以下:

<beans xmlns="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节点模式的配置 -->  
    <bean id="jsonJacksonCodec" class="org.redisson.codec.JsonJacksonCodec"></bean>  
    <bean id="defaultCodecProvider" class="org.redisson.codec.DefaultCodecProvider"></bean>  
    <bean id="defaultResolverProvider" class="org.redisson.liveobject.provider.DefaultResolverProvider"></bean>  
    <bean id="nioEventLoopGroup" class="io.netty.channel.nio.NioEventLoopGroup"></bean>  
      
    <bean id="defaultThreadFactory" class="io.netty.util.concurrent.DefaultThreadFactory">  
        <constructor-arg name="poolName" value="redisson"></constructor-arg>  
    </bean>  
    <bean id="executors" factory-method="newFixedThreadPool" class="java.util.concurrent.Executors">  
        <constructor-arg name="nThreads" value="50"></constructor-arg>  
        <constructor-arg ref="defaultThreadFactory"></constructor-arg>  
    </bean>  
      
    <redisson:client   
        id="standalone"  
        name="aliasName1,aliasName2"   
        threads="8"   
        netty-threads="8"   
        codec-ref="jsonJacksonCodec"  
        codec-provider-ref="defaultCodecProvider"   
        use-linux-native-epoll="false"  
        redisson-reference-enabled="true"  
        resolver-provider-ref="defaultResolverProvider"   
        executor-ref="executors"   
        event-loop-group-ref="nioEventLoopGroup" >  
    <redisson:single-server   
        address="redis://192.168.29.24:6379"     
        subscription-connection-minimum-idle-size="1"  
        subscriptions-per-connection="50"  
        subscription-connection-pool-size="50"  
        connection-minimum-idle-size="10"  
        connection-pool-size="64"  
        idle-connection-timeout="10000"  
        connect-timeout="10000"  
        timeout="3000"  
        ping-timeout="3000"  
        retry-attempts="3"  
        retry-interval="1500"  
        reconnection-timeout="3000"  
        failed-attempts="3"  
        database="0"  
        password=""  
        client-name=""  
        ssl-enable-endpoint-identification="true"  
        ssl-keystore=""  
        ssl-keystore-password=""  
        ssl-provider="JDK"  
        ssl-truststore=""  
        ssl-truststore-password=""  
        dns-monitoring="false"  
        dns-monitoring-interval="5000"/>  
    </redisson:client>  
</beans>

7.redisson如何配置参数

    redisson的配置参数不少,容易让人感受疲乏,更恐怖的是针对每种部署方式,相关参数也不尽相同,但无论怎么变化,配置参数的归类就那么几个,初学者能够先记住配置参数的大体分类,而后针对每一个分类下的不一样参数有无进行对比总结,这样能方便理解,总结归类redisson的配置参数分类以下:

  • 配置分类1:netty相关
  • 配置分类2:redis服务端IP和端口
  • 配置分类3:redisson客户端负载均衡
  • 配置分类4:发布和订阅链接池配置
  • 配置分类5:链接池配置
  • 配置分类6:超时设置
  • 配置分类7:失败重试配置
  • 配置分类8:redis库和密码设置
  • 配置分类9:SSL相关设置
  • 配置分类10:特有的配置

    前面已经知道如何简单的去操做redisson客户端来调用redis服务端,默认值设置了服务端相关的redis IP地址和端口,没有作过多设置,那么redisson有哪些方面设置,如何设置呢?

  •     由于redisson是基于java的网络编程框架netty实现,因此首先提供了暴露了netty相关配置参数;
  •     redis服务端要么是单机要么是多机,那么这里必然存在主从相关设置;
  •     redisson做为客户端,若是频繁去建立和关闭链接,那么性能必然大幅降低,那么这里必然有链接池相关配置;
  •     考虑到安全相关,因此redis还须要有SSL相关设置;
  •     后面还有客户端操做失败重试相关设置参数和根据不一样部署的特殊配置;

    这里将各类部署方式的配置列表以下:

 

转自:https://blog.csdn.net/zilong_zilong/article/details/78252037

相关文章
相关标签/搜索