redisson分布式锁:Redis分布式锁报错,Redisson出错:Unable to send command!

最近在生产环境发现一个问题,使用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);
    }

  

打包重启后,一切恢复正常了

 

posted @ 2021-06-17 17:25  穆晟铭  阅读(6956)  评论(0编辑  收藏  举报