spring redis 集羣配置

pom引入jar包

<!-- redis start -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.6.RELEASE</version>
        </dependency>
        <!-- redis end -->

spring 配置文件

<context:property-placeholder
        location="classpath:redis.properties"
        ignore-unresolvable="true" />
        <!-- redis集羣開始 -->
    <!-- string redis template definition -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>

    <!-- redis template definition -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <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>

    <!-- Spring-redis鏈接池管理工廠 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="redisClusterConfiguration" />
        <constructor-arg ref="jedisPoolConfig" />
        <!-- Redis數據庫索引(默認爲0) -->
        <property name="database" value="${spring.redis.database}"/>
    </bean>

    <!-- 集羣配置 -->
    <bean id="redisClusterConfiguration"   class="org.springframework.data.redis.connection.RedisClusterConfiguration">
       <property name="clusterNodes"> 
              <set>
                       <ref bean="clusterRedisNodes1"/>
                       <ref bean="clusterRedisNodes2"/>
                       <ref bean="clusterRedisNodes3"/>
                       <ref bean="clusterRedisNodes4"/>
                       <ref bean="clusterRedisNodes5"/>
                       <ref bean="clusterRedisNodes6"/>
              </set>
       </property>
       <property name="maxRedirects" value="${spring.redis.maxRedirects}" />
    </bean>
    <!-- 集羣節點 -->
     <bean id="clusterRedisNodes1"   class="org.springframework.data.redis.connection.RedisNode"> 
           <constructor-arg value="${spring.redis.cluster.nodes1.host}" />
           <constructor-arg value="${spring.redis.cluster.nodes1.port}" type="int" />
     </bean>
     <bean id="clusterRedisNodes2"   class="org.springframework.data.redis.connection.RedisNode"> 
           <constructor-arg value="${spring.redis.cluster.nodes2.host}" />
           <constructor-arg value="${spring.redis.cluster.nodes2.port}" type="int" />
     </bean>
     <bean id="clusterRedisNodes3"   class="org.springframework.data.redis.connection.RedisNode"> 
           <constructor-arg value="${spring.redis.cluster.nodes3.host}" />
           <constructor-arg value="${spring.redis.cluster.nodes3.port}" type="int" />
     </bean>
     <bean id="clusterRedisNodes4"   class="org.springframework.data.redis.connection.RedisNode"> 
           <constructor-arg value="${spring.redis.cluster.nodes4.host}" />
           <constructor-arg value="${spring.redis.cluster.nodes4.port}" type="int" />
     </bean>
     <bean id="clusterRedisNodes5"   class="org.springframework.data.redis.connection.RedisNode"> 
           <constructor-arg value="${spring.redis.cluster.nodes5.host}" />
           <constructor-arg value="${spring.redis.cluster.nodes5.port}" type="int" />
     </bean>
     <bean id="clusterRedisNodes6"   class="org.springframework.data.redis.connection.RedisNode"> 
           <constructor-arg value="${spring.redis.cluster.nodes6.host}" />
           <constructor-arg value="${spring.redis.cluster.nodes6.port}" type="int" />
     </bean>
     <!-- 集羣節點 -->
    <!-- redis集羣結束 -->

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${spring.redis.pool.max-active}" />
        <property name="maxIdle" value="${spring.redis.pool.max-idle}" />
        <property name="minIdle" value="${spring.redis.pool.min-idle}" />
        <property name="maxWaitMillis" value="${spring.redis.pool.max-wait}" />
        <property name="testOnBorrow" value="true" />
    </bean>

配置文件

redis.propertiesnode

###redis集羣推送任務信息緩存
spring.redis.cluster.nodes1.host=127.0.0.1
spring.redis.cluster.nodes1.port=6380
spring.redis.cluster.nodes2.host=127.0.0.1
spring.redis.cluster.nodes2.port=6381
spring.redis.cluster.nodes3.host=127.0.0.1
spring.redis.cluster.nodes3.port=6382
spring.redis.cluster.nodes4.host=127.0.0.1
spring.redis.cluster.nodes4.port=6390
spring.redis.cluster.nodes5.host=127.0.0.1
spring.redis.cluster.nodes5.port=6390
spring.redis.cluster.nodes6.host=127.0.0.1
spring.redis.cluster.nodes6.port=6390
## Redis數據庫索引(默認爲0) 
spring.redis.database=0
## 鏈接超時時間(毫秒) 
spring.redis.timeout=60000
## 最大重試次數
spring.redis.maxRedirects=3
## 鏈接池最大鏈接數(使用負值表示沒有限制)若是是集羣就是每一個ip的鏈接數 
spring.redis.pool.max-active=300
## 鏈接池最大阻塞等待時間(使用負值表示沒有限制) 
spring.redis.pool.max-wait=-1
## 鏈接池中的最大空閒鏈接 
spring.redis.pool.max-idle=100
## 鏈接池中的最小空閒鏈接 
spring.redis.pool.min-idle=20

使用方法

@Autowired
    @Resource(name="stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;
相關文章
相關標籤/搜索