Spring+redis sentinel 主从切换

待我称王封你为后i 2022-06-10 22:45 230阅读 0赞

Spring+redis sentinel 主从切换

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

sentinel模式的spring文件配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <context:property-placeholder location="classpath:redis.properties"/>
  4. <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  5. <property name="maxIdle" value="${redis.maxIdle}"/>
  6. <property name="maxTotal" value="${redis.maxActive}"/>
  7. <property name="maxWaitMillis" value="${redis.maxWait}"/>
  8. <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
  9. </bean>
  10. <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  11. <property name="usePool" value="true"></property>
  12. <property name="password" value="${redis.sentinel.password}"/>
  13. <property name="timeout" value="10000"/>
  14. <property name="database" value="0"></property>
  15. <constructor-arg index="0" ref="sentinelConfiguration"/>
  16. <constructor-arg index="1" ref="poolConfig"/>
  17. </bean>
  18. <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
  19. <property name="master">
  20. <bean class="org.springframework.data.redis.connection.RedisNode">
  21. <property name="name" value="${redis.sentinel.master}"></property>
  22. </bean>
  23. </property>
  24. <property name="sentinels">
  25. <set>
  26. <bean class="org.springframework.data.redis.connection.RedisNode">
  27. <constructor-arg name="host" value="${redis.sentinel1.host}"></constructor-arg>
  28. <constructor-arg name="port" value="${redis.sentinel1.port}"></constructor-arg>
  29. </bean>
  30. </set>
  31. </property>
  32. </bean>
  33. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
  34. <property name="connectionFactory" ref="jedisConnectionFactory"/>
  35. </bean>
  36. </beans>

redis.properties:

  1. redis.maxIdle=5
  2. redis.maxActive=10
  3. redis.maxWait=1000
  4. redis.testOnBorrow=true
  5. redis.sentinel.master=mymaster
  6. redis.sentinel.password=system
  7. redis.sentinel1.host=192.168.10.237
  8. redis.sentinel1.port=26379

注意:上面的端口是sentinel的端口,不是redis实例的端口。这里的例子只使用了一个sentinel.

测试代码:

  1. @Test
  2. public void testSentinelBySpring() {
  3. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_redis_sentinel.xml");
  4. StringRedisTemplate redisTemplate = ctx.getBean(StringRedisTemplate.class);
  5. Collection<RedisServer> redisServers = redisTemplate.getConnectionFactory().getSentinelConnection().masters();
  6. System.out.println(redisServers);
  7. String key = "test";
  8. String value = redisTemplate.opsForValue().get(key);
  9. System.out.println(value);
  10. redisServers = redisTemplate.getConnectionFactory().getSentinelConnection().masters();
  11. System.out.println(redisServers);
  12. redisTemplate.opsForValue().set(key,"New Master...");
  13. value = redisTemplate.opsForValue().get(key);
  14. System.out.println(value);
  15. }

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。

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysql 主从切换

    最终结果:主库可写,从库只读 首先保证主从数据库都开启二进制日志,方法是在my.cnf中的\[mysqld\]节中加入log-bin=log-bin-name 然