简体   繁体   English

监控 mongodb 连接到 NodeJS 中的副本集

[英]Monitoring mongodb connection to a replicaset in NodeJS

I want to connect to a MongoDB replica set (only one instance to works with change streams) while being able to be notified of connection lost/reconnect.我想连接到 MongoDB 副本集(只有一个实例可用于更改流),同时能够收到连接丢失/重新连接的通知。 I followed what described here :我遵循 这里描述的内容:

const { MongoClient } = require("mongodb");

// Replace the following with your MongoDB deployment's connection
// string.
const uri =
  "mongodb+srv://<clusterUrl>/?replicaSet=rs&writeConcern=majority";

const client = new MongoClient(uri);

// Replace <event name> with the name of the event you are subscribing to.
const eventName = "<event name>";
client.on(eventName, event => {
  console.log(`received ${eventName}: ${JSON.stringify(event, null, 2)}`);
});

async function run() {
  try {
    await client.connect();

    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

I tried subscribing to events:我尝试订阅事件:

  • serverOpening and works fine serverOpening并且工作正常
  • serverClosed and I can't understand why but it does not work!!! serverClosed我不明白为什么,但它不起作用!!!

No "reconnect" event, any solution?没有“重新连接”事件,有什么解决办法吗?

You are mixing monitoring connections and application connections.您正在混合监视连接和应用程序连接。 Unfortunately the documentation you referenced doesn't talk about this and doesn't document CMAP events so the confusion is understandable.不幸的是,您引用的文档没有谈到这一点,也没有记录 CMAP 事件,因此混淆是可以理解的。 See the Ruby driver docs for a more in-depth explanation of the events that drivers publish (including the Node driver).有关驱动程序发布的事件(包括节点驱动程序)的更深入解释,请参阅Ruby 驱动程序文档

Monitoring connections are established by the driver to figure out what server(s) exist in the deployment it was instructed to work with.监控连接由驱动程序建立,以确定部署中存在哪些服务器,它被指示使用。 One (or two depending on driver and server version) such connection is established per known server.每个已知服务器建立一个(或两个取决于驱动程序和服务器版本)这样的连接。 You don't control when these connections are established.您无法控制何时建立这些连接。 These connections are NOT used for operations you initiate (inserts/finds etc.).这些连接不用于您启动的操作(插入/查找等)。 They are only used internally by the driver.它们仅由驱动程序在内部使用。

The events published for monitoring connections are server opened, server closed, server heartbeat - the ones listed here .为监视连接发布的事件是服务器打开、服务器关闭、服务器心跳 - 此处列出的事件。 You are going to get these events when the client is instantiated (assuming a spec-compliant client, which the Node one is not in it default configuration as you are using it) without any operations being issued like creating a change stream.当客户端被实例化时,您将获得这些事件(假设一个符合规范的客户端,当您使用它时,节点一不在其默认配置中)而不会发出任何操作,例如创建更改 stream。

Application connections are established by the driver to satisfy the application's operations like finds and inserts.应用程序连接由驱动程序建立以满足应用程序的操作,如查找和插入。 One of these would be needed for your change stream.您的更改 stream 将需要其中之一。 The events relevant to these connections are CMAP ones and start with "Connection" or "Pool", eg ConnectionCreated.与这些连接相关的事件是CMAP事件,并以“Connection”或“Pool”开头,例如 ConnectionCreated。 These connections aren't established until you issue an operation, unless you have the min pool size on the client set to a value greater than zero.这些连接在您发出操作之前不会建立,除非您将客户端上的最小池大小设置为大于零的值。

If you want to "monitor connections", you can subscribe to either category of events or both.如果您想“监控连接”,您可以订阅任一类别的事件或两者。

With that said, both types of connections are managed internally by the driver.话虽如此,这两种类型的连接都由驱动程序在内部管理 You don't get a say in when they are created or destroyed (other than setting min pool size and idle timeouts).您在创建或销毁它们时没有发言权(除了设置最小池大小和空闲超时)。 So if your goal is to have a working, continuously-running, resuming change stream, you don't need any of this and instead you should be using the proper change stream consumption patterns like the one described here in Ruby syntax although all spec-compliant drivers should provide the equivalent interface.因此,如果您的目标是有一个工作的、持续运行的、恢复的更改 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ,那么您不需要任何这些,而是应该使用正确的更改 stream 消费模式,如 Z9916D1FC59FE22FECC4ZA 语法中描述的模式兼容的驱动程序应该提供等效的接口。

Lastly, there isn't a "reconnect" event defined in any driver specification.最后,在任何驱动程序规范中都没有定义“重新连接”事件。 If you have a question specifically about this event you should reference the driver documentation where it is described and read that documentation carefully to ascertain the implemented behavior.如果您对此事件有特别的疑问,您应该参考描述它的驱动程序文档并仔细阅读该文档以确定实现的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM