简体   繁体   English

在 Spring 启动应用程序中配置 MongoDB 连接池

[英]Configure MongoDB connection pooling in Spring Boot application

I am trying to connect to a MongoDB replica set through a Spring Boot application with read preference secondary.我正在尝试通过 Spring 引导应用程序连接到 MongoDB 副本集,其中读取首选项为次要。 I want to configure idle static connections only to secondary instance that get created automatically on application startup.我想仅将空闲 static 连接配置到在应用程序启动时自动创建的辅助实例 I am using MongoClient to connect to MongoDB.我正在使用 MongoClient 连接到 MongoDB。

I came across MongoClientOptions bean that an be passed while creating the MongoClient.我遇到了在创建 MongoClient 时传递的 MongoClientOptions bean。 MongoClientOptions provides a property 'minConnectionsPerHost'. MongoClientOptions 提供了一个属性“minConnectionsPerHost”。 This creates idle connections to all the hosts (both primary and secondary).这会创建到所有主机(主要和次要)的空闲连接。 My requirement is to create idle connections only to secondary instance.我的要求是只为辅助实例创建空闲连接。

@Bean
MongoClientOptions mongoClientOptions() {
    return MongoClientOptions.builder()
            .minConnectionsPerHost(10)
            .build();
}

Is there a way to configure it using MongoClientOptions bean or any alternative to achieve the same result?有没有办法使用 MongoClientOptions bean 或任何替代方法来配置它以获得相同的结果?

You should be able to use minPoolSize URI option to specify the minimum connection pool size for each server.您应该能够使用minPoolSize URI 选项来指定每个服务器的最小连接池大小。 This setting applies to all servers that the client is connected to.此设置适用于客户端连接到的所有服务器。

I am not aware of any connection pool size knobs that selectively apply to a subset of the servers.我不知道有任何选择性地应用于服务器子集的连接池大小旋钮。

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

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