摘要:此教程基于環(huán)境下載官網(wǎng)下那個安裝包有諸多問題安裝環(huán)境變量這一步很重要,用于系統(tǒng)尋找和安裝依賴庫安裝需要版本以上依賴關(guān)系真是讓人捉急啊安裝
此教程基于 Linux CentOS 6.0, php 5.3X 環(huán)境
下載git clone https://git-wip-us.apache.org/repos/asf/thrift.git thrift
安裝PS:官網(wǎng)下那個安裝包有諸多問題
環(huán)境變量:
export PATH=$PATH:{php_src}/bin
這一步很重要,用于系統(tǒng)尋找 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 依賴關(guān)系真是讓人捉急啊...
安裝 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
創(chuàng)建 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++服務(wù)端:
thrift --gen cpp demo.thrift
會產(chǎn)生兩個文件夾 gen-php 和 gen-cpp
編譯服務(wù)端:
找到 libthrift-1.0.0-dev.so 的位置 (我機器上的位置在/usr/local/lib )
1) 在gen-cpp下
創(chuàng)建服務(wù)端文件, 這里我復(fù)制了thrfit生成的skeleton文件
cp Demo_server.skeleton.cpp Demo_server.cpp
創(chuàng)建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 下創(chuàng)建 libthrift-x86_64.conf (名字可以自定義, 以.conf結(jié)尾就行) 文件, 添加路徑/usr/local/lib.
make編譯后在文件夾下會生成demo_server.
./demo_server啟動服務(wù)端.
2) 在gen-php下
把{thrift_src}/lib/php/lib/Thrift文件夾復(fù)制到gen-php下
在demo文件下創(chuàng)建文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() . " "; }
執(zhí)行:
php client.php
服務(wù)端顯示:
hello
(全文完)
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/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
閱讀 2719·2019-08-26 13:49
閱讀 1342·2019-08-26 11:59
閱讀 1252·2019-08-26 10:31
閱讀 2170·2019-08-23 17:19