有没有一种简单的方法可以从传统的Java MongoClient 中提取连接字符串?

huangapple go评论86阅读模式
英文:

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());
}

huangapple
  • 本文由 发表于 2020年8月5日 05:51:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63255577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定