protected function getDB($type = self::DB_SLAVE) { if ($this->forceMaster) { goto master; } //只读的语句 if ($type == self::DB_SLAVE) { if (empty($this->slaveDB)) { //连接到从库 $config = $this->config; //从从库中随机选取一个 $server = Swoole\Tool::getServer($config['slaves']); unset($config['slaves'], $config['use_proxy']); $config['host'] = $server['host']; $config['port'] = $server['port']; $this->slaveDB = $this->connect($config); } return $this->slaveDB; } else { master: if (empty($this->masterDB)) { //连接到主库 $config = $this->config; unset($config['slaves'], $config['use_proxy']); $this->masterDB = $this->connect($config); } return $this->masterDB; } }
/** * 从配置中取出一个服务器配置 * @return array * @throws \Exception */ function getServer() { if (empty($this->servers)) { throw new \Exception("servers config empty."); } return Tool::getServer($this->servers); }