前几天接手一个ssm工程需要使用redis集群,配置的过程中踩过几个坑,最终整合成功了,所以将这次配置redis集群的经验分享出来,如果你也在这掉坑了也希望能够帮你从这个坑中拉出来;
pom文件中添加所需要的依赖:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.9.RELEASE</version>
<!--去除Lettuce-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Redis 连接工具 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.1</version>
</dependency>
该工程使用的spring版本是 4.2.5.RELEASE
application.properties文件中添加redis的配置:
#Redis database settings
#集群模式下,集群最大转发的数量
redis.maxRedirects=3
#server IP
redis.host1=172.25.5.54
redis.port1=6310
redis.host2=172.25.5.54
redis.port2=6311
redis.host3=172.25.5.54
redis.port3=6312
#server pass
redis.pass=
#use dbIndex
redis.database=0
#连接池中的最大空闲连接
redis.maxIdle=8
#连接池中的最小空闲连接
redis.minIdle=0
#连接池最大阻塞等待时间(使用负值表示没有限制)
redis.maxWaitMillis=-1
#连接池最大连接数(使用负值表示没有限制)
redis.maxTotal=50
#在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的
redis.testOnBorrow=true
spring-context-redis.xml文件中添加redis的连接配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--集群配置结点-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration" >
<property name="maxRedirects" value="${redis.maxRedirects}" />
<property name="clusterNodes">
<set>
<bean id="node1" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host1}"/>
<constructor-arg name="port" value="${redis.port1}"/>
</bean>
<bean id="node2" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host2}"/>
<constructor-arg name="port" value="${redis.port2}"/>
</bean>
<bean id="node3" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host3}"/>
<constructor-arg name="port" value="${redis.port3}"/>
</bean>
</set>
</property>
</bean>
<!--连接池-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大连接数-->
<property name="maxTotal" value="${redis.maxTotal}"/>
<!--最大空闲数-->
<property name="maxIdle" value="${redis.maxWait}"/>
<!--最小空闲数-->
<property name="minIdle" value="${redis.minIdle}"/>
<!--连接池最大阻塞等待时间-->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<!--下面这几个属性也可以不配置-->
<property name="blockWhenExhausted" value="false"/>
<property name="numTestsPerEvictionRun" value="1024"/>
<property name="testOnBorrow" value="true"/>
<property name="softMinEvictableIdleTimeMillis" value="10000"/>
<property name="timeBetweenEvictionRunsMillis" value="30000"/>
<property name="testWhileIdle" value="true"/>
</bean>
<!--连接工厂-->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
<constructor-arg name="poolConfig" ref="poolConfig"/>
</bean>
<!--模板-->
<!--序列化为字符串-->
<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!--序列化为字节码-->
<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!--序列化为json-->
<bean id="genericJackson2JsonRedisSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!--设置连接工厂-->
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<!--设置序列化-->
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="genericJackson2JsonRedisSerializer"/>
<property name="hashKeySerializer" ref="genericJackson2JsonRedisSerializer"/>
<property name="hashValueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
</beans>
需要使用的话直接在类中注入RedisTemplate就可以
以上版本和配置,亲测可用!!!