使用最新的mongo客户端连接到Mongo Atlas Free层时遇到问题

时间:2019-04-02 07:14:43

标签: java mongodb

我有一个Mongo Atlas免费帐户,该帐户包含具有单个集合的数据库。客户端仅读取集合并检索行,因此我创建了一个具有只读访问权限的帐户。此帐户已通过罗盘GUI进行了测试,并且工作正常。

但是,当我尝试通过mongo java API连接时遇到了问题。

第一个代码:

MongoClient mongoClient = MongoClients.create(this.mongoURI);
MongoDatabase database = mongoClient.getDatabase(this.database);
MongoCollection<Document> collection = database.getCollection(this.collectionName);

当我在本地主机(dockerized mongodb)安装程序中工作时,以上代码段工作正常。

Mongo Atlas建议使用以下代码进行连接:

mongodb+srv://dev2:<password>@cluster0-ldxfb.mongodb.net/test?retryWrites=true

供参考,请在问题末尾找到第一个代码的日志。)

我永远无法正常使用此字符串作为this.mongoURI值。服务器从不响应数据,每次调用都会引发异常。

经过数小时的努力,我可以使用这种方法获得一些数据:

第二个代码:

MongoCredential credential = MongoCredential.createScramSha1Credential("dev2", "admin", "apass".toCharArray());
        MongoClientSettings settings = MongoClientSettings.builder()
                .credential(credential)
                .retryWrites(false)
                .applyToConnectionPoolSettings(builder ->
                        builder.maxConnectionIdleTime(60000, TimeUnit.MILLISECONDS))
                .applyToSslSettings(builder -> builder.enabled(true))
                .applyToClusterSettings(builder -> {
                    builder.hosts(Arrays.asList(
                            new ServerAddress("cluster0-shard-00-00-ldxfb.mongodb.net", 27017),
                            new ServerAddress("cluster0-shard-00-01-ldxfb.mongodb.net", 27017),
                            new ServerAddress("cluster0-shard-00-02-ldxfb.mongodb.net", 27017)
                    ));
                    builder.requiredReplicaSetName("Cluster0-shard-0");
                })
                .build();

        MongoClient mongoClient = MongoClients.create(settings);
        MongoDatabase database = mongoClient.getDatabase(this.database);
        MongoCollection<Document> collection = database.getCollection(this.collectionName);

使用此代码,即使响应时间> 2秒,我也可以获取数据。

我想知道是否有人遇到这个问题并设法以更少的代码连接并获取数据。

为什么第一个代码不起作用?我知道有解决方法,但是我想了解为什么mongo建议的代码失败了。 日志

2019.04.02 10:17:01 INFO org.mongodb.driver.cluster Thread[nioEventLoopGroup-3-2,10,main]: Cluster created with settings {hosts=[127.0.0.1:27017], srvHost=cluster0-ldxfb.mongodb.net, mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='Cluster0-shard-0'}
2019.04.02 10:17:01 WARNING io.helidon.webserver.RequestRouting Thread[nioEventLoopGroup-3-2,10,main]: Default error handler: Unhandled exception encountered.
java.util.concurrent.ExecutionException: Unhandled 'cause' of this exception encountered.
    at io.helidon.webserver.RequestRouting$RoutedRequest.defaultHandler(RequestRouting.java:408)
    REMOVE SOME LINES
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:579)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:496)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
    at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:176)
    at com.mongodb.internal.connection.AbstractMultiServerCluster.getDescription(AbstractMultiServerCluster.java:53)
    at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:136)
    at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:94)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.getClientSession(MongoClientDelegate.java:249)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:172)
    at com.mongodb.client.internal.MongoCollectionImpl.executeCount(MongoCollectionImpl.java:249)
    at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:218)
    at com.mongodb.client.internal.MongoCollectionImpl.countDocuments(MongoCollectionImpl.java:213)
    at io.lef.guides.se.rawina.Names.getMongoNameLastOrMiddle(Names.java:165)
    at io.lef.guides.se.rawina.Names.getNameLastOrMiddle(Names.java:221)
    at io.helidon.webserver.RequestRouting$RoutedRequest.next(RequestRouting.java:352)
    ... 29 more

0 个答案:

没有答案