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

相關文章
相關標籤/搜索