thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。
thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。
thrift允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。
类似Thrift的工具,还有Avro、protocol buffer,但相对于Thrift来讲,都没有Thrift支持全面和使用广泛。
一.下载最新的thrift源码(包含thrift主程序和各个语言类库和扩展包)
http://thrift.apache.org/
二.安装程序
1.在linux上安装thrift程序,主要提供thrift文件生成php或者其他语言类库。
2.安装thrift扩展
wget http://archive.apache.org/dist/thrift/0.10.0/thrift-0.10.0.tar.gz
tar -xf thrift-0.10.0.tar.gz
cd ./thrift-0.10.0/lib/php/src/ext/thrift_protocol
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/home/work/php/bin/php-config --enable-thrift_protocol
$ make
$ make install
主要用到了thrift_protocol_write_binary和thrift_protocol_read_binary方法
修改php.ini
extension=/home/work/php/lib/php/extensions/no-debug-non-zts-20131226/thrift_protocol.so
重新启动一下php服务就可以了
三.使用说明
根据已有的thrift文件,生成php类库。
thrift -r --gen php test.thrift
会生成一个gen-php目录,里边就是相关协议代码
因为从0.10版本,thrift接口变成名字服务的方式,所以需要php5以上版本才能支持。
所以,要在test.thrift里增加一行namespace php testname,这个名字比较重要,需要在链接的时候配置。
把thrift-0.10.0\thrift-0.10.0\lib\php\lib\Thrift和gen-php拷贝到你自己的类库目录下。如果用的是codeigniter框架,拷贝到third_party里
写一个类代码如下:
require_once APPPATH . 'third_party/thrift/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TSocketPool;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TBufferedTransport;
class Thrift_library
{
public $config;
/** @var CI_Controller $CI */
private $CI;
public function __construct(array $config)
{
$this->config = $config;
$this->CI = &get_instance();
}
public function tconnet($tdata)
{
$thrift_lib = APPPATH . 'third_party/thrift/';
$thrift_gen = APPPATH . 'third_party/thrift/gen-php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift',$thrift_lib);
$loader->registerDefinition('testname', $thrift_gen);
$loader->register();
$thriftHost = $this->config['service_ip'];
$thriftPort = $this->config['service_port'];
$socket = new TSocket($thriftHost,$thriftPort);
$socket->setSendTimeout($this->config['time_out']);#Sets the send timeout.
$socket->setRecvTimeout($this->config['time_out']);#Sets the receive timeout.
$transport = new TBufferedTransport($socket);
//$transport = new TFramedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new \testname\searcher_thriftClient($protocol);#
//print_r($client);exit;
$transport->open();
$socket->setDebug(TRUE);
//echo $tdata['pdata'];exit;
$ret = $client->search($tdata['head'], 1, $tdata['query'], $tdata['pdata']);
//error_code result_num results
if($ret->error_code!=0){
ErrorCode::logErrorMsg(ErrorCode::LEVEL_ERROR, ErrorCode::ERR_THRIFT);
}else{
$transport->close();
return $ret->results;
}
}
}
注意上边类库里,testname的两个地方,要和thrift文件里的保持一致!
这样客户端就ok了!测试一下吧!
如果发送数据格式不对,会报超时错误。
参考资料:
http://studygolang.com/articles/3110
http://blog.csdn.net/heiyeshuwu/article/details/5982222
http://www.cnblogs.com/lpit/p/4902292.html
http://www.nonb.cn/blog/thrift-in-php.html
thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。
thrift允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。
类似Thrift的工具,还有Avro、protocol buffer,但相对于Thrift来讲,都没有Thrift支持全面和使用广泛。
一.下载最新的thrift源码(包含thrift主程序和各个语言类库和扩展包)
http://thrift.apache.org/
二.安装程序
1.在linux上安装thrift程序,主要提供thrift文件生成php或者其他语言类库。
2.安装thrift扩展
wget http://archive.apache.org/dist/thrift/0.10.0/thrift-0.10.0.tar.gz
tar -xf thrift-0.10.0.tar.gz
cd ./thrift-0.10.0/lib/php/src/ext/thrift_protocol
$ /usr/local/php/bin/phpize
$ ./configure --with-php-config=/home/work/php/bin/php-config --enable-thrift_protocol
$ make
$ make install
主要用到了thrift_protocol_write_binary和thrift_protocol_read_binary方法
修改php.ini
extension=/home/work/php/lib/php/extensions/no-debug-non-zts-20131226/thrift_protocol.so
重新启动一下php服务就可以了
三.使用说明
根据已有的thrift文件,生成php类库。
thrift -r --gen php test.thrift
会生成一个gen-php目录,里边就是相关协议代码
因为从0.10版本,thrift接口变成名字服务的方式,所以需要php5以上版本才能支持。
所以,要在test.thrift里增加一行namespace php testname,这个名字比较重要,需要在链接的时候配置。
把thrift-0.10.0\thrift-0.10.0\lib\php\lib\Thrift和gen-php拷贝到你自己的类库目录下。如果用的是codeigniter框架,拷贝到third_party里
写一个类代码如下:
require_once APPPATH . 'third_party/thrift/Thrift/ClassLoader/ThriftClassLoader.php';
use Thrift\ClassLoader\ThriftClassLoader;
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\TSocketPool;
use Thrift\Transport\TFramedTransport;
use Thrift\Transport\TBufferedTransport;
class Thrift_library
{
public $config;
/** @var CI_Controller $CI */
private $CI;
public function __construct(array $config)
{
$this->config = $config;
$this->CI = &get_instance();
}
public function tconnet($tdata)
{
$thrift_lib = APPPATH . 'third_party/thrift/';
$thrift_gen = APPPATH . 'third_party/thrift/gen-php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift',$thrift_lib);
$loader->registerDefinition('testname', $thrift_gen);
$loader->register();
$thriftHost = $this->config['service_ip'];
$thriftPort = $this->config['service_port'];
$socket = new TSocket($thriftHost,$thriftPort);
$socket->setSendTimeout($this->config['time_out']);#Sets the send timeout.
$socket->setRecvTimeout($this->config['time_out']);#Sets the receive timeout.
$transport = new TBufferedTransport($socket);
//$transport = new TFramedTransport($socket);
$protocol = new TBinaryProtocol($transport);
$client = new \testname\searcher_thriftClient($protocol);#
//print_r($client);exit;
$transport->open();
$socket->setDebug(TRUE);
//echo $tdata['pdata'];exit;
$ret = $client->search($tdata['head'], 1, $tdata['query'], $tdata['pdata']);
//error_code result_num results
if($ret->error_code!=0){
ErrorCode::logErrorMsg(ErrorCode::LEVEL_ERROR, ErrorCode::ERR_THRIFT);
}else{
$transport->close();
return $ret->results;
}
}
}
注意上边类库里,testname的两个地方,要和thrift文件里的保持一致!
这样客户端就ok了!测试一下吧!
如果发送数据格式不对,会报超时错误。
参考资料:
http://studygolang.com/articles/3110
http://blog.csdn.net/heiyeshuwu/article/details/5982222
http://www.cnblogs.com/lpit/p/4902292.html
http://www.nonb.cn/blog/thrift-in-php.html