spring-redis.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd "
default-autowire="byName">
<util:properties id="configPropsForRedis" location="classpath:redis.properties" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF8" />
<property name="cacheSeconds" value="5" />
<property name="basenames">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<!--<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> -->
<!--<property name="maxTotal" value="#{configPropsForRedis['redis.pool.maxTotal']}" /> -->
<!--<property name="maxIdle" value="#{configPropsForRedis['redis.pool.maxIdle']}" />-->
<!--<property name="minIdle" value="#{configPropsForRedis['redis.pool.minIdle']}"/>-->
<!--<property name="maxWaitMillis" value="#{configPropsForRedis['redis.pool.maxWaitMillis']}" /> -->
<!--<property name="testOnBorrow" value="#{configPropsForRedis['redis.pool.testOnBorrow']}" />-->
<!--<property name="testOnReturn" value=" #{configPropsForRedis['redis.pool.testOnReturn']}" />-->
<!--<property name="testWhileIdle" value=" #{configPropsForRedis['redis.pool.testWhileIdle']}" />-->
<!--<property name="numTestsPerEvictionRun" value=" #{configPropsForRedis['redis.pool.numTestsPerEvictionRun']}" />-->
<!--<property name="timeBetweenEvictionRunsMillis" value=" #{configPropsForRedis['redis.pool.timeBetweenEvictionRunsMillis']}" />-->
<!--<property name="jmxEnabled" value=" #{configPropsForRedis['redis.pool.jmxEnabled']}" />-->
<!--</bean>-->
<!--<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> -->
<!--<property name="hostName" value="#{configPropsForRedis['redis.hostname']}" /> -->
<!--<property name="port" value="#{configPropsForRedis['redis.port']}" /> -->
<!--<property name="password" value="#{configPropsForRedis['redis.password']}" /> -->
<!--<property name="poolConfig" ref="jedisPoolConfig" />-->
<!--<!– usePool:是否使用连接池 –>-->
<!--<property name="usePool" value="true"/>-->
<!--</bean> -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisClusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
<!-- Redis数据库索引(默认为0) -->
<property name="database" value="${spring.redis.database}"/>
<property name="password" value="${spring.redis.password}"></property>
</bean>
<!-- 集群配置 -->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="clusterNodes">
<set>
<ref bean="clusterRedisNodes1"/>
<ref bean="clusterRedisNodes2"/>
<ref bean="clusterRedisNodes3"/>
<ref bean="clusterRedisNodes4"/>
<ref bean="clusterRedisNodes5"/>
<ref bean="clusterRedisNodes6"/>
</set>
</property>
<property name="maxRedirects" value="${spring.redis.maxRedirects}" />
</bean>
<!-- 集群节点 -->
<bean id="clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${spring.redis.cluster.nodes1.host}" />
<constructor-arg value="${spring.redis.cluster.nodes1.port}" type="int" />
</bean>
<bean id="clusterRedisNodes2" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${spring.redis.cluster.nodes2.host}" />
<constructor-arg value="${spring.redis.cluster.nodes2.port}" type="int" />
</bean>
<bean id="clusterRedisNodes3" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${spring.redis.cluster.nodes3.host}" />
<constructor-arg value="${spring.redis.cluster.nodes3.port}" type="int" />
</bean>
<bean id="clusterRedisNodes4" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${spring.redis.cluster.nodes4.host}" />
<constructor-arg value="${spring.redis.cluster.nodes4.port}" type="int" />
</bean>
<bean id="clusterRedisNodes5" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${spring.redis.cluster.nodes5.host}" />
<constructor-arg value="${spring.redis.cluster.nodes5.port}" type="int" />
</bean>
<bean id="clusterRedisNodes6" class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${spring.redis.cluster.nodes6.host}" />
<constructor-arg value="${spring.redis.cluster.nodes6.port}" type="int" />
</bean>
<!-- 集群节点 -->
<!-- redis集群结束 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${spring.redis.pool.max-active}" />
<property name="maxIdle" value="${spring.redis.pool.max-idle}" />
<property name="minIdle" value="${spring.redis.pool.min-idle}" />
<property name="maxWaitMillis" value="${spring.redis.pool.max-wait}" />
<property name="testOnBorrow" value="true" />
</bean>
<!--redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
<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="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
</property>
<!--<property name="enableTransactionSupport" value="true"></property>-->
</bean>
<bean id="springRedisService" class="com.aspirecn.sys.service.utils.CacheOperationService">
<property name="redisTemplate" ref="redisTemplate"/>
</bean>
</beans>
redis.properties
###redis集群推送任务信息缓存
spring.redis.cluster.nodes1.host=192.168.7.171
spring.redis.cluster.nodes1.port=16385
spring.redis.cluster.nodes2.host=192.168.7.171
spring.redis.cluster.nodes2.port=16382
spring.redis.cluster.nodes3.host=192.168.7.171
spring.redis.cluster.nodes3.port=16383
spring.redis.cluster.nodes4.host=192.168.7.171
spring.redis.cluster.nodes4.port=16384
spring.redis.cluster.nodes5.host=192.168.7.171
spring.redis.cluster.nodes5.port=16380
spring.redis.cluster.nodes6.host=192.168.7.171
spring.redis.cluster.nodes6.port=16386
spring.redis.password=cloudhis123321
## Redis数据库索引(默认为0)
spring.redis.database=0
## 连接超时时间(毫秒)
spring.redis.timeout=60000
## 最大重试次数
spring.redis.maxRedirects=3
## 连接池最大连接数(使用负值表示没有限制)如果是集群就是每个ip的连接数
spring.redis.pool.max-active=1000
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=100
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=20
pom依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
<scope>compile</scope>
</dependency>