使用spring+Jedis配置
1.使用到的版本
spring3.1+redis2.4.5
jar包:
jedis-2.4.2.jar
spring-data-redis-1.4.2.RELEASE.jar
commons-pool2-2.4.2.jar
2.报错问题
2.1:Caused by: java.lang.NoSuchMethodError: redis.clients.jedis.JedisShardInfo.setTimeout(I)V
原因是之前用的jedis2.7.2.jar版本太高了,换成低版本jedis-2.4.2.jar。
2.2:Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'maxActive' of bean class [redis.clients.jedis.JedisPoolConfig]:
需要换成maxTotal
2.3:Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'maxWait' of bean class [redis.clients.jedis.JedisPoolConfig]
需要换成maxWaitMillis
之前的版本配置
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
maxActive 修改成 maxTotal, maxWait修改成maxWaitMillis.
<!-- jedis pool配置 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- spring data redis -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.timeout}" />
<!-- <property name="database" value="${redis.default.db}"></property> -->
<constructor-arg index="0" ref="jedisPool" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="userDao" class="com.www.common.redis.UserDao" />
redis.properties
#redis的服务器地址
redis.host=10.13.17.33
#redis的服务端口
redis.port=6379
#密码
redis.pass=
#链接数据库
redis.default.db=0
#客户端超时时间单位是毫秒
redis.timeout=100000
#最大连接数
redis.maxActive=300
#最大空闲数
redis.maxIdle=100
#最大建立连接等待时间
redis.maxWait=1000
redis.testOnBorrow=true
#指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
#DBSync.testOnBorrow=true
测试效果: