摘要:此教程基于环境下载官网下那个安装包有诸多问题安装环境变量这一步很重要,用于系统寻找和安装依赖库安装需要版本以上依赖关系真是让人捉急啊安装
此教程基于 Linux CentOS 6.0, php 5.3X 环境
下载git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
安装PS:官网下那个安装包有诸多问题
环境变量:
export PATH=$PATH:{php_src}/bin
这一步很重要,用于系统寻找 phpize 和 php-config
安装依赖库:
yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel openssl-devel
安装 autoconf (需要2.65版本以上):
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 依赖关系真是让人捉急啊...
安装 thrift:
./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
检查工作:
查看PHP扩展安装目录中有 thrift_protocol.so 则 PHP 扩展安装成功
查看 /usr/local/include/thrift/c_glib 存在则C Library安装成功
查看 /usr/local/include/thrift/(server|protocol|...) 存在则C++ Library安装成功
修改 php.ini:
添加
extension="thrift_protocol.so"Demo
创建 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
添加 ld 路径: /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
register(); $loader->registerNamespace("ThriftBase", dirname(dirname(__FILE__))); $loader->registerNamespace("ThriftType", dirname(dirname(__FILE__))); $loader->registerNamespace("ThriftException", dirname(dirname(__FILE__))); $loader->registerNamespace("ThriftTransport", dirname(dirname(__FILE__))); $loader->registerNamespace("ThriftProtocol", dirname(dirname(__FILE__))); $loader->registerNamespace("ThriftFactory", dirname(dirname(__FILE__))); $loader->registerNamespace("ThriftStringFunc", 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() . " "; }
执行:
php client.php
服务端显示:
hello
(全文完)
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/20743.html
阅读 1536·2023-04-26 02:08
阅读 3128·2021-10-14 09:42
阅读 7177·2021-09-22 15:34
阅读 3236·2019-08-30 13:16
阅读 2718·2019-08-26 13:49
阅读 1342·2019-08-26 11:59
阅读 1251·2019-08-26 10:31
阅读 2170·2019-08-23 17:19