最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring-data-redis操作redis cluster的示例代碼

 更新時(shí)間:2018年10月11日 09:50:33   作者:moonandstar08  
這篇文章主要介紹了Spring-data-redis操作redis cluster的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Redis 3.X版本引入了集群的新特性,為了保證所開發(fā)系統(tǒng)的高可用性項(xiàng)目組決定引用Redis的集群特性。對(duì)于Redis數(shù)據(jù)訪問(wèn)的支持,目前主要有二種方式:一、以直接調(diào)用jedis來(lái)實(shí)現(xiàn);二、使用spring-data-redis,通過(guò)spring的封裝來(lái)調(diào)用。下面分別對(duì)這二種方式如何操作Redis進(jìn)行說(shuō)明。

一、利用Jedis來(lái)實(shí)現(xiàn)

通過(guò)Jedis操作Redis Cluster的模型可以參考Redis官網(wǎng),具體如下:

    Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

     //Jedis Cluster will attempt to discover cluster nodes automatically

     jedisClusterNodes.add(new HostAndPort("10.96.5.183",9001));

     jedisClusterNodes.add(new HostAndPort("10.96.5.183",9002));

     jedisClusterNodes.add(new HostAndPort("10.96.5.183",9003));

    JedisCluster jc = new JedisCluster(jedisClusterNodes);

    jc.set("foo","bar");

    jc.get("foo");

二、利用spring-data-redis來(lái)實(shí)現(xiàn)

目前spring-data-redis已發(fā)布的主干版本都不能很好的支持Redis Cluster的新特性。為了解決此問(wèn)題spring-data-redis開源項(xiàng)目組單獨(dú)拉了一個(gè)315分支,但截止到目前尚未發(fā)布。下面在分析spring-data-redis源碼的基礎(chǔ)上配置spring實(shí)現(xiàn)操作Redis Cluster.下面分別針對(duì)XML和注入的方式進(jìn)行說(shuō)明。

315分支gitHub下載路徑如下:https://github.com/spring-projects/spring-data-redis

315分支源碼下載路徑:http://maven.springframework.org/snapshot/org/springframework/data/spring-data-redis/1.7.0.DATAREDIS-315-SNAPSHOT/

(1)采用setClusterNodes屬性方式構(gòu)造RedisClusterConfiguration

代碼目錄結(jié)構(gòu)如下所示:

     src
       com.example.bean 
       com.example.repo
       com.example.repo.impl
       resources

A.在resources目錄下增加spring-config.xml配置,配置如下:

   <!--通過(guò)構(gòu)造方法注入RedisNode-->

   <bean id="clusterRedisNodes1"  class="org.springframework.data.redis.connection.RedisNode"> 

      <constructor-arg value="10.96.5.183" />

      <constructor-arg value="9002" type="int" />

   </bean>

   ....

  <!--setter方式注入-->

  <bean id="redisClusterConfiguration"  class="org.springframework.data.redis.connection.RedisClusterConfiguration">

    <property name="clusterNodes"> 

       <set>

            <ref bean="clusterRedisNodes1"/>

            <ref bean="clusterRedisNodes2"/>

            <ref bean="clusterRedisNodes3"/>

       </set>

    </property>

   <!--紅色所示部分在從gitHub上獲取的jar包中無(wú)對(duì)應(yīng)setter方法,因此需要修改其對(duì)應(yīng)的源碼。

另外,如果不設(shè)置clusterTimeOut值,源碼中默認(rèn)為2S。當(dāng)集群服務(wù)器與客戶端不在同一服務(wù)器上時(shí),容易報(bào):Could not get a resource from the Cluster;

如果不設(shè)置maxRedirects值,源碼中默認(rèn)為5。一般當(dāng)此值設(shè)置過(guò)大時(shí),容易報(bào):Too many Cluster redirections -->

    <property name="clusterTimeOut" value="10000" />

    <property name="maxRedirects"  value="5" />

  </bean>

 <!--setter方式注入,對(duì)應(yīng)的屬性需存在setterXXX方法-->

  <bean id="jedisPoolConfig"  class="redis.clients.jedis.JedisPoolConfig">

      <property name="maxToal" value="1000" />

      <property name="maxIdle" value="1000" />

      <property name="maxWaitMillis" value="1000" />

  </bean>

 <bean id="jedisConnFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true">

      <constructor-arg ref="redisClusterConfiguration" />

      <constructor-arg ref="jedisPoolConfig" />

 </bean>

 <bean id="redisTemplate"  class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory" />

<!--setter方式注入PersonRepoImpl-->

