如何获得从MongoDB使用(和免费)到MongoDB的连接数(从客户端的角度来看)? [英] How to get the number of connections used (and free) to the MongoDB (from a client perspective)?

查看:197
本文介绍了如何获得从MongoDB使用(和免费)到MongoDB的连接数(从客户端的角度来看)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里发布问题只是为了确保我没有在错误的树上吠叫.

I'm posting the question here just to be sure I'm not barking on the wrong tree.

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

How to get the number of connections used (and free) to the MongoDB, but from a client perspective (eg. Java client), using the 4.x driver?

有些关于使用serverStatus的帖子(获取使用java 的mongoDB中打开的连接数),但它假定对MongoDB具有管理员"访问权限.使用常规用户"(具有较低特权(例如,仅访问一个数据库)的数据库用户)无法运行serverStatus().但这仅提供了服务器端的视图(IP x 中存在 N 个连接).

There are posts regarding using the serverStatus(Get the number of open connections in mongoDB using java), but it presumes having 'admin' access to the MongoDB. Using a 'regular user'(an db user with lower privileges (e.g access to only one database)) cannot run the serverStatus(). But this provides only a view from the server-side (there are N connections from IP x).

其他文章提到了如何设置连接池大小(例如使用 MongoClients.create((MongoClientSettings设置))(请参阅4.x API参考(

Other posts mentioned how to setup the connection pool size (eg. using the MongoClients.create​(MongoClientSettings settings) (see the 4.x API reference (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());

但是没有提供任何方法来使连接池获得已使用和可用的连接.

But none provided means to get the used and available connections the connection pool.

如Oleg所述,使用ConnectionPoolListener是一种方法,但这仅在3.x驱动程序中可用.ConnectionPoolListener方法在4.x上已标记为已弃用(尽管JMX Monitoring部分中仍提到了该方法(

As mentioned by Oleg, using the ConnectionPoolListener would be a way, but that is available only in the 3.x drivers. The ConnectionPoolListener methods are marked as deprecated on 4.x (although it is still mentioned in the JMX Monitoring section (http://mongodb.github.io/mongo-java-driver/4.0/driver-reactive/reference/monitoring/).

推荐答案

终于成功了:

  • 创建了自定义连接池侦听器,实现了 com.mongodb.event.ConnectionPoolListener ...
  • created a custom connection pool listener, implementing the com.mongodb.event.ConnectionPoolListener...
    public class CustomConnectionPoolListener implements ConnectionPoolListener {
        ...
    }

  • ...并在 store 上更新统计计数器(以后可以访问)
    • ... and having the stats counters updated on a store (accessible later)
    •         @Override
              public void connectionCreated(ConnectionCreatedEvent event) {
                  ConnectionPoolStatsPOJO cps = mongoConnectionPoolList.get(connectionPoolAlias);
                  cps.incrementConnectionsCreated();
                  mongoConnectionPoolList.put(connectionPoolAlias, cps);
              }
      

      • 将此自定义连接池侦听器附加到MongoClient连接:
        • attached this custom connection pool listener to the MongoClient connection:
        • 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"指向正确的方向.

            Therefore, thanks to "@D. SM" for pointing to the right direction.

            这篇关于如何获得从MongoDB使用(和免费)到MongoDB的连接数(从客户端的角度来看)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