首页 > 解决方案 > 无法从谷歌云运行连接到 redis 公牛

问题描述

我正在尝试在 Google Cloud 运行上设置我的 redis 公牛。在本地一切正常。

服务器启动时我使用以下代码:

const client = redis.createClient('6379', '10.103.YYY.YYY');

这似乎奏效了。我在启动时没有收到任何错误。

当我尝试与 Bull 开展工作时,例如:

             const actionQueue = new Bull(uuid(), {
                redis: {
                    port: 6379, host: '10.103.YYY.YYY', tls: {
                        servername: '10.103.YYY.YYY'
                    }
                }
            });
         
            await actionQueue.add();

            actionQueue.process(async (job) => {
                return this._job();
            });

            actionQueue.on('completed', async (job, actionId) => {
                console.log(`Job completed with result ${actionId}`);
            });

            actionQueue.on("failed", (job, error) => {
                console.log(job.id, error);
            });

但是redis仍然连接到localhost ip&port。有人知道为什么这仍然连接到我的本地主机吗?我需要以不同的方式设置吗?

错误日志:

2020-11-21T14:55:35.794783Z <rejected> Error: connect ECONNREFUSED 127.0.0.1:6379
Standaard
2020-11-21T14:55:35.794791Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) {
Standaard
2020-11-21T14:55:35.794798Z errno: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.794804Z code: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.794810Z syscall: 'connect',
Standaard
2020-11-21T14:55:35.794816Z address: '127.0.0.1',
Standaard
2020-11-21T14:55:35.794822Z port: 6379
Standaard
2020-11-21T14:55:35.794828Z }
Standaard
2020-11-21T14:55:35.794834Z}
Standaard
2020-11-21T14:55:35.794987Z The error was: Error: connect ECONNREFUSED 127.0.0.1:6379
Standaard
2020-11-21T14:55:35.794994Z at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1145:16) {
Standaard
2020-11-21T14:55:35.795Z errno: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.795006Z code: 'ECONNREFUSED',
Standaard
2020-11-21T14:55:35.795011Z syscall: 'connect',
Standaard
2020-11-21T14:55:35.795017Z address: '127.0.0.1',
Standaard
2020-11-21T14:55:35.795022Z port: 6379
Standaard
2020-11-21T14:55:35.795028Z}

标签: node.jsredisgoogle-cloud-run

解决方案


Cloud Run 本身不与 VPC 对话。您需要在没有项目 VPC 的情况下连接无服务器世界。

为此,您必须使用无服务器 VPC 连接器。在部署 Cloud Run 的同一区域中创建。

然后将其附加到您的 Cloud Run 服务。像这样,私有范围(至少,您也可以路由所有传出流量)流量被路由到连接器并登陆您的 VPC。现在您可以访问您的 VPC 中的私有资源,例如您的 redis 实例。


推荐阅读