1:单redis模式下
properties文件 配制
#JedisPoolConfig的参数 #最大连接数 redis.pool.maxTotal=30 #最大空闲时间 redis.pool.maxIdle=10 #每次最大连接数 redis.pool.numTestsPerEvictionRun=1024 #释放扫描的扫描间隔 redis.pool.timeBetweenEvictionRunsMillis=30000 #连接的最小空闲时间 redis.pool.minEvictableIdleTimeMillis=1800000 #连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 redis.pool.softMinEvictableIdleTimeMillis=10000 #获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 redis.pool.maxWaitMillis=1500 #在获得链接的时候检查有效性,默认false redis.pool.testOnBorrow=true #在空闲时检查有效性,默认false redis.pool.testWhileIdle=true #连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true redis.pool.blockWhenExhausted=false #JedisConnectionFactory的参数 #主机地址,默认:localhost redis.hostName=192.168.200.128 #主机端口,默认:6379 redis.port=6379 #超时时间,默认:2000 redis.timeout=3000 #密码 #redis.password #是否使用连接池,默认true redis.usePool=true #使用数据库的索引,0-15之间的数字,默认:0 redis.dbIndex=0 #是否使用数据类型的转换,默认:true #redis.convertPipelineAndTxResults #哨兵配置 #redis.sentinelConfig #集群配置 #redis.clusterConfig
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:redis.propertiesvalue>
list>
property>
bean>
<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}"/>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.hostName}"/>
<property name="port" value="${redis.port}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="database" value="${redis.dbIndex}"/>
<property name="usePool" value="${redis.usePool}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
bean>
beans>
2:集群模式下
properties文件 配制
#JedisPoolConfig的参数 #最大连接数 redis.pool.maxTotal=30 #最大空闲时间 redis.pool.maxIdle=10 #每次最大连接数 redis.pool.numTestsPerEvictionRun=1024 #释放扫描的扫描间隔 redis.pool.timeBetweenEvictionRunsMillis=30000 #连接的最小空闲时间 redis.pool.minEvictableIdleTimeMillis=1800000 #连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 redis.pool.softMinEvictableIdleTimeMillis=10000 #获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 redis.pool.maxWaitMillis=1500 #在获得链接的时候检查有效性,默认false redis.pool.testOnBorrow=true #在空闲时检查有效性,默认false redis.pool.testWhileIdle=true #连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true redis.pool.blockWhenExhausted=false #RedisClusterConfiguration配置 redis.maxRedirects=5 #主机和端口号 redis.host1=192.168.200.128 redis.port1=7000 redis.host2=192.168.200.128 redis.port2=7001 redis.host3=192.168.200.128 redis.port3=7002 redis.host4=192.168.200.128 redis.port4=7003 redis.host5=192.168.200.128 redis.port5=7004 redis.host6=192.168.200.128 redis.port6=7005
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:redis-cluster.propertiesvalue>
list>
property>
bean>
<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}"/>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
bean>
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}">property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host1}"/>
<constructor-arg name="port" value="${redis.port1}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host2}"/>
<constructor-arg name="port" value="${redis.port2}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host3}"/>
<constructor-arg name="port" value="${redis.port3}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host4}"/>
<constructor-arg name="port" value="${redis.port4}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host5}"/>
<constructor-arg name="port" value="${redis.port5}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host6}"/>
<constructor-arg name="port" value="${redis.port6}"/>
bean>
set>
property>
bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="poolConfig" ref="jedisPoolConfig"/>
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
bean>
beans>