Java Spring框架中的Redis设置

Java Spring框架中的Redis设置,java,spring,redis,Java,Spring,Redis,我们在Spring框架项目中使用Redis v3.2.100 windows版本进行缓存。与Redis相关的依赖项: <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.6.RELEASE</version> &

我们在Spring框架项目中使用Redis v3.2.100 windows版本进行缓存。与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>

org.springframework.data
spring数据redis
1.6.6.1发布
org.apache.commons
commons-pool2
2.4.2
redis.clients
绝地武士
2.8.0
和配置:

<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()
    ...
}
@Resource(name=“redisTemplate”)
私有val redisLongKeyStrValueHashOps:HashOperations?=无效的
...
{
...
redisLongKeyStrValueHashOps!!.get(“RepoName”,111L).toString()
...
}
但有一个问题,有时Spring应用程序无法与Redis一起工作,服务器与Redis的连接丢失。
如果我们当时检查Redis的状态,连接计数大约是1600,如果我们重新启动Spring应用程序,连接计数将返回到零!一切都会好起来的

我觉得你的连接池快用完了

可能您可以直接尝试自动连接
redisTemplate
,然后使用
execute
方法进行回调,如:

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

template.execute(new RedisCallback()){
// ...
}

请参阅此以了解更多信息

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

听起来像是空闲连接的问题。我看到您的maxIdle是350,通常应该是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>


你是否运行多个应用实例?将redis模板连接到HashOperations对我来说很奇怪。这是怎么回事?@Mr.Arjun-大多数情况下只有一个实例在运行。但是服务器的输入请求计数和与redis的交互计数很高。@wI.GIG-每秒到我的服务器的请求计数很高,并且每一个大约是400毫秒。在处理一个请求的过程中,我与Redis server进行了多次交互。根据此信息,您想对建议的配置进行任何更改吗?
    <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>