我的 redis 实例在 Azure VM 上的 docker 容器中运行。我从我的本地应用程序和我的 Azure 应用程序连接到同一个实例。两者都有完全相同的代码库,但如果我尝试运行我的 Azure 应用程序,在这两个环境上经过一段时间的空闲时间后,它就会变得无响应。如果我检查 redis 日志,它只显示 2 个客户端(其中 1 个是 cli 本身)。所以我看到我与 Azure 的 redis 连接已结束,但在我的 Azure 应用程序日志中没有记录连接结束。我正在使用 node_redis 从 APP 连接到 redis。我正在使用如下重试策略,它在 node_redis 上作为示例给出。
retry_strategy: function (options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with
// a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands
// with a individual error
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.min(options.attempt * 100, 3000);
}
我是否遗漏了任何需要完成的 Azure 端配置?
最佳答案
最后我找到了解决方案,我希望没有其他人陷入这种情况。 在以下链接的帮助下,我能够确定问题所在。 https://serverfault.com/questions/661704/azure-closing-idle-network-connections
解决方法: 将 redis.conf 中的 tcp_keepalive 设置为小于 240 使用 redis-cli 的命令:
CONFIG SET tcp_keepalive 200
并检查设置
CONFIG GET tcp_keepalive
由于 Azure 服务器不会完全关闭连接,而只会更改连接映射,因此我们的客户端不会获得结束事件或触发 retry_strategy。
关于azure - Azure 上的 Redis 连接断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50755433/