getServer() static public method

从Server列表中随机选出一个,使用status配置可以实现上线下线管理,weight(0-100)配置权重
static public getServer ( array $servers ) : mixed
$servers array
return mixed
Example #1
0
 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;
     }
 }
Example #2
0
 /**
  * 从配置中取出一个服务器配置
  * @return array
  * @throws \Exception
  */
 function getServer()
 {
     if (empty($this->servers)) {
         throw new \Exception("servers config empty.");
     }
     return Tool::getServer($this->servers);
 }