redis叢集 與spring-data-redis 整合

來源:互聯網
上載者:User

標籤:ati   core   wait   節點配置   class   word   cti   bsp   方法   

 

所遇到的坑:必須使用如下的jedis 版本與spring-data-redis 版本,才能夠達到叢集效果 。1.7版本以前是不支援叢集的

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

 ***********叢集配置**********

   <bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="${redis.maxTotal}" />        <property name="maxIdle" value="${redis.maxIdle}" />        <property name="maxWaitMillis" value="${redis.maxWait}" />        <property name="testOnBorrow" value="${redis.testOnBorrow}" />    </bean>                <!-- 配置redis 節點 -->    <bean id="redisClusterConfiguration"    class="org.springframework.data.redis.connection.RedisClusterConfiguration">    <property name="maxRedirects" value="3"></property>    <!-- 節點配置 -->    <property name="clusterNodes">        <set>            <bean class="org.springframework.data.redis.connection.RedisClusterNode">                <constructor-arg name="host" value="192.168.241.129 "></constructor-arg>                <constructor-arg name="port" value="9006"></constructor-arg>            </bean>            <bean class="org.springframework.data.redis.connection.RedisClusterNode">                <constructor-arg name="host" value="192.168.241.129 "></constructor-arg>                <constructor-arg name="port" value="9001"></constructor-arg>            </bean>            <bean class="org.springframework.data.redis.connection.RedisClusterNode">                <constructor-arg name="host" value="192.168.241.129 "></constructor-arg>                <constructor-arg name="port" value="9002"></constructor-arg>            </bean>            <bean class="org.springframework.data.redis.connection.RedisClusterNode">                <constructor-arg name="host" value="192.168.241.129"></constructor-arg>                <constructor-arg name="port" value="9003"></constructor-arg>            </bean>            <bean class="org.springframework.data.redis.connection.RedisClusterNode">                <constructor-arg name="host" value="192.168.241.129"></constructor-arg>                <constructor-arg name="port" value="9004"></constructor-arg>            </bean>            <bean class="org.springframework.data.redis.connection.RedisClusterNode">                <constructor-arg name="host" value="192.168.241.129"></constructor-arg>                <constructor-arg name="port" value="9005"></constructor-arg>            </bean>        </set>    </property></bean>        <bean id="jedisConnectionFactory"    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">    <constructor-arg ref="redisClusterConfiguration" />    <constructor-arg ref="redisPoolConfig" /></bean>           <!-- 儲存序列化 --> <bean id="stringRedisSerializer"       class="org.springframework.data.redis.serializer.StringRedisSerializer" />                       <!-- redis 訪問的模版 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">    <property name="connectionFactory" ref="jedisConnectionFactory" />    <!-- 添加如下序列化配置解決key亂碼問題以及令keys()方法生效 -->    <property name="keySerializer" ref="stringRedisSerializer"/>    <property name="hashKeySerializer" ref="stringRedisSerializer"/></bean> 

  

**********單機版配置*******

 

   <bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="${redis.maxTotal}" />        <property name="maxIdle" value="${redis.maxIdle}" />        <property name="maxWaitMillis" value="${redis.maxWait}" />        <property name="testOnBorrow" value="${redis.testOnBorrow}" />    </bean>    <!-- 單機版配置 -->    <!-- 配置JedisConnectionFactory -->    <bean id="jedisConnectionFactory"        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <property name="hostName" value="127.0.0.1" />        <property name="port" value="6379" />        <!-- <property name="password" value="${redis.pass}" /> -->        <property name="database" value="1" />        <property name="poolConfig" ref="redisPoolConfig" />    </bean>           <!-- 儲存序列化 --> <bean id="stringRedisSerializer"       class="org.springframework.data.redis.serializer.StringRedisSerializer" />                       <!-- redis 訪問的模版 --><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">    <property name="connectionFactory" ref="jedisConnectionFactory" />    <!-- 添加如下序列化配置解決key亂碼問題以及令keys()方法生效 -->    <property name="keySerializer" ref="stringRedisSerializer"/>    <property name="hashKeySerializer" ref="stringRedisSerializer"/></bean> 

 

 

 

在java 代碼中引入即可

@Autowiredprivate RedisTemplate template;

  

redis叢集 與spring-data-redis 整合

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.