简体   繁体   English

尝试在MongoClient中使用种子时出现问题

[英]Problem trying to use seeds with MongoClient

Hi I have a problem when trying to use seeds with MongoClient in JAVA. 嗨,我尝试在JAVA中将MongoClient与种子一起使用时遇到问题。 When debuging I may look into the mongoClient and see that the servers is added in there, but once it tries to get the database I see this error: 调试时,我可能会查看mongoClient并看到服务器已添加到其中,但是一旦尝试获取数据库,我会看到此错误:

Caused by: com.mongodb.MongoTimeoutException: Timed out after 15000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[]

Code building server list: 代码构建服务器列表:

private List<ServerAddress> getListOfServerAddresses() throws Exception {
    List<ServerAddress> serverAddresses = new ArrayList<>();
    instancesJsonList = "[{\"host\":\"localhost\",\"port\":12345},{\"host\":\"localhost\",\"port\":54321}]\"}]";
    if (instancesJsonList != null) {
        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode instanceList = mapper.readTree(instancesJsonList);
        for (final JsonNode node : instanceList) {
            final String host = node.get("host").asText();
            final String port = node.get("port").asText();

            ServerAddress address = new ServerAddress(host, Integer.parseInt(port));
            serverAddresses.add(address);
        }
    }
    return serverAddresses;
}

Client initiation as normal: 正常启动客户端:

mongoClient = new MongoClient(serverAddresses);

And I try to get collection list like this: 我尝试获取这样的收藏夹列表:

MongoDatabase db = mongoClient.getDatabase("My_database");
List<String> collectionList = db.listCollectionNames().into(new ArrayList<String>());

It fails on db.listCollectionNames(). 它在db.listCollectionNames()上失败。

Note that when using a single ServerAddress this works, it is only when I add a second ServerAddress to the list that it fails. 请注意,当使用单个ServerAddress时,此方法有效,只有当我向列表中添加第二个ServerAddress时,它才会失败。

The server address list listed from the mongoClient looks like this 从mongoClient列出的服务器地址列表如下所示

[localhost:59508, localhost:59518]

So the adress and ports are there, I have tried to connect to the host Using Robo... and I may connect to them both. 因此地址和端口都在那儿,我已尝试使用Robo连接到主机...,我可能会同时连接它们。

Note that I use MongodForTestsFactory to create the 2 mongoDB instances. 请注意,我使用MongodForTestsFactory创建2个mongoDB实例。

MongodForTestsFactory primaryTestsFactory = MongodForTestsFactory.with(Version.V3_4_1);
mongoClientPrimary = primaryTestsFactory.newMongo();
primaryPort = mongoClientPrimary.getAddress().getPort();

Found problem: The two embeded mongo instances launched was not set to be replica sets. 发现的问题:启动的两个嵌入式mongo实例未设置为副本集。 To work with mongoClient they must be replica sets. 要使用mongoClient,它们必须是副本集。

Sollution: I found the sollution on github: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/257#issuecomment-427642563 解决方案:我在github上找到了解决方案: https : //github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/257#issuecomment-427642563

With this I could launch mongo instances that was replica sets to each other and use them for my test. 这样,我可以启动相互为副本集的mongo实例,并将其用于测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM