简体   繁体   English

如何检测副本集的 MongoDB 重新连接

[英]How detect MongoDB reconnection for replica set

If I try to register for the event 'reconnect' in a MongoDB replicaset:如果我尝试在 MongoDB 副本集中注册事件“重新连接”:

db.on('reconnect', () => console.log('Reconnected'));

I receive a deprecation warning as:我收到了一个弃用警告:

DeprecationWarning: The `reconnect` event is no longer supported by the unified topology

How can I handle a case of lost MongoDB connection (all servers in the replica set) but I want to be notified of servers availability status (when at least one server become again available)?如何处理 MongoDB 连接丢失的情况(副本集中的所有服务器)但我想收到服务器可用性状态的通知(当至少一台服务器再次可用时)?
Suppose to handle this in a Node app with MongoDB native drivers.假设在带有 MongoDB 本机驱动程序的 Node 应用程序中处理此问题。 Thanks in advance.提前致谢。

If we take a look at the spec regarding the unified topology , we can find the following section:如果我们查看有关统一拓扑的规范,我们可以找到以下部分:

The unified topology is the first step in a paradigm shift away from a concept of “connecting” to a MongoDB deployment using a connect method.统一拓扑是范式从“连接”概念转变为使用连接方法的 MongoDB 部署的第一步。 Consider for a moment what it means to be connected to a replica set: do we trigger this state when connected to a primary?考虑一下连接到副本集意味着什么:连接到主服务器时我们是否会触发此状态? A primary and one secondary?一个小学和一个中学? When connected to all known nodes?当连接到所有已知节点时? It's unclear whether its possible to answer this without introducing something like a ReadPreference parameter to the connect method.目前尚不清楚是否可以在不向 connect 方法引入 ReadPreference 参数之类的内容的情况下回答这个问题。 At this point “connecting” is just one half of “operation execution” - you pass a ReadPreference in, and await a selectable server for the operation, now we're connected!此时“连接”只是“操作执行”的一半——你传入一个 ReadPreference,并等待一个可选择的服务器进行操作,现在我们连接了!

There are, however, new events you can listen to and that might be useful for your usecase -> see this for more information.有,但是,新的事件,你可以听,并且可能是您的用例有用的- >看到这个以获取更多信息。

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

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