本文介绍了从mongo副本集中的特定节点读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由三个成员组成的副本集.我是否只想从两个辅助节点之一中读取?我使用以下代码,其中ip是辅助节点之一,但是我仍然看到流量已部署到其他节点.
I have a replica set of three members. Is it possible that I just want to read from one of the two secondary nodes? I use following code where the ip is one of the secondary, but I still saw the traffic was deployed to other nodes.
Mongo mongo = new MongoClient("171.21.43.34");
推荐答案
最好的方法是使用mongodb手册中所述的标签.
The best way is to use tags as stated in mongodb manual.
https://docs.mongodb.com/manual /tutorial/configure-replica-set-tag-sets/
conf = rs.conf()
conf.members[0].tags = { "offline": "false"}
conf.members[1].tags = { "offline": "false"}
conf.members[2].tags = { "offline": "true"}
rs.reconfig(conf)
在客户端中,您只需将readpreference设置为该标签
In the client, you just set the readpreference to that tag
MongoClientOptions options = MongoClientOptions
.builder()
.connectionsPerHost(config.connectionLimit)
.readPreference(TaggableReadPreference.secondaryPreferred(new TagSet(new Tag("offline", "true"))))
.socketTimeout(config.socketTimeout)
.connectTimeout(config.connectionTimeout)
.build();
mongo = new MongoClient(NewsDAOConfig.parseAddresses(config.mongoAddress), options);
这篇关于从mongo副本集中的特定节点读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!