facebook rpc框架 thrift的編譯 及 夸語言調(diào)用(c++服務(wù)端, java 客戶端)

2. 協(xié)議制定
本例子協(xié)議如下:
struct User{
? 1:i64 id,
? 2:string name,
? 3:i32 age,
? 4:bool vip
}

service Test{
i32 add(1:i32 a,2:i32 b)
User getById(1:i64 id)
}

3. 代碼自動(dòng)生成---------生成cpp, java源碼
利用工具thrift-0.9.0.exe,生成c++代碼,命令為:thrift -r --gen cpp user.thrift
生成java代碼,命令為:thrift -r --gen java person.thrift

4. c++ 服務(wù)端工程搭建
4.1 將步驟三產(chǎn)生的cpp代碼拷貝到服務(wù)端程序目錄下:

4.2? 添加庫依賴?? LibThrift.lib;ws2_32.lib;
../THIRD_PARTY\thrift\lib;../THIRD_PARTY\boost\boost_1_59_0\lib\windows;../THIRD_PARTY\openssl\lib;

4.3 添加文件依賴: ../THIRD_PARTY\thrift;../THIRD_PARTY\boost\boost_1_59_0;

4.4 服務(wù)端代碼如下:
#include "Test.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;

class TestHandler : virtual public TestIf {
public:

? TestHandler() {
? ? // Your initialization goes here
? }

? int32_t add(const int32_t a, const int32_t b) {

? ? // Your implementation goes here

? printf("add\n");

? return a + b;

? }

? void getById(User& _return, const int64_t id) {

? ? // Your implementation goes here

? // Your implementation goes here

? User *user;

? std::list<User *>::iterator iter = m_listUser.begin(),iterEnd = m_listUser.end();

? for(iter; iter!=iterEnd; iter++)
? {

? user = *iter;

? if(user->id == id)
? {

? _return.id = user->id;

? _return.__set_name(user->name.c_str());

? _return.age = user->age;

? _return.__set_vip(user->vip);

? break;
? }
? }

? printf("getById\n");

? }

protected:

void Init(){

m_listUser.clear();

int n = 0;

for(n=0; n<10; n++){

User * user = new User();

user->id = n+1;

user->age = 18+n;

sprintf((char *)user->name.data(),"name_%d",n+1);

//插入

m_listUser.push_back(user);

}

}

std::list<User *> m_listUser;

};

int main(int argc, char **argv) {

? TWinsockSingleton::create();? ? ? // 需要用戶自己添加, 進(jìn)行WSAStartup的初始化, 算是windows 版的thrift的一個(gè)疏忽

? int port = 9090;

? shared_ptr<TestHandler> handler(new TestHandler());

? shared_ptr<TProcessor> processor(new TestProcessor(handler));

? shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));

? shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());

? shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

? TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);

? server.serve();

? return 0;

}

5. java 客戶端代碼調(diào)用服務(wù)端接口
5.1? 利用idea新建工程,本例只是測(cè)試,新建控制臺(tái)即可,然后添加 步驟3 產(chǎn)生的java文件
5.2 引入thrift相關(guān)jar包,注意,版本為0.9.0, libthrift-0.9.0.jar
5.3 客戶端代碼編寫

public static void main(String[] args) {
? ? launch(args);}

public void start() {

? ? try {
? ? ? ? String ip= "127.0.0.1"; //服務(wù)端的ip

? ? ? ? int port= 9090;//端口
? ? ? ? TTransport socket= new TSocket(ip, port);

? ? ? //TProtocol protocol = new TCompactProtocol(socket);

? ? ? ? TProtocol protocol= new TBinaryProtocol(socket);

? ? ? ? socket.open();

? ? ? ? Test.Client tc= new Test.Client(protocol);

? ? ? ? int ret= tc.add(2, 3);

? ? ? ? socket.close();

? ? } catch (TTransportException e) {

? ? ? ? e.printStackTrace();

? ? } catch (TException e) {

? ? ? ? e.printStackTrace();

}

}

6. 注意:服務(wù)端和客戶端相關(guān)協(xié)議一致,本例用的都是 TBinaryProtocol 協(xié)議。

結(jié)束語:如果需要相關(guān)代碼及編譯調(diào)試遇到問題,可以聯(lián)系, 2736483347@qq.com

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容