Redis RedisCluster Spring整合

前言:

在上一篇文章中,用了jedisCluster來做redis集羣的客戶端,這一篇我們介紹一下spring-data-redis給我們提供的操作redis集羣的redisTemplate,着急用的可以跳過1,直接看2

準備工作:

jdk版本:1.8

junit版本:4.12

jar包版本:

    <!-- jedis Java接口 -->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
      <type>jar</type>
    </dependency>
    <!-- Spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.3.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.7.4.RELEASE</version>
    </dependency>

1:RedisTemplate由來簡介

在網上沒有找到redisTemplate操作redis集羣的例子,所以只能自己動手,在這裏簡單說一下過程.首先既然redisTemplate依賴jedis,那我們可以認爲他內部操作的就是jedis,同理,我們也可以認爲他內部也能操作jedisCluster.接下來就在spring-data-redis的源碼裏面搜一下jedisCluster這個字符串,發現JedisClusterConnection和JedisConnectionFactory中出現了jedisCluster,有沒有覺得JedisConnectionFactory很眼熟呢,對,就是配置文件中redisTemplate初始化時候需要用到的連接工廠.現在就可以直接看JedisConnectionFactory
首先,我們來看JedisConnectionFactory,發現裏面有一個屬性就是jedisCluster,那就看看jedisCluster是如何被初始化的,看下圖:


我們可以先看這個方法,這個方法是從InitializingBean中實現的方法,是spring初始化bean的時候需要調用的.所以可以假裝認爲只要實例化了JedisConnectionFactory就可以實例化jedisCluster,但是不要忘了有一個條件,那就是clusterConfig不能爲空,接下來我們找clusterConfig是如何被實例化的.發現JedisConnectionFactory有一個構造函數

JedisConnectionFactory(RedisClusterConfiguration clusterConfig, JedisPoolConfig poolConfig).這下就好辦了JedisPoolConfig我們本來就認識,RedisClusterConfiguration是需要我們實例化的,接下來就看看RedisClusterConfiguration,一進來RedisClusterConfiguration我們就能看到個好東西,見下圖:



我們可以看RedisClusterConfiguration的註釋,雖然沒有說明,但是光看格式,就大概能猜到這些東西應該是寫到properties文件裏面的,而構造函數的參數又正好是propertySource,很容易就能聯想到ResourcePropertySource,接下來就簡單了,直接開始開幹了.

2:redisClusterConfig配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:redis.properties" />
    </bean>

    <!-- jedis 配置-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >
        <!--最大空閒數-->
        <property name="maxIdle" value="${redis.maxIdle}" />
        <!--最大建立連接等待時間-->
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <!--是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個-->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean >

    <!--配置文件加載-->
    <bean id="resourcePropertySource" class="org.springframework.core.io.support.ResourcePropertySource">
        <constructor-arg name="name" value="redis.properties"/>
        <constructor-arg name="resource" value="classpath:redis.properties"/>
    </bean>
    <!--redisCluster配置-->
    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <constructor-arg name="propertySource" ref="resourcePropertySource"/>
    </bean>
    <!-- redis服務器中心 -->
    <bean id="connectionFactory"  class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
       <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
        <constructor-arg name="poolConfig" ref="poolConfig"/>
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}" ></property>
    </bean >
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
        <property name="connectionFactory" ref="connectionFactory" />
        <!--如果不配置Serializer,那麼存儲的時候缺省使用String,如果用User類型存儲,那麼會提示錯誤User can't cast to String!!  -->
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer" >
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean >

    <bean id="redisHttpSessionConfiguration"
          class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
       <!--超時時間,默認1800秒-->
        <property name="maxInactiveIntervalInSeconds" value="1800" />
    </bean>

</beans>

3:redis.properties配置文件

#redis中心
#redis的服務器地址
redis.host=127.0.0.1
#redis的服務端口
redis.port=6379
#密碼
redis.password=
#最大空閒數
redis.maxIdle=100
#最大連接數
redis.maxActive=300
#最大建立連接等待時間
redis.maxWait=1000
#客戶端超時時間單位是毫秒
redis.timeout=100000
redis.maxTotal=1000
redis.minIdle=8
#明是否在從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試取出另一個
redis.testOnBorrow=true

#sentinel
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.node1.host=127.0.0.1
spring.redis.sentinel.node2.host=127.0.0.1
spring.redis.sentinel.node3.host=127.0.0.1
spring.redis.sentinel.node1.port=26379
spring.redis.sentinel.node2.port=26479
spring.redis.sentinel.node3.port=26579
#sentinel

#jediscluster
cluster1.host.port=127.0.0.1:7000
cluster2.host.port=127.0.0.1:7001
cluster3.host.port=127.0.0.1:7002
cluster4.host.port=127.0.0.1:7003
cluster5.host.port=127.0.0.1:7004
cluster6.host.port=127.0.0.1:7005
cluster7.host.port=127.0.0.1:7006
cluster8.host.port=127.0.0.1:7007
#jediscluster

#rediscluster
spring.redis.cluster.nodes=127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006,127.0.0.1:7007
spring.redis.cluster.max-redirects=3
#rediscluster

4:直接注入就可以使用,測試通過

發佈了32 篇原創文章 · 獲贊 21 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章