如何获取 MongoDB 的已使用(和空闲)连接数(从客户端角度来看)?

How to get the number of connections used (and free) to the MongoDB (from a client perspective)?

我在这里发布问题只是为了确保我没有找错树。

如何获得连接数已用(和免费)到MongoDB,但从客户端的角度来看(例如 Java 客户端),使用 4.x 驱动程序?

有关于使用 serverStatus(Get the number of open connections in mongoDB using java) 的帖子,但假定具有 'admin' 访问 MongoDB 的权限。使用 'regular user'(权限较低的数据库用户(例如,只能访问一个数据库))不能 运行 serverStatus()。但这仅提供了服务器端的视图(IP xN 个连接)。

其他帖子提到了如何设置连接池大小(例如使用 MongoClients.create​(MongoClientSettings settings)(请参阅 4.x API 参考资料 (https://mongodb.github.io/mongo-java-driver/4.0/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClients.html)):

        MongoCredential credential = MongoCredential.createCredential(
                    username,
                    "admin",
                    password.toCharArray());

        MongoClient mongoClient = MongoClients.create(MongoClientSettings.builder()
                .applyToClusterSettings(
                        builder -> builder.hosts(Arrays.asList(new ServerAddress(hostname, portNumber))))
                .credential(credential)
                .applyToConnectionPoolSettings(builder -> builder
                                                                .minSize(connectionPoolMinimumSize)
                                                                .maxSize(connectionPoolMaximumSize))
                .readConcern(readConcern)
                .readPreference(readPreference)
                .writeConcern(writeConcern)
                .build());

但是none提供了获取连接池中已使用和可用连接的方法。

正如 Oleg 所提到的,使用 ConnectionPoolListener 是一种方法,但这仅在 3.x 驱动程序中可用。 ConnectionPoolListener 方法在 4.x 上被标记为已弃用(尽管在 JMX 监控部分 (http://mongodb.github.io/mongo-java-driver/4.0/driver-reactive/reference/monitoring/) 中仍然提到它)。

您可以使用 here 中描述的连接池监控来跟踪连接状态,并推断出您正在寻找的计数。

我不知道 Java driver 是否将您正在寻找的计数器公开为 public API;许多 driver 没有。

终于成功了:

  • 创建了一个自定义连接池侦听器,实现了com.mongodb.event.ConnectionPoolListener...
    public class CustomConnectionPoolListener implements ConnectionPoolListener {
        ...
    }
  • ... 并在 商店 上更新统计计数器(稍后可访问)
        @Override
        public void connectionCreated(ConnectionCreatedEvent event) {
            ConnectionPoolStatsPOJO cps = mongoConnectionPoolList.get(connectionPoolAlias);
            cps.incrementConnectionsCreated();
            mongoConnectionPoolList.put(connectionPoolAlias, cps);
        }
  • 将此自定义连接池侦听器附加到 MongoClient 连接:
ConnectionPoolListener customConnPoolListener = new CustomConnectionPoolListener(...); /* added some references in the */
    ...
    MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .applicationName(applicationName)
                .applyConnectionString(connURI)
                .credential(credential)
                .readConcern(readConcern)
                .readPreference(readPreference)
                .writeConcern(writeConcern)
                .applyToConnectionPoolSettings(builder -> builder
                        .minSize(connectionPoolMinimumSize)
                        .maxSize(connectionPoolMaximumSize)
                        .addConnectionPoolListener(customConnPoolListener)
                )
                .retryWrites(true)
                .retryReads(true)
                .build();
    ...
    MongoClient mongoClient = MongoClients.create(mongoClientSettings);
    ....
  • 最后,要访问连接池统计信息,只需查询 store:
        ConnectionPoolStatsPOJO connectionPoolStats = MongoDB_ConnectionPool_Repository.getInstance().getMongoConnectionPoolList().get(connectionPoolAlias);

所以感谢“@D.SM”指点正确的方向