java mongo连接问题,一直超时,原因不知道在哪

mingxu 发布于 2016/09/12 18:11
阅读 1K+
收藏 0

【Gopher China万字分享】华为云的Go语言云原生实战经验!>>>

@kurumi 你好,想跟你请教个问题:

http://www.oschina.net/question/2380402_248685 这篇帖子我遇到了同样的问题一直没有解决~

复制集状态都正常:

{
"set" : "res",
"date" : ISODate("2016-09-12T10:01:24.787Z"),
"myState" : 1,
"term" : NumberLong(23),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "172.16.200.41:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 23605,
"optime" : {
"ts" : Timestamp(1473672023, 2),
"t" : NumberLong(23)
},
"optimeDate" : ISODate("2016-09-12T09:20:23Z"),
"lastHeartbeat" : ISODate("2016-09-12T10:01:23.556Z"),
"lastHeartbeatRecv" : ISODate("2016-09-12T10:01:23.137Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "172.16.100.40:27017",
"configVersion" : 2
},
{
"_id" : 1,
"name" : "172.16.100.40:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 23605,
"optime" : {
"ts" : Timestamp(1473672023, 2),
"t" : NumberLong(23)
},
"optimeDate" : ISODate("2016-09-12T09:20:23Z"),
"electionTime" : Timestamp(1473672023, 1),
"electionDate" : ISODate("2016-09-12T09:20:23Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 2,
"name" : "172.16.200.20:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1860,
"lastHeartbeat" : ISODate("2016-09-12T10:01:23.701Z"),
"lastHeartbeatRecv" : ISODate("2016-09-12T10:01:24.746Z"),
"pingMs" : NumberLong(0),
"configVersion" : 2
}
],
"ok" : 1
}


但JAVA连接就出错:

String url = "mongodb://" + "admin" + ":" + "res1234" + "@" 
+  "54.119.134.45:27017" + "," 
+  "54.119.129.46:27017" + "/" 
                +  "admin" 
                +  "?replicaSet=" + replset_name
+  "&slaveOk=true&connectTimeoutMS=300000&readPreference=secondary";
System.out.println(url);
        MongoClientURI connectionString = new MongoClientURI(url);
        return new MongoClient(connectionString);

错误信息:

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. 
Client view of cluster state is {type=REPLICA_SET, servers=[{address=172.16.100.40:27017, type=UNKNOWN, state=CONNECTING, 
exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}, 
{address=172.16.200.20:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, 
caused by {java.net.SocketTimeoutException: connect timed out}}, 
{address=172.16.200.41:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, 
caused by {java.net.SocketTimeoutException: connect timed out}}]

加载中
0
kurumi
kurumi
额,好久没用mongo了,你的连接方式我也不熟悉,我以前那问题是因为内外网权限的问题导致超时的。如果你连接单台没问题的话,大概也是网络布局配置这方面的原因吧
m
mingxu
@kurumi 找到问题了 17 在配置复制集时写的是内网IP 测试时JAVA程序写的外网IP , 现以修改复制集先用外网IP配置! 谢谢
kurumi
kurumi
回复 @mingxu : 这就奇怪了,两边都没问题,那主从连接呢?如果还没问题,那我也不知道问题到底是出在哪了。。。
m
mingxu
单台连接主节点读写没有问题 ,单连接从节点读也没有问题,但用集群方式连接就不行 , 防火墙都关闭了, 难道是链接方式的问题?
0
冷川
冷川
可能是防火墙的原因吧。
m
mingxu
我把防火墙都关闭了, 可是还是不行
返回顶部
顶部