欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Spring-data-redis如何操作rediscluster

小編給大家分享一下Spring-data-redis如何操作redis cluster,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

公司主營業(yè)務(wù):成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)推出夏縣免費(fèi)做網(wǎng)站回饋大家。

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

通過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來實(shí)現(xiàn)

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

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配置,配置如下:

   <!--通過構(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包中無對(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è)置過大時(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)資源文件的訪問-->

   <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.通過注解方式使用JedisClusterConnection和RedisTemplate

@Autowired

    JedisClusterConnection clusterConnection;

   @Autowired

   RedisTemplate redisTemplate;

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

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

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

使用時(shí),直接通過注解或者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è)置過??;

(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);

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

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

看完了這篇文章,相信你對(duì)“Spring-data-redis如何操作redis cluster”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

分享文章:Spring-data-redis如何操作rediscluster
標(biāo)題URL:http://aaarwkj.com/article0/iggcoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、全網(wǎng)營銷推廣靜態(tài)網(wǎng)站、網(wǎng)站營銷、虛擬主機(jī)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)
欧美成人精品欧美一级乱| 91一区二区亚洲嫩草| 亚洲精品国产亚洲精品| 国产三级精品三级专区| 亚洲人妻乱人伦中文字幕在线| 亚洲国产精品一区二区首页| 麻豆久久精品国产亚洲精品超碰热| 国产真实乱偷精品视频免| 亚洲精品黄色在线观看| 国产午夜亚洲精品福利| 在线观看日韩三级av| 97精品在线免费视频| 人妻少妇亚洲中文字幕| 激情毛片av在线免费看| 欧美日韩另类综合久久久| 亚洲欧美中文日韩一区| 日韩视频免费看一区二区| 伊人欧美一区二区三区| 国产在线乱码一区二区| 6080yy精品一区二区三区| 国产91久久精品一区二区| 日韩精品精美视频在线观看| 小仙女精品经典三级永久| 国产国语久久91老女人| 欧美另类亚洲综合久青草| 日本加勒比高清在线播放| 日韩亚洲一区在线观看| 中文字幕人妻出轨一区二区| 日本熟妇中文字幕系列| 最新日韩欧美一区二区| 亚洲国产黄色美女视频| 国产经典午夜福利在线| 偷拍一区二区三区免费| 国产一级性生活高清在线| 久久热久久热在线视频| 亚洲人妻乱人伦中文字幕在线| 日韩日美日韩av影视| 天堂av在线一区二区三区| 亚洲一区二区三区精品电影网| 免费av不卡一区二区| 欧美伊人久久大综合精品|