

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