Mac安装Thrift
2017-08-04 13:53
267 查看
Thrift是支持多种语言的远程服务调用的框架,该框架通过自定义的
Interface Definition Language(IDL)可以基于
RPC客户端和服务端服务代码
Mac安装
Thrift
brew install thrift
其他平台安装较为麻烦需要安装一系列依赖
安装成功后执行
thrift -version
看到版本号则安装成功.
生成语言为
php的调用方式
创建一个
HelloThrift.thrift文件
namespace php HelloThrift service HelloService { string sayHello(1:string username) }
使用
thrift -r --gen php:server HelloThrift.thrift去在当前目录下生成
一个
gen-php(此处文档写的极不清楚,一开始以为是创建文件夹),并且将
gen-php改为
gen_php(此处随意)
Notice
但是还是需要下载
thrift的源码
git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
将下载的源码
lib目录下的文件
cp到刚才生成文件的根目录下
服务端代码
<?php namespace HelloThrift\php; error_reporting(E_ALL); require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; use Thrift\ClassLoader\ThriftClassLoader; $GEN_DIR = realpath(dirname(__FILE__)).'/gen_php'; $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift',__DIR__.'/lib/php/lib'); $loader->registerDefinition('HelloThrift',$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 \HelloThrift\HelloServiceIf { public function sayHello($username) { return "Hello ".$username; } } header('Content-Type','application/x-thrift'); if (php_sapi_name() == 'cli') { echo PHP_EOL; } $handler = new HelloHandler(); $processor = new \HelloThrift\HelloServiceProcessor($handler); $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W)); $protocol = new TBinaryProtocol($transport,true,true); $transport->open(); $processor->process($protocol,$protocol); $transport->close();
客户端代码
<?php namespace HelloThrift\php; error_reporting(E_ALL); require_once __DIR__.'/lib/php/lib/Thrift/ClassLoader/ThriftClassLoader.php'; use Thrift\ClassLoader\ThriftClassLoader; $GEN_DIR = realpath(dirname(__FILE__)).'/gen_php'; $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift',__DIR__.'/lib/php/lib'); $loader->registerDefinition('HelloThrift',$GEN_DIR); $loader->register(); use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TSocket; use Thrift\Transport\THttpClient; use Thrift\Transport\TBufferedTransport; use Thrift\Exception\TException; try { if (array_search('--http',$argv)) { $socket = new THttpClient('localhost',8080,'/Server.php'); } else { $socket = new TSocket('localhost',9090); } $transport = new TBufferedTransport($socket,1024,1024); $protocol = new TBinaryProtocol($transport); $client = new \HelloThrift\HelloServiceClient($protocol); $transport->open(); echo $client->sayHello(" World! "); $transport->close(); } catch (\Exception $e) { print 'TException:'.$e->getMessage().PHP_EOL; }
一定要确保能够引入配置文件才可以。
在根目录运行
php -S localhost:8080
客户端执行
php Client --http可以看到
hello即为成功
相关文章推荐
- Mac下安装降级版thrift
- mac安装thrift-0.9.1
- 在Mac OS X 10.9上安装 Thrift 0.9.1
- Mac下安装Thrift
- MAC下thrift安装
- thrift的安装以及与http网络性能对比(MAC环境)
- Mac安装thrift-0.10.0
- IOS/MAC环境下thrift的安装
- mac下安装thrift
- Mac安装thrift因bison报错的解决办法
- Mac 10.9.2 安装thrift
- mac安装thrift
- MAC/linux 下Thrift安装
- Mac上安装Thrift
- 解决Mac安装thrift因bison报错的问题
- 【Thrift】Mac 安装
- mac 下如何安装上thrift
- mac 安装thrift
- VMWare下安装MAC OS X Snow Leopard 10.6
- mac 安装mysql