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

时间:2016-06-29 18:42:51

标签: java mongodb

我正在尝试连接到测试环境中的mongo查询路由器(我只设置一个用于测试的查询路由器 - >指向配置服务器(而不是三个),这反过来又指向一个没有副本的双节点分片)。我可以使用mongo shell插入/获取文档(并且已经验证文档将转到分片节点)。但是,当我尝试测试与mongo数据库的连接时,我得到下面复制的输出(正在使用的代码也在下面复制)。我使用的是mongo数据库v3.2.0和java驱动程序v3.2.2(我正在尝试使用async 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分片中插入/获取文档。

1 个答案:

答案 0 :(得分:1)

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