gpt4 book ai didi

java - 如何使用 mongodb java 驱动程序 3.0.0 从辅助设备获取配置文件状态?

转载 作者:行者123 更新时间:2023-11-30 08:13:10 24 4
gpt4 key购买 nike

我无法获取辅助节点 (s132) 的配置文件状态,因为驱动程序正在循环并最终超时:

2015-05-05 17:59:55,829 [main] INFO  org.mongodb.driver.cluster - No server chosen by PrimaryServerSelector from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=SINGLE, all=[ServerDescription{address=s132:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[2, 6, 4]}, minWireVersion=0, maxWireVersion=2, maxDocumentSize=16777216, roundTripTimeNanos=8836176, setName='offerStoreDE3', hosts=[s132:27017, s124:27017], passives=[], arbiters=[], primary='s124:27017', tagSet=TagSet{[]}}]}. Waiting for 30000 ms before timing out
2015-05-05 17:59:56,315 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017
2015-05-05 17:59:56,324 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to {type=REPLICA_SET, servers=[{address=s132:27017, type=REPLICA_SET_SECONDARY, roundTripTime=8,8 ms, state=CONNECTED}]
2015-05-05 17:59:56,824 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017
2015-05-05 17:59:56,834 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to {type=REPLICA_SET, servers=[{address=s132:27017, type=REPLICA_SET_SECONDARY, roundTripTime=8,9 ms, state=CONNECTED}]
2015-05-05 17:59:57,333 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017
2015-05-05 17:59:57,343 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to {type=REPLICA_SET, servers=[{address=s132:27017, type=REPLICA_SET_SECONDARY, roundTripTime=8,8 ms, state=CONNECTED}]
2015-05-05 17:59:57,842 [cluster-ClusterId{value='5548e8fbf03cf20477e864a9', description='null'}-s132:27017] DEBUG org.mongodb.driver.cluster - Checking status of s132:27017

但是,询问主要节点 (s124) 的配置文件状态效果很好。

我的java代码是这样的:

MongoClientOptions options = MongoClientOptions.builder().
connectTimeout(2000).
socketTimeout(10000).
readPreference(ReadPreference.secondaryPreferred()).
build();
MongoCredential mc = MongoCredential.createCredential("user", "db", "XXXX".toCharArray());
MongoClient mongo = new MongoClient(new ServerAddress("s132:27017"), Lists.newArrayList(mc), options);
MongoDatabase myDB = mongo.getDatabase("myDB");

BsonDocument bd = BsonDocument.parse("{profile:1}");
Bson bson = (Bson)bd;
Document doc = myDB.runCommand(bson); //loops until timeout
Object obj = doc.get("slowms");

在 mongo shell 中我可以毫无问题地执行相同的操作:

offerStoreDE3:SECONDARY> db.runCommand({profile:1})
{ "was" : 0, "slowms" : 100, "ok" : 1 }

replSet的状态如下:

offerStoreDE3:SECONDARY> rs.status()
{
"set" : "offerStoreDE3",
"date" : ISODate("2015-05-05T16:09:47Z"),
"myState" : 2,
"syncingTo" : "s124:27017",
"members" : [
{
"_id" : 7,
"name" : "s124:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1400,
"optime" : Timestamp(1430740008, 1),
"optimeDate" : ISODate("2015-05-04T11:46:48Z"),
"lastHeartbeat" : ISODate("2015-05-05T16:09:45Z"),
"lastHeartbeatRecv" : ISODate("2015-05-05T16:09:47Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1430840794, 1),
"electionDate" : ISODate("2015-05-05T15:46:34Z")
},
{
"_id" : 8,
"name" : "s132:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1740,
"optime" : Timestamp(1430740008, 1),
"optimeDate" : ISODate("2015-05-04T11:46:48Z"),
"self" : true
}
],
"ok" : 1
}

最佳答案

runCommand(bson); 始终将命令定向到主节点,因为它在下面执行写入命令。

对于显式读取命令,必须提供读取首选项,例如:

文档 doc = myDB.runCommand(bson, ReadPreference.secondaryPreferred());

关于java - 如何使用 mongodb java 驱动程序 3.0.0 从辅助设备获取配置文件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30058223/

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