我在连接到托管在 Mongodb Atlas 上的副本集的 java 异步驱动程序时遇到了一个烦人的问题。
我使用以下代码通过 Mongo DB Java Async API 3.8.2 和 3.9.1 连接到服务器。
我可以使用同步驱动程序使用完全相同的连接字符串进行连接。
private static String str = "mongodb+srv://RWUser:****@foo-cluster-s813m.mongodb.net/?retryWrites=true&streamType=netty";
public static void main(String... args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
MongoClient mongoClient = MongoClients.create(str);
MongoDatabase database = mongoClient.getDatabase("mydb");
database.createCollection("coll", (result, t) -> latch.countDown());
latch.await();
}
Run Code Online (Sandbox Code Playgroud)
这是日志的视图:
INFO: Cluster created with settings {hosts=[foo-cluster-shard-00-00-s813m.mongodb.net:27017, foo-cluster-shard-00-01-s813m.mongodb.net:27017, foo-cluster-shard-00-02-s813m.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='foo-cluster-shard-0'}
janv. 18, 2019 11:31:37 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server foo-cluster-shard-00-00-s813m.mongodb.net:27017 to client …Run Code Online (Sandbox Code Playgroud)