spring-data-redis 哨兵配置例子

时间:2023-01-27 20:37:04

spring-data-redis  哨兵配置例子

spring 自带的哨兵确实简化了高可用性的配置,使用起来也比较简单。

首先是spring-redis-sentinel.xml(文件名可以随意命名)配置文件:

<?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">

<!-- Spring自动将该包目录下标记为@Service的所有类作为spring的Bean -->
<context:component-scan base-package="com.zz.redis" />

<!-- <context:property-placeholder location="classpath:conf/redis/redis.properties" /> -->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="2048" />
<property name="maxIdle" value="200" />
<property name="numTestsPerEvictionRun" value="1024" />
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<property name="minEvictableIdleTimeMillis" value="-1" />
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<property name="maxWaitMillis" value="1500" />
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="testOnReturn" value="false" />
<property name="jmxEnabled" value="true" />
<property name="blockWhenExhausted" value="false" />
</bean>

<bean id="redisSentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster">
</property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.125.128" />
<constructor-arg name="port" value="26379" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.125.129" />
<constructor-arg name="port" value="26379" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode ">
<constructor-arg name="host" value="192.168.125.130" />
<constructor-arg name="port" value="26379" />
</bean>
</set>
</property>
</bean>
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:password="pwdisadmin">
<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
</bean>
</beans>

上面的p:password="pwdisadmin"指的是redis的密码,mymaster指的是redis哨兵中配置的名字。

然后再程序中就可以通过注解或者getBean获取redisTemplate的实例了。

public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:conf/spring-redis-sentinel.xml");
RedisTemplate<String, String> template = (RedisTemplate<String, String>) context.getBean("redisTemplate");
template.opsForValue().set("aaa", "aaabbb");
System.err.println(template.opsForValue().get("aaa"));
}

在配置中我们只需要指定哨兵的配置就可以了。在这个里面指定好哨兵后,哨兵会自动发redis的主的redis server的

配置好后,启动项目日志打印如下(例子如下:)

一月 19, 2017 4:33:35 下午 redis.clients.jedis.JedisSentinelPool initSentinels
信息: Trying to find master from available Sentinels...
一月 19, 2017 4:33:35 下午 redis.clients.jedis.JedisSentinelPool initSentinels
信息: Redis master running at 10.10.39.104:16379, starting Sentinel listeners...
一月 19, 2017 4:33:35 下午 redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 10.10.39.104:16379
一个配置sentinel的例子
sentinel.conf 
================================


port 26379
dir "/letv/redis-sentinel/tmp"
//monitor master 10.120.16.45 6379
sentinel monitor shoppingcar1 10.120.16.45 6379 1
sentinel down-after-milliseconds shoppingcar1 3000
sentinel failover-timeout shoppingcar1 5000
sentinel auth-pass mycart1 passwordabc


sentinel config-epoch mycart1 1554
sentinel leader-epoch mycart1 1975

//monitor slave 10.120.16.61 16379
sentinel known-slave mycart1 10.120.16.61 16379

//other sentinels
sentinel known-sentinel mycart1 10.120.16.161 26379 12345abc
sentinel known-sentinel mycart1 10.120.16.61 26379 12345abc
sentinel known-sentinel mycart1 10.120.16.184 26379 12345abc


sentinel current-epoch 1975