英文:
Is there easy way to extract connection string from legacy java MongoClient
问题
有没有一种简便的方法(或库),可以从旧的com.mongodb.MongoClient
对象中提取Mongo连接字符串?
我知道我可以从MongoClient
中提取所有信息并自己构建连接URI,但这似乎容易出错。
如果有一种方法可以从com.mongodb.MongoClient
创建com.mongodb.client.MongoClient
实例,那对我的问题也是一个好解决方案。
英文:
Is there easy way (or a library) that would extract mongo connection string from legacy com.mongodb.MongoClient
object?
I know that I could extract all that information from MongoClient
and build the connection URI myself, but it seems to be error prone.
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.
答案1
得分: 0
如D.SM在评论中提到的,最好从旧的客户端创建新的MongoClient,而不是从连接字符串创建它,因为并非所有客户端选项都具有相应的URI选项。
到目前为止,我得到了以下结果:
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());
}
英文:
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.
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());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论