此教程基于 Linux CentOS 6.0, php 5.3X 环境php
git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
PS:官网下那个安装包有诸多问题java
环境变量:python
export PATH=$PATH:{php_src}/bin
这一步很重要,用于系统寻找 phpize 和 php-configc++
安装依赖库:git
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel openssl-devel
安装 autoconf (须要2.65版本以上):apache
wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz tar tar xzvf autoconf-latest.tar.gz cd autoconf-xxx ./configure --prefix=/usr
libtool, autoconf, automake
依赖关系真是让人捉急啊...bootstrap
安装 thrift:ruby
./bootstrap.sh ./configure --with-cpp --with-boost --without-python --without-csharp --without-java --without-erlang --without-perl --with-php --with-php_extension --without-ruby --without-haskell --without-go --without-d --without-nodjs --without-lua --without-openssl=/usr make && make install
检查工做:socket
thrift_protocol.so
则 PHP 扩展安装成功/usr/local/include/thrift/c_glib
存在则C Library安装成功/usr/local/include/thrift/(server|protocol|...)
存在则C++ Library安装成功修改 php.ini:flex
添加
extension="thrift_protocol.so"
建立 demo.thrift:
namespace cpp demo namespace php demo /* C like comments are supported */ // This is also a valid comment typedef string my_string // We can use typedef to get pretty names for the types we are using service Demo { my_string hello(1:my_string thing), }
生成PHP客户端:
thrift --gen php demo.thrift
生成C++服务端:
thrift --gen cpp demo.thrift
会产生两个文件夹 gen-php
和 gen-cpp
编译服务端:
找到 libthrift-1.0.0-dev.so
的位置 (我机器上的位置在/usr/local/lib
)
1) 在gen-cpp
下
thrfit
生成的skeleton
文件cp Demo_server.skeleton.cpp Demo_server.cpp
Makefile
GEN_SRC := Demo.cpp demo_php_constants.cpp demo_php_types.cpp GEN_OBJ := $(patsubst %.cpp,%.o, $(GEN_SRC)) THRIFT_DIR := /usr/local/include/thrift BOOST_DIR := /usr/local/include INC := -I$(THRIFT_DIR) -I$(BOOST_DIR) .PHONY: all clean all: demo_server %.o: %.cpp $(CXX) -Wall $(INC) -c $< -o $@ demo_server: Demo_server.o $(GEN_OBJ) $(CXX) -L/usr/local/lib -lthrift $^ -o $@ clean: $(RM) *.o demo_server
/etc/ld.so.conf.d
下建立 libthrift-x86_64.conf
(名字能够自定义, 以.conf
结尾就行) 文件, 添加路径/usr/local/lib
.make
编译后在文件夹下会生成demo_server
../demo_server
启动服务端.2) 在gen-php
下
{thrift_src}/lib/php/lib/Thrift
文件夹复制到gen-php
下demo
文件下建立文client.php
<?php require_once '../Thrift/ClassLoader/ThriftClassLoader.php'; require_once 'Demo.php'; require_once 'Types.php'; use Thrift\ClassLoader\ThriftClassLoader; use Thrift\Transport\TSocket; use Thrift\Transport\TBufferedTransport; use Thrift\Protocol\TBinaryProtocolAccelerated; use demo\DemoClient; $loader = new ThriftClassLoader(); $loader->register(); $loader->registerNamespace('Thrift\Base', dirname(dirname(__FILE__))); $loader->registerNamespace('Thrift\Type', dirname(dirname(__FILE__))); $loader->registerNamespace('Thrift\Exception', dirname(dirname(__FILE__))); $loader->registerNamespace('Thrift\Transport', dirname(dirname(__FILE__))); $loader->registerNamespace('Thrift\Protocol', dirname(dirname(__FILE__))); $loader->registerNamespace('Thrift\Factory', dirname(dirname(__FILE__))); $loader->registerNamespace('Thrift\StringFunc', dirname(dirname(__FILE__))); $loader->registerNamespace('demo', dirname(dirname(__FILE__))); //TBase $loader->loadClass('TBase'); //Type $loader->loadClass('TType'); $loader->loadClass('TMessageType'); //Transport $loader->loadClass('TSocket'); $loader->loadClass('TBufferedTransport'); //Protocol $loader->loadClass('TProtocol'); $loader->loadClass('TBinaryProtocolAccelerated'); //Factorys $loader->loadClass('TStringFuncFactory'); //StringFunc $loader->loadClass('Core'); //Exception $loader->loadClass('TException'); $loader->loadClass('TProtocolException'); $loader->loadClass('TApplicationException'); try { $host = '127.0.0.1'; $port = 9090; $socket = new TSocket($host ,$port); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocolAccelerated($transport); $client = new DemoClient($protocol); $transport->open(); $ret = $client->hello("Hello world!!"); echo $ret; $transport->close(); } catch (TException $e) { print 'Something went wrong: ' . $e->getMessage() . "\n"; }
执行:
php client.php
服务端显示:
hello
(全文完)