通过 mongo-java-驱动程序 Java API 设置 Mongodb 连接池选项
Setting Mongodb Connection Pool Options via mongo-java-driver Java API
使用 Morphia 和 mongo-java-驱动程序,我正在连接到 mongo 服务器:
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.sslEnabled(true);
builder.sslInvalidHostNameAllowed(true);
builder.maxConnectionIdleTime(0);
MongoClientOptions sslOptions = builder.build();
MongoClient mongoClient = new MongoClient(serverAddressList, Arrays.asList(mongoCredential), sslOptions);
其中 serverAddressList 是 host:port 列表,mongo使用 kerberos 身份验证的凭据创建为:
MongoCredential mongoCredential = MongoCredential.createGSSAPICredential("user@REALM.COM");
我想使用以下方法设置自定义连接池参数:
- 最大池大小
- 最小池大小
- maxIdleTimeMS 等
为了实现这一点,我找到了 ConnectionPoolSettings.Builder 和 com.mongodb.connection.ConnectionPoolSettings 类.
我搜索了一个示例代码here:
ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings
.builder()
.minSize(MIN_MONGO_POOL_SIZE)
.maxSize(MONGO_POOL_SIZE)
.build();
MongoClientSettings settings = MongoClientSettings
.builder()
.readPreference(MONGO_READ_PREFERENCE)
.credentialList(credentialsList).clusterSettings(clusterSettings)
.connectionPoolSettings(connectionPoolSettings).build();
有人可以建议一种应用这些设置来获取 MongoClient 实例的方法吗?
正在使用的库:
dependencies {
compile 'org.mongodb:mongodb-driver-sync:3.4.3'
}
终于找到连接池参数的正确设置方法了,所以在这里回答我自己的问题。
我浏览了 MongoClientOptions 的源代码 here
其中 connectionPool 参数设置如下:
connectionPoolSettings = ConnectionPoolSettings
.builder()
.minSize(getMinConnectionsPerHost())
.maxSize(getConnectionsPerHost())
.maxWaitQueueSize(getThreadsAllowedToBlockForConnectionMultiplier()
* getConnectionsPerHost())
.maxWaitTime(getMaxWaitTime(), MILLISECONDS)
.maxConnectionIdleTime(getMaxConnectionIdleTime(), MILLISECONDS)
.maxConnectionLifeTime(getMaxConnectionLifeTime(), MILLISECONDS)
.build();
我们可以看到正在从 getMinConnectionsPerHost();
和 getConnectionsPerHost();
方法中获取 minSize 和 maxSize 值。因此,要在客户端代码中更改和自定义连接池参数,我们只需将 maxConnectionIdleTime 和 minConnectionsPerHost 值添加到构建器中,如下所示:
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.maxConnectionIdleTime(0);
builder.minConnectionsPerHost(50);
builder.connectionsPerHost(200);
MongoClientOptions sslOptions = builder.build();
此代码成功打开指定的最小连接到 mongodb 服务器主机。
LOG : 00:20:00,714 INFO [org.mongodb.driver.connection] (pool-1-thread-1) Opened connection [connectionId{localValue:50}] to server.com:27180
使用 Morphia 和 mongo-java-驱动程序,我正在连接到 mongo 服务器:
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.sslEnabled(true);
builder.sslInvalidHostNameAllowed(true);
builder.maxConnectionIdleTime(0);
MongoClientOptions sslOptions = builder.build();
MongoClient mongoClient = new MongoClient(serverAddressList, Arrays.asList(mongoCredential), sslOptions);
其中 serverAddressList 是 host:port 列表,mongo使用 kerberos 身份验证的凭据创建为:
MongoCredential mongoCredential = MongoCredential.createGSSAPICredential("user@REALM.COM");
我想使用以下方法设置自定义连接池参数:
- 最大池大小
- 最小池大小
- maxIdleTimeMS 等
为了实现这一点,我找到了 ConnectionPoolSettings.Builder 和 com.mongodb.connection.ConnectionPoolSettings 类.
我搜索了一个示例代码here:
ConnectionPoolSettings connectionPoolSettings = ConnectionPoolSettings
.builder()
.minSize(MIN_MONGO_POOL_SIZE)
.maxSize(MONGO_POOL_SIZE)
.build();
MongoClientSettings settings = MongoClientSettings
.builder()
.readPreference(MONGO_READ_PREFERENCE)
.credentialList(credentialsList).clusterSettings(clusterSettings)
.connectionPoolSettings(connectionPoolSettings).build();
有人可以建议一种应用这些设置来获取 MongoClient 实例的方法吗?
正在使用的库:
dependencies {
compile 'org.mongodb:mongodb-driver-sync:3.4.3'
}
终于找到连接池参数的正确设置方法了,所以在这里回答我自己的问题。
我浏览了 MongoClientOptions 的源代码 here 其中 connectionPool 参数设置如下:
connectionPoolSettings = ConnectionPoolSettings
.builder()
.minSize(getMinConnectionsPerHost())
.maxSize(getConnectionsPerHost())
.maxWaitQueueSize(getThreadsAllowedToBlockForConnectionMultiplier()
* getConnectionsPerHost())
.maxWaitTime(getMaxWaitTime(), MILLISECONDS)
.maxConnectionIdleTime(getMaxConnectionIdleTime(), MILLISECONDS)
.maxConnectionLifeTime(getMaxConnectionLifeTime(), MILLISECONDS)
.build();
我们可以看到正在从 getMinConnectionsPerHost();
和 getConnectionsPerHost();
方法中获取 minSize 和 maxSize 值。因此,要在客户端代码中更改和自定义连接池参数,我们只需将 maxConnectionIdleTime 和 minConnectionsPerHost 值添加到构建器中,如下所示:
MongoClientOptions.Builder builder = MongoClientOptions.builder();
builder.maxConnectionIdleTime(0);
builder.minConnectionsPerHost(50);
builder.connectionsPerHost(200);
MongoClientOptions sslOptions = builder.build();
此代码成功打开指定的最小连接到 mongodb 服务器主机。
LOG : 00:20:00,714 INFO [org.mongodb.driver.connection] (pool-1-thread-1) Opened connection [connectionId{localValue:50}] to server.com:27180