使用thrift做c++,java和python的相互調(diào)用_第1頁
免費預覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、使用thrift做c+,java和python的相互調(diào)用linux上安裝thrift見 c mole調(diào)用thrift要用法binary協(xié)議。thrift開發(fā)團隊似乎對c語言不太感冒。1.定義l文件acser.thrift suct user 1: string uid, 2: string , 3: bool us, 4: i16 uage, service userservice void a(1: user u), user get(1: string uid), 2.生成c+,java和python代碼框架 thrift -r -gen p user.thrift thrift -r -

2、gen java user.thrift thrift -r -gen py user.thrift 這時生成子名目gen-cpp,gen-java,gen-py3.生成c+服務端代碼 cp gen-cpp/userservice_server.skeleton.cpp userserver.cpp 修改userserver.cpp ilude "userservice.h" include config.h /include proto/tbinaryprotocol.h include protocol/tcompactprotocol.h include server

3、/tsimpleserver.h include transport/tserversocket.h include transport/tbuffertransports.h include concurrency/threadmanager.h include concurrency/posixthreadfactory.h include server/tthreadpoolserver.h include server/tthreadserver.h using namespace :apache:thrift; using namespace :apache:thrift:proto

4、col; using namespace :apache:thrift:transport; using namespace :apache:thrift:server; using namespace :apache:thrift:concurrency; using boost:shared_ptr; class userservicehandler : virtual public userserviceif public: userservicehandler() / your initialization goes here void add(const user u) / your

5、 implementation goes here printf("uid=%s uname=%s usex=%d uage=%dn", u.uid.c_str(), u.uname.c_str(), u.usex, u.uage); void get(user _return, const std:string uid) / your implementation goes here _return.uid = "leo1" _return.uname = "yueyue" _return.usex = 1; _return.uag

6、e = 3; printf("uid=%s uname=%s usex=%d uage=%dn", _return.uid.c_str(), _return.uname.c_str(), _return.usex, _return.uage); int main(int argc, char *argv) shared_ptr userservicehandler handler(new userservicehandler(); shared_ptr tprocessor processor(new userserviceprocessor(handler); share

7、d_ptr tprotocolfactory protocolfactory(new tcompactprotocolfactory(); shared_ptr ttransportfactory transportfactory(new tbufferedtransportfactory(); shared_ptr tservertransport servertransport(new tserversocket(9090); shared_ptr threadmanager threadmanager = threadmanager:newsimplethreadmanager(10);

8、 shared_ptr posixthreadfactory threadfactory = shared_ptr posixthreadfactory (new posixthreadfactory(); threadmanager- threadfactory(threadfactory); threadmanager- start(); printf("start user server.n"); tthreadpoolserver server(processor, servertransport, transportfactory, protocolfactory

9、, threadmanager); server.serve(); return 0; 注重這段代碼用法tcompactprotocol,需要include config.h 另外這個是blocking的多線程服務器4.生成c+的client文件userclient.cpp include "userservice.h" include config.h include transport/tsocket.h include transport/tbuffertransports.h include protocol/tcompactprotocol.h using nam

10、espace apache:thrift; using namespace apache:thrift:protocol; using namespace apache:thrift:transport; using boost:shared_ptr; int main(int argc, char *argv) boost:shared_ptr tsocket socket(new tsocket("localhost", 9090); boost:shared_ptr ttransport transport(new tbufferedtransport(socket)

11、; boost:shared_ptr tprotocol protocol(new tcompactprotocol(transport); transport- open(); user u; u.uid = "leo" u.uname = "yueyue" u.usex = 1; u.uage = 3; userserviceclient client(protocol); client.add(u); user u1; client.get(u1,"lll"); transport- close(); printf("

12、uid=%s uname=%s usex=%d uage=%dn", u1.uid.c_str(), u1.uname.c_str(), u1.usex, u1.uage); return 0; 5.生成make boost_dir = /usr/local/include/boost/ thrift_dir = /usr/local/include/thrift lib_dir = /usr/local/lib gen_src = ./gen-cpp/acsuser_types.cpp ./gen-cpp/acsuser_constants.cpp ./gen-cpp/userse

13、rvice.cpp default: server client server: userserver.cpp g+ -g -o userserver -i$thrift_dir -i$boost_dir -i./gen-cpp -l$lib_dir -lthrift userserver.cpp $gen_src client: userclient.cpp g+ -g -o userclient -lm -pthread -lz -lrt -sl -i$thrift_dir -i$boost_dir -i./gen-cpp -l$lib_dir -lthrift userclient.cp

14、p $gen_src clean: $(rm) -r userserver userclient 6.啟動c+ server ./userserver 7.測試c+ client ./userclient 8.寫java client文件userclient.java import org.apache.thrift.texception; import tocol.tcompactprotocol; import tocol.tprotocol; import org.apache.thrift.transp

15、ort.tframedtransport; import org.apache.thrift.transport.tnonblockingsocket; import org.apache.thrift.transport.tsocket; import org.apache.thrift.transport.ttransport; import org.apache.thrift.transport.ttransportexception; /import userservice.client; public class userclient private void start() try

16、 ttransport socket = new tsocket("localhost", 9090); /ttransport transport = new tframedtransport(socket); tprotocol protocol = new tcompactprotocol(socket); userservice.client client = new userservice.client(protocol); socket.open(); system.out.print(client.get("lll"); user u =

17、new user(); u.uid="leojava" u.uname="yueyue" u.usex=true; u.uage=3; client.add(u); socket.close(); ch (ttransportexception e) e.printstacktrace(); catch (texception e) e.printstacktrace(); public ic void main(string args) userclient c = new userclient(); c.start(); 編譯和運行java clie

18、nt javac -classpath /usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/log4j-1.2.14.jar:/usr/local/lib/commons-logging-1.1.1.jar:/usr/local/lib/slf4j-api-1.5.8.jar userclient.java ./gen-java/*.java java -classpath .:./gen-java:/usr/local/lib/libthrift-0.7.0.jar:/usr/local/lib/log4j-1.2.14.jar:/usr/lo

19、cal/lib/commons-logging-1.1.1.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/slf4j-log4j12-1.5.8.jar userclient 9.寫python client文件pythonclient.py !/usr/bin/env python import sys sys.path.append('./gen-py') from acsuser import userservice from acsuser.ttypes import * from thrift import thrift from thrift.transport import tsocket from thrift.transport import ttransport from tocol import tcompactprotoc

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論