Spring+redis sentinel master-slave switch (failover)
redis sentinel configuration reference: http://www.cnblogs.com/yjmyzz/p/redis-sentinel-sample.html
Reference for the integration of redis sentinel and spring: http://www.cnblogs.com/yjmyzz/p/integrate-redis-with-spring.html
For redis object caching, please refer to the above article.
spring file configuration in sentinel mode
<context:property-placeholder location="classpath:redis.properties"/>
<beans>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxActive}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="password" value="${redis.sentinel.password}"/>
<property name="timeout" value="10000"/>
<property name="database" value="0"></property>
<constructor-arg index="0" ref="sentinelConfiguration"/>
<constructor-arg index="1" ref="poolConfig"/>
</bean>
<bean id="sentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="${redis.sentinel.master}"></property>
</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>
</set>
</property>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
</bean>
</beans>
redis profile
redis.properties:
redis.maxIdle=5
redis.maxActive=10
redis.maxWait=1000
redis.testOnBorrow=true
redis.sentinel.master=mymaster
redis.sentinel.password=system
redis.sentinel1.host=192.168.10.237
redis.sentinel1.port=26379
Note: the above port is the sentinel port, not the redis instance port.
Test code
@Test
public void testSentinelBySpring() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_redis_sentinel.xml");
StringRedisTemplate redisTemplate = ctx.getBean(StringRedisTemplate.class);
Collection<RedisServer> redisServers = redisTemplate.getConnectionFactory().getSentinelConnection().masters();
System.out.println(redisServers);
String key = "test";
String value = redisTemplate.opsForValue().get(key);
System.out.println(value);
redisServers = redisTemplate.getConnectionFactory().getSentinelConnection().masters();
System.out.println(redisServers);
redisTemplate.opsForValue().set(key,"New Master...");
value = redisTemplate.opsForValue().get(key);
System.out.println(value);
}
Jedisconfactory is configured with the ip and port of the master. The port is not the sentinel port, but the port of the redis instance (configured in redis.conf).
Sentinels property of redisSentinelConfiguration configures sentinels to monitor the master. After confirming that the master is down, one of the Sentinels is promoted to master according to certain algorithm.
Say the version of Jar used:
spring:3.2.10.RELEASE
jedis:2.5.2
spring-data-redis:1.4.1.RELEASE
You may also rely on other jar s, such as jackson.