集群监控
本页内容
概述
本指南展示了如何在MongoDB实例、副本集或分片集群中监控拓扑事件。当您连接到的实例或集群状态发生变化时,驱动程序会创建拓扑事件,也称为服务器发现和监控(SDAM)事件。例如,当您建立新的连接或集群选举新的主节点时,驱动程序会创建一个事件。
以下部分展示了如何在您的应用程序中记录拓扑更改并探索这些事件提供的信息。
事件订阅示例
您可以通过在应用程序中订阅它们来使用驱动程序访问一个或多个SDAM事件。以下示例演示了如何连接到副本集并订阅MongoDB部署创建的SDAM事件之一。
/* Subscribe to SDAM event */ 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>"; // Subscribe to a specified event and print a message when the event is received client.on(eventName, event => { console.log(`received ${eventName}: ${JSON.stringify(event, null, 2)}`); }); async function run() { try { // Establish and verify connection to the database await client.db("admin").command({ ping: 1 }); console.log("Connected successfully"); } finally { // Close the database connection on completion or error await client.close(); } } run().catch(console.dir);
事件描述
您可以订阅以下任何SDAM事件
事件名称 | 描述 |
---|---|
serverOpening | 在打开实例连接时创建。 |
serverClosed | 在关闭实例连接时创建。 |
serverDescriptionChanged | 在实例状态更改时创建(例如从从节点到主节点)。 |
topologyOpening | 在尝试连接实例之前创建。 |
topologyClosed | 在拓扑中所有实例连接关闭之后创建。 |
topologyDescriptionChanged | 在拓扑更改时创建,例如选举新的主节点或mongos代理断开连接。 |
serverHeartbeatStarted | 在向MongoDB实例发出hello命令之前创建。 |
serverHeartbeatSucceeded | 当从MongoDB实例成功返回hello命令时创建。 |
serverHeartbeatFailed | 当向特定MongoDB实例发出的hello命令失败并返回不成功响应时创建。 |
示例事件文档
以下各节展示了每种类型SDAM事件的示例输出。
serverDescriptionChanged
ServerDescriptionChangedEvent { topologyId: 0, address: 'localhost:27017', previousDescription: ServerDescription { address: 'localhost:27017', error: null, roundTripTime: 0, lastUpdateTime: 1571251089030, lastWriteDate: null, opTime: null, type: 'Unknown', minWireVersion: 0, maxWireVersion: 0, hosts: [], passives: [], arbiters: [], tags: [] }, newDescription: ServerDescription { address: 'localhost:27017', error: null, roundTripTime: 0, lastUpdateTime: 1571251089051, lastWriteDate: 2019-10-16T18:38:07.000Z, opTime: { ts: Timestamp, t: 18 }, type: 'RSPrimary', minWireVersion: 0, maxWireVersion: 7, maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 100000, me: 'localhost:27017', hosts: [ 'localhost:27017' ], passives: [], arbiters: [], tags: [], setName: 'rs', setVersion: 1, electionId: ObjectID, primary: 'localhost:27017', logicalSessionTimeoutMinutes: 30, '$clusterTime': ClusterTime } }
此事件中ServerDescription
对象的type
字段包含以下可能值之一
类型 | 描述 |
---|---|
未知 | 未知实例 |
独立服务器 | 独立服务器实例 |
Mongos | Mongos代理实例 |
可能的主节点 | 至少有一台服务器将其识别为主要服务器,但尚未被所有实例验证。 |
RSPrimary | 主要实例 |
RSSecondary | 次要实例 |
RSArbiter | 仲裁实例 |
RSOther | 参见RSGhost 和 RSOther 规范获取更多详细信息 |
RSGhost | 参见RSGhost 和 RSOther 规范获取更多详细信息 |
serverHeartbeatStarted
ServerHeartbeatStartedEvent { connectionId: 'localhost:27017' }
服务器心跳成功
ServerHeartbeatSucceededEvent { duration: 1.939997, reply:{ hosts: [ 'localhost:27017' ], setName: 'rs', setVersion: 1, isWritablePrimary: true, secondary: false, primary: 'localhost:27017', me: 'localhost:27017', electionId: ObjectID, lastWrite: { opTime: { ts: [Timestamp], t: 18 }, lastWriteDate: 2019-10-16T18:38:17.000Z, majorityOpTime: { ts: [Timestamp], t: 18 }, majorityWriteDate: 2019-10-16T18:38:17.000Z }, maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 100000, localTime: 2019-10-16T18:38:19.589Z, logicalSessionTimeoutMinutes: 30, minWireVersion: 0, maxWireVersion: 7, readOnly: false, ok: 1, operationTime: Timestamp, '$clusterTime': ClusterTime }, connectionId: 'localhost:27017' }
服务器心跳失败
ServerHeartbeatFailed { duration: 20, failure: MongoError('some error'), connectionId: 'localhost:27017' }
服务器开启
ServerOpeningEvent { topologyId: 0, address: 'localhost:27017' }
服务器关闭
ServerClosedEvent { topologyId: 0, address: 'localhost:27017' }
拓扑打开
TopologyOpeningEvent { topologyId: 0 }
拓扑关闭
TopologyClosedEvent { topologyId: 0 }
拓扑描述已更改
TopologyDescriptionChangedEvent { topologyId: 0, previousDescription: TopologyDescription { type: 'ReplicaSetNoPrimary', setName: null, maxSetVersion: null, maxElectionId: null, servers: Map { 'localhost:27017' => ServerDescription }, stale: false, compatible: true, compatibilityError: null, logicalSessionTimeoutMinutes: null, heartbeatFrequencyMS: 10000, localThresholdMS: 15, options: Object, error: undefined, commonWireVersion: null }, newDescription: TopologyDescription { type: 'ReplicaSetWithPrimary', setName: 'rs', maxSetVersion: 1, maxElectionId: null, servers: Map { 'localhost:27017' => ServerDescription }, stale: false, compatible: true, compatibilityError: null, logicalSessionTimeoutMinutes: 30, heartbeatFrequencyMS: 10000, localThresholdMS: 15, options: Object, error: undefined, commonWireVersion: 7 } }
此事件中 拓扑描述
对象的 类型
字段包含以下可能值之一
类型 | 描述 |
---|---|
单例 | 独立服务器实例 |
主副本集 | 带主节点的副本集 |
无主副本集 | 无主节点的副本集 |
分片 | 分片集群 |
未知 | 未知拓扑 |