Spring Framework中的Redis设置

时间:2018-07-15 09:48:13

标签: java spring redis

我们正在使用Redis v3.2.100 Windows版本在Spring Framework项目中进行缓存。 Redis相关的依赖项:

<dependency>
     <groupId>org.springframework.data</groupId>
     <artifactId>spring-data-redis</artifactId>
     <version>1.6.6.RELEASE</version>
</dependency>
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-pool2</artifactId>
     <version>2.4.2</version>
</dependency>
<dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.8.0</version>
</dependency>

和配置:

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" p:max-total="400" p:maxIdle="350" p:maxWaitMillis="1000"
         p:test-on-borrow="true" p:test-on-return="true" p:testOnCreate="true" />

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     p:host-name="127.0.0.1" p:port="6379" p:use-pool="true" p:password="11223344">
   <constructor-arg ref="jedisPoolConfig"/>
</bean>

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
     p:connection-factory-ref="jedisConnectionFactory" p:enable-transaction-support="true"/>

还有Kotlin中的用法示例:

@Resource(name = "redisTemplate")
private val redisLongKeyStrValueHashOps: HashOperations<String, Long, String>? = null

...
{
    ...
    redisLongKeyStrValueHashOps!!.get("RepoName", 111L).toString()
    ...
}

但是有一个问题,有时Spring Application无法与Redis一起使用,并且与Redis的服务器连接丢失。 如果此时检查Redis的状态,则连接计数约为1600,如果重新启动Spring Application,连接计数将返回零!一切都会好起来!

3 个答案:

答案 0 :(得分:2)

在我看来,您的连接池已耗尽,

也许您可以尝试直接自动连接redisTemplate,然后将execute方法用于回调,例如:

template.execute(new SessionCallback<List<Object>>() {
 //...
});

template.execute(new RedisCallback<Void>() {
// ...
}

请参阅此question以获取更多信息

答案 1 :(得分:0)

lettuce与单个持久连接一起使用。这是线程安全的。唯一的问题是,如果由于某种原因损坏了连接,您需要能够重新创建该连接。

答案 2 :(得分:0)

这听起来像是您的空闲连接的问题。我看到您的 maxIdle 是350,对于单个Redis服务器和单个Redis客户端,通常应该在10-60之间。此外,您的配置中没有其他用于空闲连接的内容。

尝试以下配置:

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- max connections -->
        <property name="maxTotal" value="30" />
        <!-- max idle connections -->
        <property name="maxIdle" value="10" />
        <!-- max released connections each time -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- time interval of releasing connection scan (ms) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- min connection idle time -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- time interval to release for idle connection, 
        when the number of  idle connection is bigger than 'maxIdle' and reaches this time
        it would be realsed inmediately-->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- max waiting time of getting connection, less than zero means uncertain time, default -1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- test connection work when get connection, default false -->
        <property name="testOnBorrow" value="true" />
        <!-- test idle connection work, default false -->
        <property name="testWhileIdle" value="true" />
        <!-- if it is blocked when connection is exhausted, false throws exception, true blocked until timeout, default true-->
        <property name="blockWhenExhausted" value="false" />
    </bean>