2. 协议制定
本例子协议如下:
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. 代码自动生成---------生成cpp, java源码
利用工具thrift-0.9.0.exe,生成c++代码,命令为:thrift -r --gen cpp user.thrift
生成java代码,命令为:thrift -r --gen java person.thrift
4. c++ 服务端工程搭建
4.1 将步骤三产生的cpp代码拷贝到服务端程序目录下:
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 服务端代码如下:
#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(); // 需要用户自己添加, 进行WSAStartup的初始化, 算是windows 版的thrift的一个疏忽
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 客户端代码调用服务端接口
5.1 利用idea新建工程,本例只是测试,新建控制台即可,然后添加 步骤3 产生的java文件
5.2 引入thrift相关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"; //服务端的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. 注意:服务端和客户端相关协议一致,本例用的都是 TBinaryProtocol 协议。
结束语:如果需要相关代码及编译调试遇到问题,可以联系, 2736483347@qq.com