将MongoClient与列表ServerAddress一起使用时,com.mongodb.MongoTimeoutException
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将MongoClient与列表ServerAddress一起使用时,com.mongodb.MongoTimeoutException相关的知识,希望对你有一定的参考价值。
我正在尝试将副本数据库部署到具有不同端口的一台服务器上并与之连接。如果我只使用单个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和自我配置服务器)-_-
将副本集更改为IP地址实际上是错误的方法。
试试这个:
- 恢复使用副本集配置中的域名。
- 使用副本集域名更新您的mongo客户端配置。
List<ServerAddress> lstServer = new ArrayList<ServerAddress>(); lstServer.add(new ServerAddress("G-Server-1", 27000)); lstServer.add(new ServerAddress("G-Server-1", 27002)); lstServer.add(new ServerAddress("G-Server-1", 27001)); mongoClient = new MongoClient(lstServer);
那应该解决它。 Spring Mongo实际上使用主机名和端口号来确定副本集中配置的主副本。
以上是关于将MongoClient与列表ServerAddress一起使用时,com.mongodb.MongoTimeoutException的主要内容,如果未能解决你的问题,请参考以下文章
NodeJS - MongoClient.Connect 与数据库的 URL 不是默认的
pymongo中的连接操作:Connection()与MongoClient()
Socket tips: 同意socket发送UDP Broadcast