public function init($host = "127.0.0.1", $port = 8090) { if (false == $this->is_init) { $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift', $GLOBALS['THRIFT_ROOT']); $loader->registerDefinition('openflow\\master', APPPATH . 'thirdlib/'); $loader->register(); $socket = new TSocket($host, $port); $framedSocket = new TFramedTransport($socket); $this->transport = $framedSocket; $protocol = new TBinaryProtocol($this->transport); $this->transport->open(); $client = new MasterServiceClient($protocol); $this->is_init = true; return $client; } }
public function __get($name) { if (!isset($this->serviceConfig[$name]) || !is_array($this->serviceConfig[$name]) || !isset($this->serviceConfig[$name]['clientName'])) { throw new \Exception('参数配置错误'); } $config = $this->serviceConfig[$name]; $loader = new ThriftClassLoader(); // 当前service的cient类名(包含全部命名空间) $clientName = $config['clientName']; $defineName = explode('\\', ltrim($clientName, '\\'))[0]; $loader->registerDefinition($defineName, $this->genDir); if (!empty($config['defineName'])) { if (is_string($config['defineName'])) { $loader->registerDefinition($config['defineName'], $this->genDir); } elseif (is_array($config['defineName'])) { foreach ($config['defineName'] as $item) { $loader->registerDefinition($item, $this->genDir); } } } $loader->register(); // 如果有单独配置则使用单独配置,否则使用全局配置 $host = isset($config['host']) ? $config['host'] : $this->host; $port = isset($config['port']) ? $config['port'] : $this->port; $uri = isset($config['uri']) ? $config['uri'] : $this->uri; $rBufSize = isset($config['rBufSize']) ? $config['rBufSize'] : $this->rBufSize; $wBufSize = isset($config['wBufSize']) ? $config['wBufSize'] : $this->wBufSize; $transportName = '\\Thrift\\Transport\\' . (isset($config['transportName']) ? $config['transportName'] : $this->transportName); $protocolName = '\\Thrift\\Protocol\\' . (isset($config['protocolName']) ? $config['protocolName'] : $this->protocolName); if ((isset($config['requestType']) ? $config['requestType'] : $this->requestType) == 'http') { $httpName = '\\Thrift\\Transport\\' . (isset($config['httpName']) ? $config['httpName'] : $this->httpName); $this->socket = new $httpName($host, $port, $uri); } else { $socketName = '\\Thrift\\Transport\\' . (isset($config['socketName']) ? $config['socketName'] : $this->socketName); $this->socket = new $socketName($host, $port); } $this->transport = new $transportName($this->socket, $rBufSize, $wBufSize); $this->protocol = new $protocolName($this->transport); $this->transport->open(); $this->{$name} = new $clientName($this->protocol); return $this->{$name}; }
private function __construct() { $GEN_DIR = realpath(dirname(__FILE__) . '/..') . '/gen-php'; $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift', __DIR__); $loader->registerDefinition('shared', $GEN_DIR); $loader->registerDefinition('tutorial', $GEN_DIR); $loader->register(); $kafkaUrl = getconfig('kafkaUrl'); $port = getconfig('kafkaPort'); $socket = new TSocket($kafkaUrl, $port); $transport = new TBufferedTransport($socket, 1024, 1024); $transport->open(); $protocol = new TBinaryProtocol($transport); $this->client = new \com\feiniu\kafka\thrift\service\KafkaService($protocol); $this->project = getconfig('kafkaProject'); $this->topic = getconfig('kafkaTopic'); $this->kafkakey = getconfig('kafkaKey'); $this->group = getconfig('kafkaGroup'); //$this->msg = getconfig(''); }
<?php error_reporting(E_ALL); // BAD: 写死了路径 require_once '/usr/lib/php/Thrift/ClassLoader/ThriftClassLoader.php'; use Thrift\ClassLoader\ThriftClassLoader; $GEN_DIR = dirname(__FILE__) . '/gen-php'; $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift', '/usr/lib/php'); $loader->registerDefinition('Hello', $GEN_DIR); $loader->register(); if (php_sapi_name() == 'cli') { ini_set("display_errors", "stderr"); } use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TPhpStream; use Thrift\Transport\TBufferedTransport; class HelloHandler implements \Hello\HelloServiceIf { public function ping() { error_log("ping()"); } public function hello($name) { return "Hello {$name}"; } public function helloV2(\Hello\Person $person) { return "Hello {$person->firstName}, {$person->lastName}"; }
/** * Register the definitions with the Thrift class loader. */ protected function register() { // use loader only for definitions, base class should use the Composer loader $loader = new ThriftClassLoader(); // load Thrift definitions if (is_array($this->definitions)) { foreach ($this->definitions as $dnamespace => $dpath) { if (is_numeric($dnamespace)) { $dnamespace = $dpath; $dpath = ''; } $curPath = $this->genPath; if ($dpath != '') { $curPath .= '/' . $dpath; } $loader->registerDefinition($dnamespace, Yii::getAlias($curPath)); } } // Yii autoloader must be last spl_autoload_unregister(['Yii', 'autoload']); $loader->register(); spl_autoload_register(['Yii', 'autoload'], true, true); }
/** * 加载Thrift client */ private function _loadClient() { $loader = new ThriftClassLoader(); //注册命名空间 $loader->registerNamespace('Thrift', $this->t_thrift_path); //注册client service定义 $loader->registerDefinition($this->_t_client, $this->t_client_path); $loader->register(); }
/** * Register alias load. */ private function register() { $loader = new ThriftClassLoader(); foreach ($this->definitions as $alias => $path) { $loader->registerDefinition($alias, $this->path . $path); } $loader->register(); }