spring - com.mongodb.async.client.ClientSessionHelpe 没有从集群描述 ClusterDescription 选择服务器

标签 spring aws-documentdb

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

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

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

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

No server chosen by WritableServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=aws-cluster:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message}, caused by {io.netty.handler.timeout.ReadTimeoutException}}]}. Waiting for 30000 ms before timing out.

    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!");
            }
        });

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

最佳答案

我用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);

关于spring - com.mongodb.async.client.ClientSessionHelpe 没有从集群描述 ClusterDescription 选择服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443432/

相关文章:

node.js - 使用 mongodb 4.x Node 驱动程序连接到 documentDB,端口转发不起作用

amazon-web-services - AWS DocumentDB 中的删除操作

mongodb - 从 AWS Glue 连接到 DocumentDB

java - Spring Cloud Gateway 中的身份验证

java - 为什么AutowiredAnnotationBeanPostProcessor支持@Value?

java - 在违反唯一约束时使用 JPA/Hibernate 和实体更新列

java - 仅删除连接表中的一行

java - 在生产中部署 .jar 文件的正确方法

aws-documentdb - mongodb索引什么时候加载到内存中?

amazon-web-services - 使用 VPC 共享从单独的 VPC 访问 AWS DocumentDB?