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); }
<?php require 'vendor/autoload.php'; require 'gen-php/helloSvc.php'; $msg = 'No server response'; if ($_SERVER['REQUEST_METHOD'] === 'POST' && array_key_exists('hello_form', $_POST) && $_POST['hello_form'] == "Submit") { try { $trasn_ep = new Thrift\Transport\TSocket('localhost', 9095); $trans_buf = new Thrift\Transport\TBufferedTransport($trasn_ep); $proto = new Thrift\Protocol\TBinaryProtocol($trans_buf); $client = new helloSvcClient($proto); $trasn_ep->open(); $msg = $client->getMessage($_POST['user_name']); $trasn_ep->close(); } catch (Thrift\Exception\TException $tx) { error_log('TException: ' . $tx->getMessage() . "\n"); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Hello Thrift</title> </head> <body> <form name="hello_form" method="post" action="hello_client.php"> Name: <input type="text" name="user_name"> <input type="submit" name="hello_form" value="Submit">
$hbaseHost = "localhost"; /* Host or IP of your HBase */ $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"];
/** * 处理受到的数据 * @param TcpConnection $connection * @return void */ public function onConnect($connection) { $socket = $connection->getSocket(); $t_socket = new Thrift\Transport\TSocket(); $t_socket->setHandle($socket); $transport_name = '\\Thrift\\Transport\\' . $this->thriftTransport; $transport = new $transport_name($t_socket); $protocol_name = '\\Thrift\\Protocol\\' . $this->thriftProtocol; $protocol = new $protocol_name($transport); // 执行处理 try { // 先初始化一个 $protocol->fname == 'none'; // 统计开始时间 \Thrift\Statistics\StatisticClient::tick(); // 业务处理 $this->processor->process($protocol, $protocol); \Thrift\Statistics\StatisticClient::report($this->name, $protocol->fname, 1, 0, '', $this->statisticAddress); } catch (\Exception $e) { \Thrift\Statistics\StatisticClient::report($this->name, $protocol->fname, 0, $e->getCode(), $e, $this->statisticAddress); ThriftWorker::log('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage() . "\n" . $e->getTraceAsString() . "\nCLIENT_IP:" . $connection->getRemoteIp() . "\n"); $connection->send($e->getMessage()); } }
/** * 处理受到的数据 * @param event_buffer $event_buffer * @param int $fd * @return void */ public function dealInputBase($connection, $flag, $fd = null) { $this->currentDealFd = (int) $connection; if (feof($connection)) { $this->statusInfo['client_close']++; return $this->closeClient($this->currentDealFd); } $socket = new Thrift\Transport\TSocket(); $socket->setHandle($connection); $transport = new $this->thriftTransport($socket); $protocol = new $this->thriftProtocol($transport); // 执行处理 try { // 先初始化一个 $protocol->fname == 'none'; // 统计开始时间 \Thrift\Statistics\StatisticClient::tick(); // 业务处理 $this->processor->process($protocol, $protocol); \Thrift\Statistics\StatisticClient::report($this->workerName, $protocol->fname, 1, 0, '', $this->statisticAddress); } catch (\Exception $e) { \Thrift\Statistics\StatisticClient::report($this->workerName, $protocol->fname, 0, $e->getCode(), $e, $this->statisticAddress); $this->notice('CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage() . "\n" . $e->getTraceAsString() . "\nCLIENT_IP:" . $this->getRemoteIp() . "\nBUFFER:[" . var_export($this->recvBuffers[$fd]['buf'], true) . "]\n"); $this->statusInfo['throw_exception']++; $this->sendToClient($e->getMessage()); } // 是否是长连接 if (!$this->isPersistentConnection) { $this->closeClient($fd); } // 检查是否是关闭状态或者是否到达请求上限 if ($this->workerStatus == self::STATUS_SHUTDOWN || $this->statusInfo['total_request'] >= $this->maxRequests) { // 停止服务 $this->stop(); // EXIT_WAIT_TIME秒后退出进程 pcntl_alarm(self::EXIT_WAIT_TIME); } }