1

我正在尝试使用 async mongoClient 连接到 aws DocumentDB。

我在 aws 中创建了一个 DocumentDB 集群,并通过 ssh 命令行成功连接。

我去了这里并创建了 MongoClient 并成功连接并插入事件。

但是当我尝试创建 com.mongodb.async.client.MongoClient 时,连接失败并出现以下错误:

WritableServerSelector 没有从集群描述中选择服务器 ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=aws-cluster:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while接收消息},由 {io.netty.handler.timeout.ReadTimeoutException}}]} 引起。在超时之前等待 30000 毫秒。

    ClusterSettings clusterSettings = ClusterSettings.builder()
                .applyConnectionString(new ConnectionString(connectionString)).build();
        List<MongoCredential> credentials = new ArrayList<>();
        credentials.add(
                 MongoCredential.createCredential(
                         mongoUserName,
                         mongoDBName,
                         mongoPassword));

    MongoClientSettings settings = MongoClientSettings.builder()
            .credentialList(credentials)
            .clusterSettings(clusterSettings)
            .streamFactoryFactory(new NettyStreamFactoryFactory())
            .writeConcern(WriteConcern.ACKNOWLEDGED)
            .build();
    com.mongodb.async.client.MongoClient mongoClient = MongoClients.create(settings);



    MongoDatabase testDB = mongoClient.getDatabase("myDB");
    MongoCollection<Document> collection = testDB.getCollection("test");
    Document doc = new Document("name", "MongoDB").append("type", "database");

    //**trying insert document => here I got an error**
    collection.insertOne(doc, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                System.out.println("Inserted!");
            }
        });

你有什么想法,为什么会这样?

4

3 回答 3

1

我通过使用uri解决了它:

String uri = "mongodb://<username>:<Password>@<hostname>:27017/?ssl=true&ssl_ca_certs=cert";
MongoClientSettings settings = MongoClientSettings.builder()
                        .streamFactoryFactory(new NettyStreamFactoryFactory())
                        .applyConnectionString(new ConnectionString(uri))
                        .build();

com.mongodb.async.client.MongoClient mongoClient = MongoClients.create(settings);
于 2020-01-21T12:33:05.080 回答
0

我遇到了类似的错误,对我来说它与 TLS 配置有关。

我在 documentDB https://docs.aws.amazon.com/documentdb/latest/developerguide/security.encryption.ssl.html中禁用了 TLS

于 2020-01-16T18:26:35.787 回答
0

在我的情况下,我必须在禁用 TLS 后重新启动集群。(用例不需要 TLS)。重新启动后,连接建立成功。

于 2022-02-25T13:40:35.220 回答