服务治理 Java学习笔记SpringFramework

spring-data-redis有关哨兵配置

2017-01-19  本文已影响1180人  吴世浩

一、浩言

回顾过去没有什么意义,我情愿向前看
----------------------------《成为乔布斯》


二、背景

最近几天一直在弄redis的使用,并且在数据使用的过程中熟悉相关属性,redis的哨兵配置好了,也就是高可用配置好了,但是在代码里面怎么连接了,那么如下就说明下了。


配置文件配置

由于我是配置好了,所以才发现,在配置中我们只需要指定哨兵的配置就好,在这个里面指定好哨兵后,哨兵会自动发redis的主的,配置好后,启动项目日志打印如下

一月 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

xml里的配置如下

    <context:property-placeholder location="classpath:sys_redis.properties" />  
    
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="${redis.pool.maxIdle}" />  
        <property name="maxTotal" value="${redis.pool.maxActive}" />  
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </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="mymaster"></property>
            </bean>
        </property>
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="10.10.39.104"/>
                    <constructor-arg name="port" value="26379"/>
                </bean>
            </set>
        </property>
    </bean>  
      
     <!-- redis连接中指定哨兵 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
        <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"/>
        <constructor-arg name="poolConfig" ref="poolConfig"/>
        <!-- <property name="password" value="123456"></property> -->
    </bean>
      
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
        <property name="connectionFactory"   ref="connectionFactory" /> 
    </bean>  

在spring-data-redis的文档中对于Sentinel的说明如下图

Paste_Image.png

四:注意点

4.1 对于代码里面在xml的配置,我们需要指定哨兵的地址。


Sentinel.png

4.2 我在主上配置了密码,我原本以为在xml里面也要配置密码,但是后来发现并不用,我在Sentinel的配置属性里面也找了下并没有发现密码这个属性。


五:浩语

                                           __                                                        
                            __  _  ____ __|  |__ _____    ___
                            \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                             \     /|  |  /   Y  \/ __ \(  <_> )
                              \/\_/ |____/|___|  (____  /\____/ 
                                                    \/     \/          
                                 任何事情都是要靠努力和用心。                                                   
上一篇 下一篇

猜你喜欢

热点阅读