gpt4 book ai didi

java - 将 MongoClient 与列表 ServerAddress 一起使用时出现 com.mongodb.MongoTimeoutException

转载 作者:可可西里 更新时间:2023-11-01 09:16:59 27 4
gpt4 key购买 nike

我正在尝试将副本数据库部署到具有不同端口的一台服务器上并与之连接。如果我只使用单个 ServerAddress 并直接连接到主数据库,一切都很好像这样:

mongoClient =new MongoClient(new ServerAddress("104.236.106.53", 27000));
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");

一切正常。但是当我尝试使用 List<ServerAddress> 时像这样:

List<ServerAddress> lstServer = new ArrayList<ServerAddress>();
lstServer.add(new ServerAddress("104.236.106.53", 27000));
lstServer.add(new ServerAddress("104.236.106.53", 27002));
lstServer.add(new ServerAddress("104.236.106.53", 27001));
mongoClient = new MongoClient(lstServer);
morphia = new Morphia();
ds = morphia.createDatastore(mongoClient, "morphiaDB");

它会以这个错误结束:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches {serverSelectors=[ReadPreferenceServerSelector{readPreference=primary}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]}. Client view of cluster state is {type=ReplicaSet, servers=[{address=G-Server-1:27000, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}, {address=G-Server-1:27001, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}, {address=G-Server-1:27002, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: G-Server-1}}]
at com.mongodb.BaseCluster.getServer(BaseCluster.java:82)
at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:654)
at com.mongodb.DBTCPConnector.access$300(DBTCPConnector.java:39)
at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:503)
at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:451)
at com.mongodb.DBTCPConnector.getPrimaryPort(DBTCPConnector.java:409)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:182)
at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:165)
at com.mongodb.DBCollection.insert(DBCollection.java:161)
at com.mongodb.DBCollection.insert(DBCollection.java:107)
at com.mongodb.DBCollection.save(DBCollection.java:965)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:949)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1013)
at org.mongodb.morphia.DatastoreImpl.save(DatastoreImpl.java:1000)
at com.learn.DAO.AuthorBookDAO.main(AuthorBookDAO.java:18)

任何人都可以告诉我这个错误是什么或给我一些解决方法的提示吗?

最佳答案

在谷歌搜索后。我发现 java 无法识别哪个是主数据库

GWReplication:PRIMARY> cfg=rs.conf();
{
"_id" : "GWReplication",
"version" : 1,
"members" : [
{
"_id" : 0,
"host" : "G-Server-1:27000"
},
{
"_id" : 1,
"host" : "G-Server-1:27001"
},
{
"_id" : 2,
"host" : "G-Server-1:27002"
}
]
}

这是我的 mongodb 的默认配置。而java无法理解G-Server-1所以我通过将它更改为我上面的 ip 来修复它

GWReplication:PRIMARY> rs.config();
{
"_id" : "GWReplication",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "104.236.106.53:27000"
},
{
"_id" : 1,
"host" : "104.236.106.53:27001"
},
{
"_id" : 2,
"host" : "104.236.106.53:27002"
}
]
}

现在它工作正常。我知道这是一种非常愚蠢的修复方法,但老实说我不知道​​如何让 java 识别我的域名或如何在 ubuntu 服务器上配置它(我在 digitalocean 的主机上部署了 mongodb,这是我第一次使用 ubuntu并自行配置服务器)-_-

关于java - 将 MongoClient 与列表 ServerAddress 一起使用时出现 com.mongodb.MongoTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27607773/

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