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>