mongo驱动程序如何选择要连接的节点?

时间:2016-11-22 10:29:53

标签: java mongodb database-replication

在文档MongoClient中,mongo驱动程序选择使用最近的(ping)mongo节点。但写请求只能由主节点处理。能否请您解释一下驱动程序使用节点连接的方式?假设我有一个副本集(从使用驱动程序的机器连接):

rs.status()
set: "mySet", members:[
  {name: "mongo1", stateStr: "PRIMARY", pingMs: 900}
  {name: "mongo2", stateStr: "SECONDARY", pingMs: 500}
  {name: "mongo3", stateStr: "SECONDARY", pingMs: 100}
]

驱动程序设置:

MongoClientOptions options = new MongoClientOptions.Builder()
  .readPreference(secondaryPreferred()) //please mention
  .build()
List<ServerAddress> nodes = asList(
  new ServerAddress("mongo1"),
  new ServerAddress("mongo2")
) //no "mongo3"
new MongoClient(options, nodes);

mongo实例会选择什么?写请求将如何路由?

0 个答案:

没有答案