public function connectCardCenter() { $config = Yaf_Application::app()->getConfig(); $host = $config->cardcenter->host; $port = $config->cardcenter->port; $socket = new Thrift\Transport\TSocket($host, $port); $socket->setSendTimeout(30000); $socket->setRecvTimeout(20000); $transport = new Thrift\Transport\TFramedTransport($socket); $protocol = new Thrift\Protocol\TBinaryProtocol($transport); $client = new Thrift\Server\CardCenterServiceClient($protocol); $transport->open(); return $client; }
/** * 获取一个实例 * @return instance */ protected function __instance() { // 获取一个服务端节点地址 $address = AddressManager::getOneAddress($this->serviceName); list($ip, $port) = explode(':', $address); // Transport $socket = new \Thrift\Transport\TSocket($ip, $port); //timeout config $socket->setSendTimeout($this->sendTimeout); $socket->setRecvTimeout($this->recvTimeout); $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 = ThriftClient::getServiceDir($this->serviceName); // 类不存在则报出异常 if (!class_exists($class_name)) { throw new \Exception("Class {$class_name} not found in directory {$service_dir}"); } // 初始化一个实例 return new $class_name($protocol); }
/** * 获取一个实例 * @return instance */ protected function __instance() { if (\Thrift\Context::get('serverName') != $this->serviceName) { \Thrift\Context::put('serverName', $this->serviceName); } $address = CLientForTest::getOneAddress($this->serviceName); list($ip, $port) = explode(':', $address); $socket = new \Thrift\Transport\TSocket($ip, $port); // 接收超时 if (($timeout = \Thrift\Context::get('timeout')) && $timeout >= 1) { $socket->setRecvTimeout($timeout * 1000); } else { // 默认30秒 $socket->setRecvTimeout(30000); } $transport = new \Thrift\Transport\TFramedTransport($socket); $pname = \Thrift\Context::get('protocol') ? \Thrift\Context::get('protocol') : 'binary'; $protocolName = self::getProtocol($pname); $protocol = new $protocolName($transport); $classname = '\\Provider\\' . $this->serviceName . "\\" . $this->serviceName . "Client"; if (!class_exists($classname)) { $this->includeFile($classname); } try { $transport->open(); } catch (\Exception $e) { CLientForTest::kickAddress($address); throw $e; } return new $classname($protocol); }
$hbasePort = 9090; $GLOBALS['THRIFT_ROOT'] = '/usr/share/php'; define("DEBUG", true); // Thrift stuff require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/ClassLoader/ThriftClassLoader.php'; $classLoader = new Thrift\ClassLoader\ThriftClassLoader(); $classLoader->registerNamespace('Thrift', $GLOBALS['THRIFT_ROOT']); $classLoader->register(); require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TSocket.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Transport/TBufferedTransport.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Protocol/TBinaryProtocol.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Packages/Hbase/Hbase.php'; require_once $GLOBALS['THRIFT_ROOT'] . '/Thrift/Packages/Hbase/Types.php'; $socket = new Thrift\Transport\TSocket($hbaseHost, $hbasePort); $socket->setSendTimeout(10000); $socket->setRecvTimeout(20000); $transport = new Thrift\Transport\TBufferedTransport($socket); $protocol = new Thrift\Protocol\TBinaryProtocol($transport); $client = new Hbase\HbaseClient($protocol); /* * BEGIN */ // Parse arguments if (DEBUG) { echo "Parse options\n"; } $options = getopt("t:n:f:"); $listType = @$options["t"]; $listName = @$options["n"]; $listFile = @$options["f"]; if (strlen($listType) == 0 || strlen($listName) == 0 || strlen($listFile) == 0) {