我在 tomcat 网络应用程序中使用 Jedis 连接到 Elascticache Redis 节点。该应用程序在白天被数百名用户使用。我不确定这是否正常,但每当我使用 cloudwatch 指标检查当前连接数时,我都会看到当前连接在增加而没有下降。
这是我的 Jedis 池配置:
public static JedisPool getPool(){
if(pool == null){
JedisPoolConfig config = new JedisPoolConfig();
config.setMinIdle(5);
config.setMaxIdle(35);
config.setMaxTotal(1500);
config.setMaxWaitMillis(3000);
config.setTestOnBorrow(true);
config.setTestWhileIdle(true);
pool = new JedisPool(config, PropertiesManager.getInstance().getRedisServer());
}
return pool;
}
这就是我总是使用池连接来执行 redis 命令的方式:
Jedis jedis = JedisUtil.getPool().getResource();
try{
//Redis commands
}
catch(JedisException e){
e.printStackTrace();
throw e;
}finally{
if (jedis != null) JedisUtil.getPool().returnResource(jedis);
}
使用此配置,计数目前超过 200。我是否遗漏了一些应该丢弃或终止未使用的连接的东西?我将 maxIdle 设置为 35,我预计当流量非常低时计数会下降到 35,但这从未发生过。
最佳答案
我们遇到了同样的问题。在进一步调查之后,我们发现了这个(来自 redis 官方文档 - http://redis.io/topics/clients):
By default recent versions of Redis don't close the connection with the client if the client is idle for many seconds: the connection will remain open forever.
默认情况下,aws 提供的超时值为 0。因此,即使您的客户端初始化的连接已关闭,redis 仍会保留任何已与您的 redis 实例初始化的连接。
在 aws 中创建一个新的缓存参数策略,超时时间不为 0 然后你就可以了
关于java - 我的 Elasticache Redis 节点中的当前连接数不断增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28573529/