When performing performance optimizations on Redis, you always want to read and write the Redis. However, due to the use of Spring-data-redis on the bottom of the project to operate Redis, referring to the spring official website found that Spring-data-redis currently (1.7.0.RELEASE) and previous versions do not support read and write separation.
First, source code analysis
The configuration for jedisconnectionfactory in Spring-data-redis is as follows:
<?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"> <beanclass="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> <beanclass="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> <beanclass="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> <beanclass="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> <!--connection Pool configuration-<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-argref="redissentinelconfiguration"/> </bean> <bean id="stringredistemplate" class="org.springframework.data.redis.core.StringRedisTemplate"p:connection-factory-ref="jedisconnectionfactory"/></beans>
See Jedisconnectionfactory source found pool is pool<jedis> instead of pool<shardedjedis>. So I guess spring data redis is not able to read and write separation, stringredistemplate read and write operations are on the master.
Second, read and write separation transformation
Refer to Sentinel's primary and standby election mechanism to transform the Spring-data-redis configuration as follows:
(1) Spring Configuration
<?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"> <beanclass="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 Read/write separation