两台 MongoDB 的复制集,怎么做查询负载均衡?

2015-10-21 00:30:27 +08:00
 dingyaguang117
read_preference 的选项里面,

PRIMARY: Queries are sent to the primary of the replica set.
PRIMARY_PREFERRED: Queries are sent to the primary if available, otherwise a secondary.
SECONDARY: Queries are distributed among secondaries. An error is raised if no secondaries are available.
SECONDARY_PREFERRED: Queries are distributed among secondaries, or the primary if no secondary is available.
NEAREST: Queries are distributed among all members.

最符合的似乎是 NEAREST , 但是实测只是读了本机的 MongoDB ,另外一台机器的完全空闲。。。


难道我要手动建立 2 个 MongoClient 然后随机查询某个库么。。。
1561 次点击
所在节点    问与答
6 条回复
9hills
2015-10-21 01:21:32 +08:00
Rep 推荐三台
typcn
2015-10-21 02:57:47 +08:00
查询负载均衡当然是在程序端做

从连接池取连接的时候根据两台数据库的繁忙程序随机选择
dingyaguang117
2015-10-21 10:10:22 +08:00
@typcn 根据繁忙这个 mongodb 怎么做?
dingyaguang117
2015-10-21 10:12:10 +08:00
@9hills 是的,但是现在只有两台,官方并没有提到过这种情况, sf 上还有人直接说不要这样做。。。

不过我想的也许可以从架构方面充分利用这两台机器。比如用 nginx 做分发,跑 2 个 web server,每个都 nearest
9hills
2015-10-21 10:30:19 +08:00
@dingyaguang117 我感觉你的文档是不是有问题啊,我刚查了下,对 nearest 的定义和你那个不同:

https://docs.mongodb.org/manual/reference/read-preference/

nearest Operations read from member of the replica set with the least network latency, irrespective of the member ’ s type.


继续看: https://docs.mongodb.org/manual/core/read-preference-mechanics/#replica-set-read-preference-behavior-nearest

nearest 是默认从延时<15ms 的节点读取。所以如果两台机器有一台延时>15ms ,那么就会只落在一台了

有个办法是修改这个 15ms 的值,如果两台的延时都落在这个时间内,那么是两台随机读
dingyaguang117
2015-10-21 14:55:19 +08:00
@9hills 因为 webserver 部署与 primary mongodb 在一台机器上,所以 延迟必然是本机最低了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/229685

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX