Yas*_*xit 13 sockets redis node.js node-redis
首先,让我告诉您如何在 NodeJS 应用程序中使用 Redis 连接:
class RDB {
static async getClient() {
if (this.client) {
return this.client
}
let startTime = Date.now();
this.client = createClient({
url: config.redis.uri
});
await this.client.connect();
return this.client;
}
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因 - 我不知道 - 我的应用程序有时会崩溃,无缘无故地出现错误 - 这种情况大约每周发生一两次:
Error: Socket closed unexpectedly
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是:
我使用“错误”监听器解决了这个问题。只需聆听它即可避免节点应用程序崩溃。
client.on("error", function(error) {
console.error(error);
// I report it onto a logging service like Sentry.
});
Run Code Online (Sandbox Code Playgroud)
小智 6
我遇到了类似的套接字意外关闭问题。当我将 node-redis 从 3.x 升级到 4.x 时,问题开始出现。当我将 redis-server 从 5.x 升级到 6.x 后,这个问题就消失了。