`

spring redis sentinel(哨兵)部署

 
阅读更多

简介:

        实现reids master slave 模式添加哨兵(sentinel)监控,当主机down掉后备机承担主机身份进行生产工作。

 

实施:

       1.先依照spring redis 整合http://see-you-again.iteye.com/admin/blogs/2323435完成非哨兵监控模式下的配置。

       2.修改pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<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"
       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">

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

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"
          p:maxIdle="${redis.maxIdle}"
          p:maxTotal="${redis.maxActive}"
          p:maxWaitMillis="${redis.maxWait}"
          p:testOnBorrow="${redis.testOnBorrow}">
    </bean>
    <!-- 不使用哨兵
        <bean id="jedisConnFactory"
              class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
              p:usePool="true"
              p:hostName="127.0.0.1"
              p:port="7000"
              p:timeout="200"
              p:password=""
              p:poolConfig-ref="poolConfig"/>
         -->
        <bean id="jedisConnFactory"
              class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration"></constructor-arg>
            <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
        </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>
                <!-- 注意这里配置的是哨兵信息,而非redis服务信息-->
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg index="0" value="127.0.0.1" />
                    <constructor-arg index="1" value="5001" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg index="0" value="127.0.0.1" />
                    <constructor-arg index="1" value="5002" />
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg index="0" value="127.0.0.1" />
                    <constructor-arg index="1" value="5003" />
                </bean>               
            </set>
        </property>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" >
        <property name="connectionFactory" 	ref="jedisConnFactory" />
        <property name="enableTransactionSupport" value="true" />
    </bean>

    <bean id="userDao" class="com.test.redis.UserDao" >
        <constructor-arg name="redisTemplate" ref="redisTemplate"/>
    </bean>

    <bean id="redisDao" class="com.test.redis.RedisDao" >
        <constructor-arg name="redisTemplate" ref="redisTemplate"/>
    </bean>
</beans>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics