Spring集成Redis集羣

1、依賴介紹

一、Spring支持Redis集羣且可設置集羣節點密碼
node

        <!-- redis:支持集羣&節點密碼 -->
        <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.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>

        注意:須要spring-beans設置版本爲4.2.8.RELEASE,即不徹底支持spring3.x版本redis

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>

二、Spring支持Redis集羣,但沒法支持節點密碼
spring

         <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.1.RELEASE</version>
        </dependency>
 

 

2、配置文件緩存

一、redis-cluster.properties服務器

#密碼
redis.password=666666
#緩存池最小空閒數
redis.minIdle=5
#緩存池最大空閒數
redis.maxIdle=100
#緩存池最大鏈接數
redis.maxTotal=300
#最大等待時間
redis.maxWaitMillis=3000
#客戶端超時時間單位是毫秒
redis.timeout=100000
redis.maxTotal=1000
redis.minIdle=8
#明是否在從池中取出鏈接前進行檢驗,若是檢驗失敗,則從池中去除鏈接並嘗試取出另外一個
redis.testOnBorrow=true

#rediscluster
#本地虛擬機 Redis
spring.redis.cluster.nodes=192.168.74.129:7000,192.168.74.129:7001,192.168.74.129:7002,192.168.74.129:7003,192.168.74.129:7004,192.168.74.129:7005
spring.redis.cluster.max-redirects=3
#rediscluster

二、spring-redis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd         ">     <!-- 引入配置文件 -->       <bean id="propertyConfigurer"             class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">           <property name="location" value="classpath:redis-cluster.properties" />       </bean>         <!-- jedis 配置-->       <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >           <!--最大空閒數-->           <property name="maxIdle" value="${redis.maxIdle}" />           <!--最大創建鏈接等待時間-->           <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />           <!--是否在從池中取出鏈接前進行檢驗,若是檢驗失敗,則從池中去除鏈接並嘗試取出另外一個-->           <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-cluster.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 >   </beans>
相關文章
相關標籤/搜索