Spring + redis集群的集成

4 篇文章 0 订阅
2 篇文章 0 订阅

Spring + redis集群的集成

JAR需求

spring-data-redis-1.8.1.RELEASE.jar

jedis-2.9.0.jar

spring-data-commons-1.8.1.RELEASE.jar

commons-pool2-2.4.2.jar

使用spring-data-redis做spring的集成时,需要使用1.7.0以上的版本。

spring-data-redis 从1.7.0 之后的版本开始支持redis集群。

jedis-2.8.0.jar 对redis 的连接池的管理存在链接泄露的问题,需要使用2.9.0以上的版本。

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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
       default-lazy-init="true">
    <context:annotation-config/>

    <!-- Activates scanning of @Repository and @Service -->
    <context:component-scan base-package="cn.com.cewell"/>
    <!-- Redis -->
    <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <property name="maxRedirects" value="3"/>
<!--        <property name="clusterTimeOut"  value="10000" />-->
        <property name="clusterNodes">
            <set>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="10.0.7.68"/>
                    <constructor-arg name="port" value="6379"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="10.0.7.68"/>
                    <constructor-arg name="port" value="6382"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="10.0.7.69"/>
                    <constructor-arg name="port" value="6380"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="10.0.7.69"/>
                    <constructor-arg name="port" value="6383"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="10.0.7.70"/>
                    <constructor-arg name="port" value="6381"/>
                </bean>
                <bean class="org.springframework.data.redis.connection.RedisClusterNode">
                    <constructor-arg name="host" value="10.0.7.70"/>
                    <constructor-arg name="port" value="6384"/>
                </bean>
            </set>
        </property>

    </bean>
    <bean id="pool" class="redis.clients.jedis.JedisPoolConfig">
        <!--最大能够保持idle的数量,控制一个pool最多有多少个状态为idle的jedis实例,默认值8-->
        <property name="maxIdle" value="1000"/>
        <!--在指定时刻通过pool能够获取到的最大的连接的jedis个数,默认值8-->
        <property name="maxTotal" value="10000"/>
        <property name="maxWaitMillis" value="15000"/>
    </bean>
    <!--注入配置 JedisConnectionFactory 和 StringRedisTemplate -->
    <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="database" value="4"/>
        <property name="timeout" value="20000"/>
        <constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
        <constructor-arg name="poolConfig" ref="pool"/>
        <!--<property name="password" value="012_345^678-90"/>--><!--修改密码 和集群一致-->
    </bean>
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <constructor-arg name="connectionFactory" ref="jedisConnFactory"/>
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <property name="hashKeySerializer" ref="stringRedisSerializer"/>
    </bean>

    <bean id="stringRedisSerializer"
          class="org.springframework.data.redis.serializer.StringRedisSerializer" />

    <!-- 对应的spring-data-redis和jedis的jar包版本需要修改-->
</beans>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值