1壶冒、問(wèn)題描述
項(xiàng)目用的是芋道的微服務(wù)架構(gòu)富俄,版本是1.7.1禁炒,引用的redisson 是 3.18.0,啟動(dòng)四個(gè)微服務(wù)時(shí)沒(méi)問(wèn)題霍比,啟動(dòng)第五個(gè)的時(shí)候就報(bào)錯(cuò)幕袱。
還有一個(gè)問(wèn)題:就是 redis 服務(wù)一直占用 20% 左右的 CPU,這又是什么原因悠瞬?
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
3、原因分析
網(wǎng)上很多人都說(shuō)是連接池?cái)?shù)量不夠的原因浅妆,可是查看最大的客戶端連接數(shù)是 3168 望迎,這才連上106個(gè),咋就不夠用了呢凌外?我也試著改最大連接數(shù)辩尊,但是一直沒(méi)改成功,但我覺(jué)得不是這個(gè)的問(wèn)題趴乡,因?yàn)轫?xiàng)目本地啟動(dòng)对省,根本沒(méi)有那么多的連接。
再用 redis 的 monitor 命令晾捏,查看發(fā)現(xiàn)有很多的 ping 命令蒿涎,或許是因?yàn)?ping 命令過(guò)多導(dǎo)致的呢?
我看了下 redisson 包里面的基本配置惦辛,發(fā)現(xiàn)是 30s 間隔劳秋,也就是說(shuō)每個(gè)連接 30s 都會(huì)發(fā)送一個(gè)ping 命令,網(wǎng)上我看有文章說(shuō)修改這個(gè) 間隔 ,我就試了下 config.useSingleServer().setPingConnectionInterval(0); 也就是不發(fā)送 ping 命令玻淑,然后啟動(dòng)嗽冒,就可以啟動(dòng)了。
@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() {
// 此為單機(jī)模式
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
config.useSingleServer().setPingConnectionInterval(0); // 防止出現(xiàn) redis 連接不上的問(wèn)題
return (Redisson) Redisson.create(config);
}
}
至于redis 為什么占用 這么這么多的 CPU 資源补履,目前還沒(méi)找到原因添坊。