最近在生產環境發現一個問題,使用redisson-3.9.1分布鎖,來做加減庫存,測試環境啥問題,一道生產問題就暴露了。
系統異常:Redis server response timeout (10000 ms) occured for command:
(EVAL) with params: [if (redis.call('exists', KEYS[1]) == 0) then redis.call('hset', KEYS[1], ARGV[2], 1);
redis.call('pe..., 1, SDA-LOCK--CX-21, 30000, 41acfe35-4442-4b14-962b-0c2d88018c26:104] channel:
[id: 0x314f9ca5, L:/192.168.2.215:53820 - R:172.16.0.161/172.16.0.161:6579]
redisson的分布式鎖發現錯誤:org.redisson.client.WriteRedisConnectionException: Unable to send command!,就是分布式鎖的命令無法執行,導致許多業務都出現問題。
看了錯誤堆棧:
就知道關閉通道的時候出問題,但是具體什么原因不知道,搜索引擎老師也不知所措
最終,在redisson的github倉庫的issue中找到了答案: The connection not reconnect #1811
剛好我們使用的redisson版本也是3.9.1,這個issue發生提到的錯誤,問題的出現基本和我遇到的一致,並且在Fixed - connection is not reconnected #1811中解決了,所以,這樣子,升級!搞定!
問題總結
問題的主要原因是:在redis出問題之后,watchdog發現連接無效之后,然后打印了一個警告日志之后,就沒法有自動重連了,導致繼續使用該連接的時候出問題,問題解決,ConnectionWatchdog.channelInactive.tryReconnect方法:

解決辦法就是升級redisson.從3.9.1升級到3.11.3.
在升級后又發現第二個問題:
在部署一段時間(幾分鍾或者1個小時左右)報超時異常(org.redisson.client.RedisTimeoutException: Redis server response timeout (3000 ms) occured for command)
錯誤原因: 客戶端長時間未使用,服務端會斷開
解決辦法: redisson添加配置
#連接間隔 心跳 pingConnectionInterval: 1000
我是直接用RedissonConfig.java來寫配置的,所有代碼中添加了:
singleServerConfig.setPingConnectionInterval(1000);
/**
* RedissonClient,單機模式
*
* @return
*/
@Bean(destroyMethod = "shutdown")
public RedissonClient redisson() {
Config config = new Config();
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setAddress("redis://" + host + ":" + port);
singleServerConfig.setTimeout(timeout);
singleServerConfig.setDatabase(database);
if (password != null && !"".equals(password)) { //有密碼
singleServerConfig.setPassword(password);
}
//心跳
singleServerConfig.setPingConnectionInterval(1000);
return Redisson.create(config);
}
打包重啟后,一切恢復正常了
