Example #1
0
 /**
  * 获取一个实例
  * @return instance
  */
 protected function __instance()
 {
     // 获取一个服务端节点地址
     $address = AddressManager::getOneAddress($this->serviceName);
     list($ip, $port) = explode(':', $address);
     // Transport
     $socket = new TSocket($ip, $port);
     //        $socket = new \Thrift\Transport\TSocket($ip, $port);
     $transport_name = ThriftClient::getTransport($this->serviceName);
     $transport = new $transport_name($socket);
     // Protocol
     $protocol_name = ThriftClient::getProtocol($this->serviceName);
     $protocol = new $protocol_name($transport);
     try {
         $transport->open();
     } catch (Exception $e) {
         // 无法连上,则踢掉这个地址
         AddressManager::kickAddress($address);
         throw $e;
     }
     // 客户端类名称
     $class_name = "\\Services\\" . $this->serviceName . "\\" . $this->serviceName . "Client";
     // 类不存在则尝试加载
     /*
     if(!class_exists($class_name))
     {
         $service_dir = $this->includeFile();
         if(!class_exists($class_name))
         {
             throw new Exception("Class $class_name not found in directory $service_dir");
         }
     }
     */
     // 初始化一个实例
     return new $class_name($protocol);
 }