![使用thrift做c++,java和python的相互調(diào)用_第1頁(yè)](http://file3.renrendoc.com/fileroot_temp3/2022-1/11/154f0310-493f-46f4-8a06-0a965e94deb1/154f0310-493f-46f4-8a06-0a965e94deb11.gif)
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、使用thrift做c+,java和python的相互調(diào)用linux上安裝thrift見(jiàn) c mole調(diào)用thrift要用法binary協(xié)議。thrift開(kāi)發(fā)團(tuán)隊(duì)似乎對(duì)c語(yǔ)言不太感冒。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 這時(shí)生成子名目gen-cpp,gen-java,gen-py3.生成c+服務(wù)端代碼 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 另外這個(gè)是blocking的多線程服務(wù)器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.啟動(dòng)c+ server ./userserver 7.測(cè)試c+ client ./userclient 8.寫(xiě)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(); 編譯和運(yùn)行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.寫(xiě)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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年全球技術(shù)服務(wù)合同范例
- 2025年航空、航天設(shè)備相關(guān)專用設(shè)備項(xiàng)目提案報(bào)告模式
- 2025年國(guó)際會(huì)議服務(wù)提供商合同標(biāo)準(zhǔn)
- 2025年度公司股權(quán)策劃內(nèi)部轉(zhuǎn)讓協(xié)議
- 2025年宅基地共建住宅合同樣本
- 2025年人保租賃合同格式
- 2025年不銹鋼管材訂購(gòu)合同樣本
- 2025年個(gè)人購(gòu)置家居設(shè)施合同范文
- 2025年化學(xué)品倉(cāng)庫(kù)消防隔離帶鋪設(shè)工程承包協(xié)議
- 2025年圖書(shū)策劃保密合同
- 項(xiàng)目合作備忘錄范文
- 2024年事業(yè)單位租車服務(wù)滿意度調(diào)查及改進(jìn)協(xié)議3篇
- 婦產(chǎn)科醫(yī)生個(gè)人年終述職報(bào)告課件
- 2025年全國(guó)低壓電工作業(yè)證理論考試題庫(kù)(含答案)
- 運(yùn)用PDCA提高吞咽障礙患者護(hù)理措施落實(shí)率
- JGJ-T188-2009施工現(xiàn)場(chǎng)臨時(shí)建筑物技術(shù)規(guī)范
- 教師資格考試高級(jí)中學(xué)美術(shù)學(xué)科知識(shí)與教學(xué)能力試題與參考答案(2024年)
- 2025年人教版高考生物一輪復(fù)習(xí):綜合PCR的基因工程問(wèn)題
- 鋼筋焊接工藝性試驗(yàn)方案
- 2024年福建省新高考生物試卷真題(含答案解析)
- 自然科學(xué)基金項(xiàng)目申報(bào)書(shū)(模板)
評(píng)論
0/150
提交評(píng)論