]# yum install boost-devel-static libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev ant
先下載thrift-0.9.3.tar.gz,解壓後進入thrift-0.9.3目錄php
//須要支持的語言用--with, 不須要支持的語言用--without, 像ruby等語言最好去掉,不然可能會有一些兼容問題 ]# ./configure --with-cpp --with-boost --with-python --without-csharp --with-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell --without-go ]# make ]# make install //成功會顯示 BUILD SUCCESSFUL,經過thrift命令查看是否安裝成功 ]# thrift
//安裝Thrift的時候遇到,以下錯誤 #./configure --prefix=/usr/local/thrift trhift configure: error: "Error: libcrypto required." //解決辦法: //安裝 openssl openssl-devel (centOS) #yum -y install openssl openssl-devel # ./configure --prefix=/usr/local/thrift
]# pip install thrift==0.9.3
//建立schema目錄,建立一個schema文件RecSys.thrift [root@localhost schema]# cat RecSys.thrift service RecSys{ string rec_data(1:string data) }
]# thrift --gen python RecSys.thrift
// client代碼: #coding=utf=8 import sys sys.path.append('../schema/gen-py') from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from RecSys import RecSys try: # 設置端口 transport = TSocket.TSocket('localhost', port=9090) # 設置傳輸層 transport = TTransport.TBufferedTransport(transport) # 設置傳輸協議 protocol = TBinaryProtocol.TBinaryProtocol(transport) client = RecSys.Client(protocol) transport.open() rst = client.rec_data("are you ok!!!") print "receive return data: ", rst transport.close() except Thrift.TException, ex: print "%s" % (ex.message)
// server 代碼 #coding=utf=8 import sys sys.path.append('../schema/gen-py') from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol from thrift.server import TServer from RecSys import RecSys from RecSys.ttypes import * class RecSysHandler(RecSys.Iface): def rec_data(self, a): print "Receive: %s" %(a) return "I'm OK !!!" if __name__ == "__main__": # 實例化handler handler = RecSysHandler() # 設置processor processor = RecSys.Processor(handler) # 設置端口 transport = TSocket.TServerSocket('localhost', port=9090) # 設置傳輸層 tfactory = TTransport.TBufferedTransportFactory() # 設置傳輸協議 pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = TServer.TThreadedServer(processor, transport, tfactory, pfactory) print 'Starting the server...' server.serve() print 'done.'
//在schema目錄下執行 ]# thrift --gen cpp RecSys.thrift
]# g++ -g -Wall -I/usr/local/include/thrift RecSys_constants.cpp RecSys.cpp RecSys_server.skeleton.cpp RecSys_types.cpp -lthrift -o server
23 void rec_data(std::string& _return, const std::string& data) { 24 // Your implementation goes here 25 std::cout << "Recevie data: " << data << std::endl; 26 _return = "I'm OK !!!"; 27 }
]# ./server
//若是執行報錯: libthrift-0.9.3.so: cannot open shared object file: No such file or directory 解決方法: ]# vim /etc/ld.so.conf 在末尾添加下面一行 /usr/local/lib/ 而後再執行: ]# ldconfig
//python的client端調用c++的server端 ]# python client.py
在gen-cpp目錄下建立一個Makefile文件java
GXX = g++ FLAGS = -g -Wall INCLUDES = -I/usr/local/include/thrift LIBS = -L/usr/local/lib/*.so -lthrift SERVER_OBJECTS = RecSys_constants.cpp RecSys.cpp RecSys_server.skeleton.cpp RecSys_types.cpp CLIENT_OBJECTS = RecSys.cpp client.cpp server: $(GXX) $(INCLUDES) $(SERVER_OBJECTS) $(LIBS) -o server client: $(GXX) $(INCLUDES) $(CLIENT_OBJECTS) $(LIBS) -o client .PHONY: clean clean: rm -rf server //備註:.PHONY: clean 是爲了防止當前目錄下有clean同名文件,致使clean刪除命令沒法執行
#include "RecSys.h" #include <iostream> #include <string> #include <transport/TSocket.h> #include <transport/TBufferTransports.h> #include <protocol/TBinaryProtocol.h> using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using namespace std; int main(int argc, char **argv) { boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090)); boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket)); boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); transport->open(); RecSysClient client(protocol); string send_data = "can you help me ???"; string recevie_data; client.rec_data(recevie_data, send_data); cout << "Send data:" << send_data << endl; cout << "Receive data:" << recevie_data << endl; transport->close(); }