简体   繁体   English

如何使用 XML 中的 spring-data-mongodb 启用 mongo 连接池监控?

[英]How to enable mongo connection pool monitoring with spring-data-mongodb in XML?

I am using spring-data-mongodb 1.10.12 with mongo 3.6.4.我正在使用 spring-data-mongodb 1.10.12 和 mongo 3.6.4。 I recently upgraded from a lower version of mongo, and now my mongo connection pool monitoring is broken because there is no ConnectionPoolStatisticsMBean registered.我最近从一个较低版本的mongo升级,现在我的mongo连接池监控坏了,因为没有注册ConnectionPoolStatisticsMBean。 According to the documentation for that version of mongo "JMX connection pool monitoring is disabled by default. To enable it add a com.mongodb.management.JMXConnectionPoolListener instance via MongoClientOptions"根据该版本 mongo 的文档“默认情况下禁用 JMX 连接池监视。要启用它,请通过 MongoClientOptions 添加 com.mongodb.management.JMXConnectionPoolListener 实例”

However, in the xml schema for spring-data-mongo, the clientOptionsType does not allow setting that value, unless I am missing something.但是,在 spring-data-mongo 的 xml 模式中,clientOptionsType 不允许设置该值,除非我遗漏了什么。 Is there any way, with spring-data-mongodb, to turn on the connection pool monitoring through xml?有没有什么办法,用spring-data-mongodb,通过xml开启连接池监控?

Here is my xml for the mongo beans这是我的 mongo bean xml

<mongo:mongo-client id="mongo"
                    host="${mongo.hostname:#{null}}"
                    replica-set="${mongo.replica.set:#{null}}"
                    port="${mongo.port}"
                    credentials="'${mongo.username}:${mongo.password}@${mongo.auth.db.name}?uri.authMechanism=${mongo.auth.mechanism:SCRAM-SHA-1}'"
>
    <mongo:client-options connections-per-host="${mongo.connections-per-host:40}"
                          threads-allowed-to-block-for-connection-multiplier="${mongo.threads-blocked-per-connection:3}"
                          connect-timeout="${mongo.connection-timeout:10000}"
                          max-wait-time="${mongo.maxWaitTime:120000}"
                          socket-keep-alive="${mongo.socketKeepAlive:true}"
                          socket-timeout="${mongo.socketTimeout:0}"
                          read-preference="${mongo.read.preference:PRIMARY_PREFERRED}"
                          write-concern="${mongo.write.concern:ACKNOWLEDGED}"
    />
</mongo:mongo-client>

and my pom dependencies和我的 pom 依赖项

<properties>
    <mongo-version>3.6.4</mongo-version>
    <spring-data-version>1.10.12.RELEASE</spring-data-version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>${mongo-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>${spring-data-version}</version>
    </dependency>
</dependencies>

It is true that there is no way, through the spring-data-mongodb schema, to add a connection pool listener, but the folks that maintain the repo suggested a solution which is to use a BeanPostProcessor to alter the MongoClientOptions before they are passed to the mongo client like so确实,无法通过 spring-data-mongodb 模式添加连接池侦听器,但是维护 repo 的人提出了一个解决方案,即使用 BeanPostProcessor 来更改 MongoClientOptions,然后再将它们传递给mongo 客户端是这样的

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

    if (bean instanceof MongoClientOptions) {
        return MongoClientOptions.builder((MongoClientOptions) bean)
            .addConnectionPoolListener(new JMXConnectionPoolListener()).build();
    }
    return bean;
}

Doing so successfully registered ConnectionPoolStatisticsMBeans for me这样做成功地为我注册了 ConnectionPoolStatisticsMBeans

I tackled the very same challenge.我解决了同样的挑战。 In my case, originally the Spring configuration was done using XML.就我而言,最初 Spring 配置是使用 XML 完成的。 I have managed to combine the XML configuration with Java configuration, because the Java configuration gives you more flexibility to configure the MongoClientOptions :我已经设法将 XML 配置与 Java 配置结合起来,因为Java 配置让您可以更灵活地配置MongoClientOptions

@Configuration
public class MongoClientWrapper {

    @Bean
    public MongoClient mongo() 
    {
        //credentials:
        MongoCredential credential = MongoCredential.createCredential("user", "auth-db", "password".toCharArray());

        MongoClientOptions options = MongoClientOptions.builder()
                .addConnectionPoolListener(new MyConnectionPoolListener())
                .build();
        return new MongoClient(
                new ServerAddress("localhost", 27017),      //replica-set
                Arrays.asList(credential)
                ,options
                );
    }


    @Bean
    public MongoTemplate mongoTemplate()
    {
        return new MongoTemplate(mongo(), database);
    }
    ...
}

Hope this helps someone...希望这可以帮助某人...

In my project, adding the BeanPostProcessor was useless, because the MongoClientOptions Bean was not automatically instantiate.在我的项目中,添加 BeanPostProcessor 是没有用的,因为 MongoClientOptions Bean 没有自动实例化。 I had to create Bean manually to add a connection pool listener in my environment:我必须手动创建 Bean 才能在我的环境中添加连接池侦听器:

@Bean public MongoClientOptions myMongoClientOptions() {
      return MongoClientOptions.builder().addConnectionPoolListener(new JMXConnectionPoolListener()).build();
}

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

相关问题 spring-data-mongodb 中 mongodb 连接的默认 Mongodb 设置 - Default Mongodb settings for mongodb connection in spring-data-mongodb 需要使用 Spring-data-MongoDB 的 mongo-java-driver - Need for mongo-java-driver with Spring-data-MongoDB spring-data-mongodb在一个Mongo实例中连接多个数据库 - Spring-data-mongodb connect to multiple databases in one Mongo instance 是“ spring-data-mongodb:1.10.7”与“ mongo v3.6.0”兼容吗 - Is “spring-data-mongodb: 1.10.7” is compatible with “mongo v3.6.0” 如何将spring-data-mongodb配置为使用2个共享同一文档模型的不同mongo实例 - How to configure spring-data-mongodb to use 2 different mongo instances sharing the same document model 弹簧数据mongodb的。 我如何使用spring-data-mongodb库在mongo中动态创建数据库? - spring-data-mongodb. How can i dynamically create a database in mongo using spring-data-mongodb library? 使用spring-data-mongodb进行审计 - Auditing with spring-data-mongodb 从本地 Mongo DB 迁移到 Mongo DB Atlas 时,spring-data-mongodb 依赖是否足够? - Is spring-data-mongodb dependency sufficient when migrating from Mongo DB local to Mongo DB Atlas? 如何在 spring-boot 中禁用 spring-data-mongodb - How to disable spring-data-mongodb in spring-boot Spring MVC - 5.2.9.RELEASE with Mongo (spring-data-mongodb - 3.0.4.RELEASE) 不工作 - Spring MVC - 5.2.9.RELEASE with Mongo (spring-data-mongodb - 3.0.4.RELEASE) not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM