Spring+redis sentinel 主从切换

「爱情、让人受尽委屈。」 2022-05-12 06:18 298阅读 0赞

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

redis配置文件

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

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

测试代码

  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 条评论,298人围观)

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

相关阅读

    相关 mysql 主从切换

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