我在连接到副本集(4 db + 1 arbiter)时遇到问题。
基准:
<?php
echo "rp: nearest" . PHP_EOL;
$m = new MongoClient("mongodb://app1.n.prod,app1.m.prod,app2.m.prod,app1.d.prod/?replicaSet=booklet&readPreference=nearest&readPreferenceTags=dc:nsk");
echo "rp: primary" . PHP_EOL;
$m = new MongoClient("mongodb://app1.n.prod,app1.m.prod,app2.m.prod,app1.d.prod/?replicaSet=booklet&readPreference=primary");
echo "rp: secondary" . PHP_EOL;
$m = new MongoClient("mongodb://app1.n.prod,app1.m.prod,app2.m.prod,app1.d.prod/?replicaSet=booklet&readPreference=secondary");
echo "localhost, rs" . PHP_EOL;
$m = new MongoClient("mongodb://localhost/?replicaSet=booklet");
echo "rp: secondary, without rs" . PHP_EOL;
$m = new MongoClient("mongodb://app1.n.prod,app1.m.prod,app2.m.prod,app1.d.prod/?readPreference=secondary");
echo "localhost, without rs" . PHP_EOL;
$m = new MongoClient("mongodb://localhost");
结果(php microtime(true)中的时间):
rp: nearest 1422547256.97 1422547258.8778 rp: primary 1422547258.8778 1422547259.7998 rp: secondary 1422547259.7999 1422547260.7213 localhost, rs 1422547260.7213 1422547260.7223 rp: secondary, without rs 1422547260.7223 1422547261.4674 localhost, without rs 1422547261.4674 1422547261.4683
为什么连接到副本集需要这么长时间?(每个连接 1 秒)