<bean id="personRepo" class="com.example.repo.impl.PersonRepoImpl">

    <property name="redisTemplate" ref="redisTemplate" />

</bean>

注:加載lua文件

<bean id ="XXX" class="org.springframework.data.redis.core.script.DefaultRedisScript">

  <property name="location" value="./redis/XXX.lua" />

 <property name="resultType" value="java.lang.Void" />

</bean>

在com.example.repo.impl下增加PersonRepoImpl,主要包括屬性private RedisTemplate<String,Bean>  redisTemplate(該屬性存在setterXXX方法,對(duì)應(yīng)property屬性);

利用redisTemplate.opsForHash().put()即可完成對(duì)Redis Cluster的操作。

 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("resources/spring-config.xml").getPath());

 Repo repo =(Repo)context.getBean("personRepo");

(2)采用RedisClusterConfiguration(PropertySource<?> propertySource)方式構(gòu)造RedisClusterConfiguration

  代碼目錄結(jié)構(gòu)如下所示:

     src
       com.redis.cluster.support.config
            MonitorConfig
       resources
           spring-config.xml
            redis.properties

A.在resources目錄下增加spring-config.xml配置,配置如下:

  <!--配置文件加載-->

   <context:property-placeholder location="resources/redis.properties"/>

  <context:property-placeholder location="resources/xxx.properties"/>

   <bean class="com.redis.cluster.support.config.MonitorConfig" />

  <!--對(duì)靜態(tài)資源文件的訪問(wèn)-->

   <mvc:default-servlet-handler/>

   <mvc:annotation-driven />

   <context:component-scan base-package="com.redis.cluster"/>

B.添加redis.properties文件

spring.redis.cluster.nodes=10.48.193.201:7389,10.48.193.201:7388
spring.redis.cluster.timeout=2000
spring.redis.cluster.max-redirects=8

C.編寫初始化JedisConnectionFactory連接工廠的java類

  @Configuration

  public class MonitorConfig {

    @Value("${spring.redis.cluster.nodes}")

    private String clusterNodes;

    @Value("${spring.redis.cluster.timeout}")

    private Long timeout;

   @Value("${spring.redis.cluster.max-redirects}")

    private int redirects;

    @Bean

    public RedisClusterConfiguration getClusterConfiguration() {

      Map<String, Object> source = new HashMap<String, Object>();

      source.put("spring.redis.cluster.nodes", clusterNodes);

      source.put("spring.redis.cluster.timeout", timeout);

      source.put("spring.redis.cluster.max-redirects", redirects);

      return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));

     }

    @Bean

    public JedisConnectionFactory getConnectionFactory() {

      return new JedisConnectionFactory(getClusterConfiguration());

     }

   @Bean

    public JedisClusterConnection getJedisClusterConnection() {

      return (JedisClusterConnection) getConnectionFactory().getConnection();

     }

    @Bean

    public RedisTemplate getRedisTemplate() {

      RedisTemplate clusterTemplate = new RedisTemplate();

      clusterTemplate.setConnectionFactory(getConnectionFactory());

      clusterTemplate.setKeySerializer(new DefaultKeySerializer());

      clusterTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());

      return clusterTemplate;

     }

    }

D.通過(guò)注解方式使用JedisClusterConnection和RedisTemplate

    @Autowired

    JedisClusterConnection clusterConnection;

   @Autowired

   RedisTemplate redisTemplate;

三、簡(jiǎn)單集成Spring

自己編寫jedisCluster的工廠類JedisClusterFactory,然后通過(guò)Spring注入的方式獲取jedisCluster,實(shí)現(xiàn)客戶端使用Redis3.0版本的集群特性。

請(qǐng)參考:http://www.fzitv.net/article/128895.htm

使用時(shí),直接通過(guò)注解或者XML注入即可,如下所示:

   @Autowired
   JedisCluster jedisCluster;

  或者

  <bean id="testRedis" class="com.test.TestRedis">

    <property name="jedisCluster" ref="jedisClusterFactory" />

  </bean>
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new ClassPathResource("resources/spring-config.xml").getPath());

 TestRedis testRedis=(TestRedis)context.getBean("testRedis");

四、Redis Cluster調(diào)試中常見錯(cuò)誤

(1)當(dāng)客戶端與集群服務(wù)器不在同一臺(tái)服務(wù)器上時(shí),有如下錯(cuò)誤Could not get a resource from the Cluster

一般當(dāng)客戶端與集群服務(wù)器在同一臺(tái)服務(wù)器上時(shí),操作Redis Cluster正常; 當(dāng)二者不在同一臺(tái)服務(wù)器上時(shí)報(bào)如上錯(cuò)誤,可能是clusterTimeOut時(shí)間設(shè)置過(guò)??;

(2)操作Redis時(shí)報(bào)Too many cluster redirections

初始化JedisCluster時(shí),設(shè)定JedisCluster的maxRedirections.

JedisCluster(Set<HostAndPort> jedisClusterNode, int timeout, int maxRedirections) ;
JedisCluster jc = new JedisCluster(jedisClusterNodes,5000,1000);

請(qǐng)參考:https://gitHub.com/xetorthio/jedis/issues/659

(3)Redis Cluster數(shù)據(jù)寫入慢

檢查在通過(guò)./redis-trib命令建立集群時(shí),如果是通過(guò)127.0.0.1的方式建立的集群,那么在往Redis Cluster中寫入數(shù)據(jù)時(shí)寫入速度比較慢??梢酝ㄟ^(guò)配置真實(shí)的IP來(lái)規(guī)避此問(wèn)題。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java高并發(fā)的線程中斷的幾種方式詳解

    java高并發(fā)的線程中斷的幾種方式詳解

    這篇文章主要介紹了Java線程中斷機(jī)制幾種方法及示例,向大家分享了這幾種方法的介紹幾代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2021-10-10
  • java反轉(zhuǎn)鏈表的多種解決方法舉例詳解

    java反轉(zhuǎn)鏈表的多種解決方法舉例詳解

    這篇文章主要介紹了java反轉(zhuǎn)鏈表的多種解決方法,分別是使用棧、雙指針和遞歸,每種方法都有其實(shí)現(xiàn)原理和代碼示例,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-04-04
  • Java內(nèi)存模型可見性問(wèn)題相關(guān)解析

    Java內(nèi)存模型可見性問(wèn)題相關(guān)解析

    這篇文章主要介紹了Java內(nèi)存模型可見性問(wèn)題相關(guān)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解

    JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解

    這篇文章主要介紹了JDBC連接MySQL數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java中byte[]、String、Hex字符串等轉(zhuǎn)換的方法

    Java中byte[]、String、Hex字符串等轉(zhuǎn)換的方法

    這篇文章主要介紹了Java中byte[]、String、Hex字符串等轉(zhuǎn)換的方法,代碼很簡(jiǎn)單,需要的朋友可以參考下
    2018-05-05
  • SpringBoot向resources下寫文件的兩種方式

    SpringBoot向resources下寫文件的兩種方式

    這篇文章給大家分享了兩種SpringBoot向resources下寫文件的方式,每種方式都有詳細(xì)的代碼示例,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-08-08
  • SpringBoot開啟異步調(diào)用方法

    SpringBoot開啟異步調(diào)用方法

    這篇文章主要為大家詳細(xì)介紹了SpringBoot開啟異步調(diào)用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • Windows 10上JDK環(huán)境安裝配置圖文教程

    Windows 10上JDK環(huán)境安裝配置圖文教程

    這篇文章主要為大家詳細(xì)介紹了Windows 10上JDK環(huán)境安裝配置圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • java對(duì)象的序列化和反序列化

    java對(duì)象的序列化和反序列化

    這篇文章主要為大家詳細(xì)介紹了java對(duì)象的序列化和反序列化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • java字節(jié)流、字符流與轉(zhuǎn)換流過(guò)程

    java字節(jié)流、字符流與轉(zhuǎn)換流過(guò)程

    輸入輸出流(IO流)是數(shù)據(jù)傳輸?shù)某橄蟾拍?用于表示數(shù)據(jù)在設(shè)備間的傳輸過(guò)程,IO流按數(shù)據(jù)類型分為字符流和字節(jié)流,按數(shù)據(jù)流向分為輸入流和輸出流,字節(jié)流操作單個(gè)字節(jié),字符流操作字符,在實(shí)際應(yīng)用中,非文本文件多用字節(jié)流操作
    2024-10-10

最新評(píng)論

太和县| 青川县| 金溪县| 辽源市| 若尔盖县| 聂拉木县| 调兵山市| 阿鲁科尔沁旗| 和林格尔县| 水城县| 光泽县| 双峰县| 凭祥市| 重庆市| 扎囊县| 邹平县| 连江县| 通辽市| 睢宁县| 崇信县| 通江县| 大方县| 绥化市| 晋宁县| 建德市| 麻江县| 辰溪县| 德钦县| 罗定市| 尼勒克县| 洞头县| 长武县| 华宁县| 晋宁县| 阿巴嘎旗| 孟津县| 杭州市| 黑河市| 米脂县| 瑞金市| 依兰县|