Spring+redis sentinel 主从切换(failover)
redis sentinel配置参考:http://www.cnblogs.com/yjmyzz/p/redis-sentinel-sample.html
redis sentinel与spring的集成参考:http://www.cnblogs.com/yjmyzz/p/integrate-redis-with-spring.html
redis对象的缓存也参考上面的文章。

sentinel模式的spring文件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<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配置文件

1
2
3
4
5
6
7
8
9
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

注意:上面的端口是sentinel的端口,不是redis实例的端口。

测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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);
}

jedisConnFactory中配置的是master的ip和端口,端口不是sentinel的端口,是redis实例的端口(在redis.conf中配置的)。
redisSentinelConfiguration的sentinels属性配置的是哨兵,用于监控master,在确认master宕掉后根据一定的算法从哨兵中拿一个提升为master。

说下使用的Jar的版本:
spring:3.2.10.RELEASE
jedis:2.5.2
spring-data-redis:1.4.1.RELEASE
可能还依赖其他的一些jar,比如jackson。