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; }
/** * sms_send * * @param string $phone * @param string $message * @return boolean */ public static function create() { if (!extension_loaded('thrift_protocol')) { throw new Exception('Thrift_protocol extension not found.'); } $generatorConfig = Yaf_Registry::get('config')->generator; if (empty($generatorConfig)) { return false; } try { $socket = new Thrift\Transport\TSocket($generatorConfig->host, $generatorConfig->port); $transport = new Thrift\Transport\TFramedTransport($socket); $protocol = new Thrift\Protocol\TBinaryProtocol($transport); Yaf_Loader::import(__DIR__ . '/gen-php/generator_service.php'); $client = new generator_serviceClient($protocol); $transport->open(); $uid = $client->gen_id(); $transport->close(); return $uid; } catch (Thrift\Exception\TException $tx) { throw new Exception($tx->getMessage()); } }
/** * 获取一个实例 * @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 define('APPLICATION_PATH_DIR', dirname(dirname(__DIR__)) . "/application"); define('THRIFT_DIR_PATH', dirname(APPLICATION_PATH_DIR) . "/thrift"); require_once THRIFT_DIR_PATH . "/Thrift/ClassLoader/ThriftClassLoader.php"; $loader = new Thrift\ClassLoader\ThriftClassLoader(); $loader->registerNamespace('Thrift', THRIFT_DIR_PATH); $loader->registerNamespace('swoole', THRIFT_DIR_PATH); $loader->registerNamespace('Bin', THRIFT_DIR_PATH); $loader->registerDefinition('Bin', THRIFT_DIR_PATH); $loader->register(); define('APPLICATION_PATH', dirname(dirname(__DIR__)) . "/application"); $application = new Yaf_Application(dirname(APPLICATION_PATH_DIR) . "/conf/application.ini"); $application->bootstrap(); $config_obj = Yaf_Registry::get("config"); $rpc_config = $config_obj->rpc->toArray(); $socket = new Thrift\Transport\TSocket($rpc_config['host'], $rpc_config['port']); $transport = new Thrift\Transport\TFramedTransport($socket); $protocol = new Thrift\Protocol\TBinaryProtocol($transport); $transport->open(); $client = new Bin\rpc\rpcClient($protocol); $message = new Bin\rpc\Message(array('name' => 'userinfo', 'result' => '{"id":37936,"name"=>"zqf",email:"*****@*****.**"}')); $ret = $client->sendMessage($message); var_dump($ret); $transport->close();