amazon-web-services - AWS Redis : JedisConnectionException: java.net.SocketException:打开的文件太多

标签 amazon-web-services redis tomcat8 jedis

我已经使用 AWS Elasticache 配置了 redis,并通过安装在 AWS EC2 上的 tomcat 连接了它。

以下是我的代码:

private JedisPool jedisPool = null;

@PostConstruct
private void initialiseJedisPools() {
    try {
        String redisHost = RESOURCES.getProperty("redis.master.host");
        if(Objects.nonNull(redisHost)) {
            Integer redisPort = Integer.valueOf(RESOURCES.getProperty("redis.master.port"));
            jedisPool = new JedisPool(redisHost, redisPort);
        }
    } catch (NumberFormatException exception) {
        logger.error("Exception occurred while initialising jedis pool.", exception);
    }
}

public void addKey(String key, String value, Integer secondsToExpire) {
    if (Objects.nonNull(jedisPool)) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key, value);
            if (Objects.nonNull(secondsToExpire)) {
                jedis.expire(key, secondsToExpire.intValue());
            }
        } catch (JedisException jedisException) {
            logger.error("Exception thrown while adding key in cache.", jedisException);
        }
    }
}

经常出现以下错误,我必须重新启动 tomcat 才能使其正常工作。

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
        at redis.clients.util.Pool.getResource(Pool.java:53)
        at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
        .
        .
        .
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Too many open files
        at redis.clients.jedis.Connection.connect(Connection.java:207)
        at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
        at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
        at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
        at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
        at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
        at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
        at redis.clients.util.Pool.getResource(Pool.java:49)
        ... 10 more
Caused by: java.net.SocketException: Too many open files
        at java.net.Socket.createImpl(Socket.java:460)
        at java.net.Socket.getImpl(Socket.java:520)
        at java.net.Socket.setReuseAddress(Socket.java:1449)
        at redis.clients.jedis.Connection.connect(Connection.java:174)
        ... 17 more

我尝试增加打开文件的ulimit,为maxTotalmaxIdle配置JedisPoolConfigminIdle 等,但没有成功。

请提出建议。

最佳答案

我们过去也遇到过类似的问题,jedis 连接错误“java.net.SocketException:打开的文件太多”

发生这种情况是因为 jedis 连接没有关闭。

call jedisPool.returnResourse(jedis) or jedis.close()

关于amazon-web-services - AWS Redis : JedisConnectionException: java.net.SocketException:打开的文件太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51173256/

相关文章:

database - Redis变慢的原因

sql - 如何使 Amazon Redshift 中的 TO_DATE 在同一列中使用多种日期格式?

node.js - 如何配置我的 Nodejs 应用程序以仅响应 SSL 连接?

objective-c - hredis 与 NSDictionary 的使用

database-design - NoSQL(Redis)设计建议

linux - shared.loader="${catalina.home}/shared/classes 不能在 LINUX 上运行,但可以在 WINDOWS 上运行

mysql - 将 .sql 文件导入 mySQL 没有效果

Python lambda 函数返回 KeyError

java - 基于偏移量的 ZoneId 的 ID 无效 - Java 8

tomcat - 在 tomcat 上下文中使用 mapperDirectoryRedirectEnabled = true 和 mapperContextRootRedirectEnabled ="true"有什么副作用