Spring+redis sentinel 主从切换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qincidong/article/details/76079023

Spring+redis sentinel 主从切换

使用redis的sentinel可以避免redis单点故障,下面说一下spring使用redis sentinel模式的配置。

sentinel模式的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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:redis.properties"/>
    <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.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实例的端口。这里的例子只使用了一个sentinel.

测试代码:

@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。

猜你喜欢

转载自blog.csdn.net/qincidong/article/details/76079023