azure - Azure 上的 Redis 连接断开

标签 azure docker redis node-redis

我的 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/

相关文章:

Azure DevOps -> 管道 -> 库 -> 访问 Azure Key Vault -> Key Vault 不允许从所有网络访问

azure - 清理 Service Fabric 群集上的旧 Docker 镜像

node.js - 在react和express项目中使用docker compose up后出错

java - 如何使用JAVA在Redis中进行批量插入?

c# - Azure Functions - Host.json 不适用于 Azure 应用程序设置

amazon-web-services - Dockerfile 从 amazon s3 或其他需要凭据的来源复制文件

docker - 覆盖的docker入口点的参数

kubernetes - Kubernetes 上的 Redis 主/从设置抛出错误 : BRPOPLPUSH { ReplyError: MOVED 2651

ruby-on-rails - 在 Redis 中使用特定模式获取键范围的最佳方法

azure - Azure 表存储中的事务