我们在 Google Cloud Platform 中使用 redis 版本 - 6.2.7 和 node-redis 包版本 - 4.6.7 。我们在 Google 应用引擎的 Node.js 16 服务器中收到以下错误。
ConnectionTimeoutError: Connection timeout
at Socket.<anonymous> (/workspace/node_modules/@redis/client/dist/lib/client/socket.js:178:124)
at Object.onceWrapper (node:events:627:28)
at Socket.emit (node:events:513:28)
at Socket.emit (node:domain:489:12)
at Socket._onTimeout (node:net:550:8)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7)
连接redis的代码片段如下。
this.client = redis.createClient({
socket: { host: redisHost, port: REDISPORT},
});
this.client.connect();
this.client.on("error", (err) => log.error("Error in redis :", err));
我们还尝试在
createClient中配置
connectTimeout
10 秒,之后我们没有收到此错误。我们应该配置超时吗?
对于此连接超时错误,我们可以有适当的解决方案或解决方法吗?
你可以试试这段代码
const client = redis.createClient({
legacyMode: false,
socket: {
connectTimeout: 10000
}
});
您可以测试此端口是否为您的网络打开:
$ telnet <ip_address> <port_number>
$ telnet <domain_name> <port_number>
您可以重新启动终端,如果不起作用,请重新启动电脑。一定会成功的。
谢谢