spring-data-redis讀寫分離

來源:互聯網
上載者:User

標籤:encoding   XML   讀寫分離   har   work   idle   cto   1.0   參考   

   在對Redis進行效能最佳化時,一直想對Redis進行讀寫分離。但由於項目底層採用spring-data-redis對redis進行操作,參考spring官網卻發現spring-data-redis目前(1.7.0.RELEASE)及以前的版本並不支援讀寫分離。

 一、源碼分析

  spring-data-redis中關於JedisConnectionFactory的配置如下:

<?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-4.1.xsd">            <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">        <property name="master">            <bean class="org.springframework.data.redis.connection.RedisNode">                <property name="name" value="mymaster"/>                <constructor-arg name="host" value="${redis.master.host}"></constructor-arg>                <constructor-arg name="port" value="${redis.master.port}"></constructor-arg>            </bean>        </property>        <property name="sentinels">            <set>                <bean class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="${redis.sentinel1.host}"></constructor-arg>                    <constructor-arg name="port" value="${redis.sentinel1.port}"></constructor-arg>                </bean>                <bean class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="${redis.sentinel2.host}"></constructor-arg>                    <constructor-arg name="port" value="${redis.sentinel2.port}"></constructor-arg>                </bean>                <bean class="org.springframework.data.redis.connection.RedisNode">                    <constructor-arg name="host" value="${redis.sentinel3.host}"></constructor-arg>                    <constructor-arg name="port" value="${redis.sentinel3.port}"></constructor-arg>                </bean>            </set>        </property>    </bean>        <!-- 串連池配置 -->    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="${redis.pool.maxActive}" />        <property name="maxIdle" value="${redis.pool.maxIdle}" />        <!-- <property name="maxWait" value="${redis.pool.maxWait}" /> -->        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />        <property name="testOnReturn" value="${redis.pool.testOnReturn}" />    </bean>        <bean id="jedisConnectionFactory"          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <property name="poolConfig" ref="jedisPoolConfig"></property>          <constructor-arg ref="redisSentinelConfiguration"/>    </bean>    <bean id="stringRedisTemplate"           class="org.springframework.data.redis.core.StringRedisTemplate"           p:connection-factory-ref="jedisConnectionFactory"/></beans>

  查看JedisConnectionFactory源碼發現pool是Pool<Jedis>,而不是Pool<ShardedJedis>。因此我猜目前spring data redis是做不了讀寫分離的,stringRedisTemplate讀寫操作都是在master上。

二、讀寫分離改造

  參考sentinel的主備選舉機制對spring-data-redis的相關配置進行如下改造:

 (1)Spring配置

<?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-4.1.xsd">               <context:property-placeholder  location="classpath:redis/redis.properties" ignore-unresolvable="true" />              <bean id="poolConfig" class="redis.client.jedis.JedisPoolConfig">            <property name="maxIdle" value="${redis.maxIdle}" />            <property name="maxTotal" value="${redis.maxTotal}" />            <property name="maxWaitMillis" value="${redis.maxWait}" />            <property name="testOnBuorrow" value="${redis.testOnBorrow}" />        </bean>    <bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">     <constructor-arg index="0">          <bean class="org.springframework.core.env.MapPropertySource">            <constructor-arg index="0" value="RedisSentinelConffiguration" />            <constructor-arg index="1">               <map>                <entry key="spring.redis.sentinel.master" value="${redis.sentinel.master}"></entry>                <entry key="spring.redis.sentinel.nodes" value="${redis.sentinel.nodes"}> </entry>                </map>             </constructor-arg>             </bean>            </constructor-arg>    </bean>        <bean id="connectionFactory" class="com.test.data.redis.connection.TWJedisConnectionFactory">       <constructor-arg index="0" ref="redisSentinelConfiguration" />           <constructor-arg index="1" ref="poolConfig" />           <property name="password" value="${redis.pass}" />           <property name="databse" value="7" />    </bean>    <bean id="redisTemplate" class="com.test.data.redis.core.TWRedisTemplate"           <property name="connectionFactory" ref="connectionFactory"/>        </bean></beans>  

 

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.