gpt4 book ai didi

java - 通过java驱动程序连接到mongo shard 3.2.2

转载 作者:行者123 更新时间:2023-12-01 09:45:10 28 4
gpt4 key购买 nike

我正在尝试在测试环境中连接到 mongo 查询路由器(我只设置一个查询路由器进行测试 -> 指向一个配置服务器(而不是三个),而配置服务器又指向一个没有副本的两节点分片) 。我可以使用 mongo shell 插入/获取文档(并已验证文档将发送到分片节点)。但是,当我尝试测试与 mongo 数据库的连接时,我得到了下面复制的输出(正在使用的代码也复制在下面)。我正在使用 mongo 数据库 v3.2.0 和 java 驱动程序 v3.2.2 (我正在尝试使用异步 api)。

[info] 14:34:44.562 227 [main] MongoAuthentication INFO - testing 1
[info] 14:34:44.595 260 [main] cluster INFO - Cluster created with settings {hosts=[192.168.0.1:27018], mode=MULTIPLE, requiredClusterType=SHARDED, serverSelectionTimeout='30000 ms', maxWaitQueueSize=30}
[info] 14:34:44.595 260 [main] cluster INFO - Adding discovered server 192.168.0.1:27018 to client view of cluster
[info] 14:34:44.652 317 [main] cluster DEBUG - Updating cluster description to {type=SHARDED, servers=[{address=192.168.0.1:27018, type=UNKNOWN, state=CONNECTING}]
[info] Outputting database names:
[info] 14:34:44.660 325 [main] cluster INFO - No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=SHARDED, connectionMode=MULTIPLE, all=[ServerDescription{address=192.168.0.1:27018, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
[info] Counting the number of documents
[info] 14:34:44.667 332 [main] cluster INFO - No server chosen by ReadPreferenceServerSelector{readPreference=primaryPreferred} from cluster description ClusterDescription{type=SHARDED, connectionMode=MULTIPLE, all=[ServerDescription{address=192.168.0.1:27018, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
[info] - Count result: 0
[info] 14:34:45.669 1334 [cluster-ClusterId{value='577414c420055e5bc086c255', description='null'}-192.168.0.1:27018] connection DEBUG - Closing connection connectionId{localValue:1}

正在使用的部分代码

   final MongoClient mongoClient = MongoClientAccessor.INSTANCE.getMongoClientInstance();

final CountDownLatch listDbsLatch = new CountDownLatch(1);
System.out.println("Outputting database names:");
mongoClient.listDatabaseNames().forEach(new Block<String>() {
@Override
public void apply(final String name) {
System.out.println(" - " + name);
}
}, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
listDbsLatch.countDown();
}
});

所使用的枚举负责读取配置选项并将 MongoClient 引用传递给其调用者。枚举本身调用其他类,如果需要的话我也可以复制它们。我为 ReadPreference 配置了以下选项:

mongo.client.readPreference=PRIMARYPREFERRED

对我可能做错了什么或可能误解了什么有什么想法吗?目标是通过 mongos(查询路由器)连接到分片,以便我可以在 Mongo 分片中插入/获取文档。

最佳答案

mongo 分片设置(查询路由器、配置和带有副本集的分片)未正确配置。确保首先启动配置服务器副本集,启动 mongos(查询路由器)并指向这些配置服务器,启动 mongo 分片,然后也通过查询路由器(mongos)注册分片因为集合已启用分片。显然,请确保驱动程序正在连接到 mongos(查询路由器)进程。

关于java - 通过java驱动程序连接到mongo shard 3.2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38107724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com