欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

一、利用Jedis來實現(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來實現(xiàn)

目前spring-data-redis已發(fā)布的主干版本都不能很好的支持Redis Cluster的新特性。為了解決此問題spring-data-redis開源項目組單獨拉了一個315分支,但截止到目前尚未發(fā)布。下面在分析spring-data-redis源碼的基礎(chǔ)上配置spring實現(xiàn)操作Redis Cluster.下面分別針對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包中無對應(yīng)setter方法,因此需要修改其對應(yīng)的源碼。

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

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

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

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

  </bean>

 <!--setter方式注入,對應(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方法,對應(yīng)property屬性);

利用redisTemplate.opsForHash().put()即可完成對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" />

  <!--對靜態(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;

三、簡單集成Spring

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

請參考:http://www.dbjr.com.cn/article/128895.htm

使用時,直接通過注解或者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)試中常見錯誤

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

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

(2)操作Redis時報Too many cluster redirections

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

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

請參考:https://gitHub.com/xetorthio/jedis/issues/659

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

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

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

相關(guān)文章

  • @PathVariable、@RequestParam和@RequestBody的區(qū)別

    @PathVariable、@RequestParam和@RequestBody的區(qū)別

    本文主要介紹了@PathVariable、@RequestParam和@RequestBody的區(qū)別和使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • 淺談一下數(shù)據(jù)庫連接池Druid德魯伊

    淺談一下數(shù)據(jù)庫連接池Druid德魯伊

    數(shù)據(jù)庫連接池就是一個容器持有多個數(shù)據(jù)庫連接,當(dāng)程序需要操作數(shù)據(jù)庫的時候直接從池中取出連接,使用完之后再還回去,和線程池一個道理,需要的朋友可以參考下
    2023-05-05
  • java生成申請單序列號的實現(xiàn)方法

    java生成申請單序列號的實現(xiàn)方法

    申請單序列號一般要求根據(jù)一定的規(guī)則生成后幾位連續(xù)的字符串,下面是我項目中使用的生成序列號的代碼,其中用到了鎖機(jī)制,有需要的朋友可以參考一下
    2014-01-01
  • 使用Java讀取Excel文件數(shù)據(jù)的方法詳解

    使用Java讀取Excel文件數(shù)據(jù)的方法詳解

    通過編程方式讀取Excel數(shù)據(jù)能實現(xiàn)數(shù)據(jù)導(dǎo)入、批量處理、數(shù)據(jù)比對和更新等任務(wù)的自動化,本文為大家介紹了三種Java讀取Excel文件數(shù)據(jù)的方法,需要的可以參考下
    2024-01-01
  • SpringBoot向resources下寫文件的兩種方式

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

    這篇文章給大家分享了兩種SpringBoot向resources下寫文件的方式,每種方式都有詳細(xì)的代碼示例,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2023-08-08
  • elasticsearch分布式及數(shù)據(jù)的功能源碼分析

    elasticsearch分布式及數(shù)據(jù)的功能源碼分析

    這篇文章主要為大家介紹了elasticsearch分布式及數(shù)據(jù)功能源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • Spring?MVC?URL地址映射的示例代碼

    Spring?MVC?URL地址映射的示例代碼

    @RequestMapping是一個用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。,這篇文章主要介紹了Spring?MVC?URL地址映射,需要的朋友可以參考下
    2022-07-07
  • 攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解

    攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解

    這篇文章主要為大家介紹了攔截Druid數(shù)據(jù)源自動注入帳密解密實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • JDBC的擴(kuò)展知識點總結(jié)

    JDBC的擴(kuò)展知識點總結(jié)

    這篇文章主要介紹了JDBC的擴(kuò)展知識點總結(jié),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)JDBC的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • IDEA2022搭建Spring?Cloud多模塊項目的詳細(xì)過程

    IDEA2022搭建Spring?Cloud多模塊項目的詳細(xì)過程

    這篇文章主要介紹了IDEA2022搭建Spring?Cloud多模塊項目,網(wǎng)上有很多教程父模塊都是通過maven的方式創(chuàng)建的,然后子模塊是通過Spring?Initalizr方式創(chuàng)建,這種方式父模塊無法管理子模塊的依賴仲裁,需要每個子模塊自行管理,就失去了父模塊的用處了
    2022-10-10

最新評論