记录一次 redis 连接不上的bug(Unable to init enough connections amount! Only 20 of 24 were initialized.)

1、问题描述

项目用的是芋道的微服务架构,版本是1.7.1,引用的redisson 是 3.18.0,启动四个微服务时没问题,启动第五个的时候就报错。
还有一个问题:就是 redis 服务一直占用 20% 左右的 CPU,这又是什么原因?


image.png

2、bug详情

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redisson' threw exception; nested exception is org.redisson.client.RedisConnectionException: Unable to init enough connections amount! Only 20 of 24 were initialized. Redis server: 127.0.0.1/127.0.0.1:6379
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
    ... 107 common frames omitted
Caused by: org.redisson.client.RedisConnectionException: Unable to init enough connections amount! Only 20 of 24 were initialized. Redis server: 127.0.0.1/127.0.0.1:6379

Caused by: java.util.concurrent.CompletionException: org.redisson.client.RedisTimeoutException: Command execution timeout for command: (PING), params: [], Redis client: [addr=redis://127.0.0.1:6379]
    at java.util.concurrent.CompletableFuture.encodeRelay(CompletableFuture.java:326)
    at java.util.concurrent.CompletableFuture.completeRelay(CompletableFuture.java:338)
    at java.util.concurrent.CompletableFuture.uniRelay(CompletableFuture.java:911)
    at java.util.concurrent.CompletableFuture$UniRelay.tryFire(CompletableFuture.java:899)
    ... 12 common frames omitted
Caused by: org.redisson.client.RedisTimeoutException: Command execution timeout for command: (PING), params: [], Redis client: [addr=redis://127.0.0.1:6379]
    at org.redisson.client.RedisConnection.lambda$async$0(RedisConnection.java:244)
    at io.netty.util.HashedWheelTimer$HashedWheelTimeout.run(HashedWheelTimer.java:715)
    at io.netty.util.concurrent.ImmediateExecutor.execute(ImmediateExecutor.java:34)
    at io.netty.util.HashedWheelTimer$HashedWheelTimeout.expire(HashedWheelTimer.java:703)
    at io.netty.util.HashedWheelTimer$HashedWheelBucket.expireTimeouts(HashedWheelTimer.java:790)
    at io.netty.util.HashedWheelTimer$Worker.run(HashedWheelTimer.java:503)
    ... 2 common frames omitted
image.png

3、原因分析

网上很多人都说是连接池数量不够的原因,可是查看最大的客户端连接数是 3168 ,这才连上106个,咋就不够用了呢?
image.png

我也试着改最大连接数,但是一直没改成功,但我觉得不是这个的问题,因为项目本地启动,根本没有那么多的连接。

再用 redis 的 monitor 命令,查看发现有很多的 ping 命令,或许是因为 ping 命令过多导致的呢?
我看了下 redisson 包里面的基本配置,发现是 30s 间隔,也就是说每个连接 30s 都会发送一个ping 命令,网上我看有文章说修改这个 间隔 ,我就试了下 config.useSingleServer().setPingConnectionInterval(0); 也就是不发送 ping 命令,然后启动,就可以启动了。

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private Integer port;
    @Value("${spring.redis.database}")
    private Integer database;

    @Bean(destroyMethod = "shutdown")
    public Redisson redisson() {
        // 此为单机模式
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
        config.useSingleServer().setPingConnectionInterval(0);  // 防止出现 redis 连接不上的问题
        return (Redisson) Redisson.create(config);
    }
}

至于redis 为什么占用 这么这么多的 CPU 资源,目前还没找到原因。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容