[英]Is there easy way to extract connection string from legacy java MongoClient
Is there easy way (or a library) that would extract mongo connection string from legacy com.mongodb.MongoClient
object?是否有简单的方法(或库)可以从旧版
com.mongodb.MongoClient
object 中提取 mongo 连接字符串?
I know that I could extract all that information from MongoClient
and build the connection URI myself, but it seems to be error prone.我知道我可以从
MongoClient
中提取所有这些信息并自己构建连接 URI,但它似乎容易出错。
If there's way to create com.mongodb.client.MongoClient
instance from com.mongodb.MongoClient
then it would good solution for my problem as well. If there's way to create
com.mongodb.client.MongoClient
instance from com.mongodb.MongoClient
then it would good solution for my problem as well.
As D.SM has mentioned in the comment, it's better to create new MongoClient from legacy client instead of creating it from connection string, because not all client options have corresponding URI options.正如D.SM在评论中提到的那样,最好从旧客户端创建新的 MongoClient 而不是从连接字符串创建它,因为并非所有客户端选项都有相应的 URI 选项。 So far I came with:
到目前为止,我带来了:
private com.mongodb.client.MongoClient fromLegacyClient(MongoClient legacyMongoClient) {
MongoClientOptions mongoClientOptions = legacyMongoClient.getMongoClientOptions();
MongoClientSettings.Builder mongoClientSettingsBuilder = MongoClientSettings.builder()
.readPreference(legacyMongoClient.getReadPreference())
.writeConcern(legacyMongoClient.getWriteConcern())
.retryWrites(mongoClientOptions.getRetryWrites())
.readConcern(legacyMongoClient.getReadConcern())
.codecRegistry(mongoClientOptions.getCodecRegistry())
.compressorList(mongoClientOptions.getCompressorList())
.applicationName(mongoClientOptions.getApplicationName())
.commandListenerList(mongoClientOptions.getCommandListeners())
.codecRegistry(mongoClientOptions.getCodecRegistry())
.applyToClusterSettings(clusterSettings -> {
clusterSettings.hosts(legacyMongoClient.getAllAddress());
clusterSettings.localThreshold(mongoClientOptions.getLocalThreshold(), MILLISECONDS);
clusterSettings.serverSelectionTimeout(mongoClientOptions.getServerSelectionTimeout(), MILLISECONDS);
clusterSettings.serverSelector(mongoClientOptions.getServerSelector());
clusterSettings.requiredReplicaSetName(mongoClientOptions.getRequiredReplicaSetName());
mongoClientOptions.getClusterListeners().forEach(clusterSettings::addClusterListener);
})
.applyToConnectionPoolSettings(connectionPoolSettings -> {
mongoClientOptions.getConnectionPoolListeners().forEach(connectionPoolSettings::addConnectionPoolListener);
connectionPoolSettings.maxConnectionIdleTime(mongoClientOptions.getMaxConnectionIdleTime(), MILLISECONDS);
connectionPoolSettings.maxConnectionLifeTime(mongoClientOptions.getMaxConnectionLifeTime(), MILLISECONDS);
connectionPoolSettings.maxWaitTime(mongoClientOptions.getMaxWaitTime(), MILLISECONDS);
})
.applyToSocketSettings(socketSettings -> {
socketSettings.connectTimeout(mongoClientOptions.getConnectTimeout(), MILLISECONDS);
socketSettings.readTimeout(mongoClientOptions.getSocketTimeout(), MILLISECONDS);
})
.applyToServerSettings(serverSettings -> {
mongoClientOptions.getServerListeners().forEach(serverSettings::addServerListener);
serverSettings.minHeartbeatFrequency(mongoClientOptions.getMinHeartbeatFrequency(), MILLISECONDS);
serverSettings.heartbeatFrequency(mongoClientOptions.getHeartbeatFrequency(), MILLISECONDS);
})
.applyToSslSettings(sslSettings -> {
sslSettings.enabled(mongoClientOptions.isSslEnabled());
sslSettings.invalidHostNameAllowed(mongoClientOptions.isSslInvalidHostNameAllowed());
sslSettings.context(mongoClientOptions.getSslContext());
});
if (legacyMongoClient.getCredential() != null) {
mongoClientSettingsBuilder.credential(legacyMongoClient.getCredential());
}
return MongoClients.create(mongoClientSettingsBuilder.build());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.