Spring+SpringMVC做Redis集群(Sentinel模式)-spring配置文件

<?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:cache="http://www.springframework.org/schema/cache"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
            ">

<!-- 配置Redis客户端  start --> 

     <!--  --> 
<!--     <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> -->
<!--         <property name="hostName" value="${redis.host}" /> -->
<!--         <property name="port" value="${redis.port}" />     -->
<!--         <property name="timeout" value="1000" /> -->
<!--         <property name="usePool" value="true" /> -->
<!--     </bean> -->
     <!--  --> 
<!--     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">   -->
<!--            <property name="connectionFactory" ref="jedisConnectionFactory" /> -->
<!--         <property name="keySerializer"> -->
<!--             <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> -->
<!--         </property> -->
<!--         <property name="valueSerializer"> -->
<!--             <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> -->
<!--         </property> -->
<!--     </bean> -->
<!--      -->
<!--     <bean id="redisCacheClient" class="com.iotlab.fw.cache.impl.RedisCacheClient">   -->
<!--            <property name="redisTemplate" ref="redisTemplate" /> -->
<!--     </bean> -->
<!--     Redis config end -->

    <!-- redis集群配置 哨兵模式begin -->
    <!-- 启动缓存注解功能,否则缓解不会生效 -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    <!-- redis属性配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
           <property name="maxTotal" value="${redis.pool.maxTotal}" />
           <property name="maxIdle" value="${redis.pool.maxIdle}" />
           <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
           <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
           <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
           <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
           <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
           <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>
    <!-- redis集群配置 哨兵模式begin -->
    <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <property name="master">
            <bean class="org.springframework.data.redis.connection.RedisNode">
                <!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的-->
                <property name="name" value="mymaster"></property>
            </bean>
        </property>
        <!--指定Sentinel的IP和端口,不是Master和Slave的-->
        <property name="sentinels">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.sentinel.host1}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.sentinel.port1}"></constructor-arg>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisNode">
                    <constructor-arg name="host" value="${redis.sentinel.host2}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.sentinel.port2}"></constructor-arg>
                </bean>
            </set>
        </property>
    </bean>
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></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"></property>
        <property name="keySerializer"> 
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>
    <!-- 缓存管理器 -->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="redisTemplate" />
    </bean>
    <bean id="redisCacheClient" class="com.iotlab.fw.cache.impl.RedisCacheClient">   
            <property name="redisTemplate" ref="redisTemplate" /> 
    </bean>
    <!-- redis集群配置 哨兵模式begin -->

</beans>

配置完后测试:

java客户端Jedis在2.2.2及以上版本实现了对Sentinel的支持,只要是通过命令:
redis-cli -h 192.168.110.71 -p 26379  sentinel get-master-addr-by-name mymaster
1) "192.168.110.71"
2) "6379"

说明配置成功!

猜你喜欢

转载自blog.csdn.net/F_Clover/article/details/82111448