spring-data-redis 哨兵配置例子

本文介绍如何使用Spring Data Redis配置哨兵实现高可用性。通过XML配置文件设置Jedis连接池参数及哨兵节点信息,实现Redis集群的自动故障转移。示例代码展示了如何初始化并使用RedisTemplate进行数据读写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值