java - 我的 Elasticache Redis 节点中的当前连接数不断增加

标签 java tomcat redis jedis amazon-elasticache

我在 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/

相关文章:

java - 定义一个以接口(interface)作为值的映射,并在同一接口(interface)内声明

java - 如何设置 Jersey + Tomcat RESTful Java 网络应用程序?

javascript - 如何在Java应用中阻止XSS攻击,阻止用户在网页上执行JavaScript?

c++ - Redis命令ERR:参数数量不正确hiredis

caching - Azure Redis 缓存 - GET 调用超时

redis - 在Redis中设计多对多关系

java - 我如何在 libgdx 的 res 中的字符串 xml 文件中获取定义的字符串?

java - 一个事务中的 Realm 循环 VS 每个步进循环一个事务

运行程序时出现java.lang.NullPointerException

java - 无法初始化类 com.amazonaws.services.sqs.AmazonSQSClient