安装好thrift后,尝试一个小例子
(1)书写.thrift文件
创建一个my.thrift文件,内容如下:
namespace java demo
namespace cpp demo
struct helloIn
{
1: i32 age;
2: string name;
}
struct helloOut
{
1: i32 resCode;
3: string msg;
}
service Hello{
helloOut helloString(1:helloIn sIn)
}
(2)生成cpp文件
因为只用c++开发,所以执行
thrift -r --gen cpp my.thrift
–gen 后指定生成的语言,生成的cpp存储在目录gen-cpp下
命令执行后,将会在./gen-cpp/目录下生成如下文件:
Hello.cpp
Hello.h
Hello_server.skeleton.cpp
my_constants.cpp
my_constants.h
my_types.cpp
my_types.h
注意文件的大小写:
Hello开头的文件是由service生成的,这个关键字很重要,下面还会见到以它开头的类。
my是根据my.thrift文件名生成的。
这些文件可以进行编译,生成最初的服务端。
(3)编写客户端
目录结构参考:
客户端代码:
#include "gen-cpp/Hello.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace 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));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
transport->open();
//调用server服务
demo::helloIn sIn;
demo::helloOut sOut;
sIn.age = 25;
sIn.name = "ccy";
demo::HelloClient client(protocol);
printf("age:%d name:%s\n", sIn.age, sIn.name.c_str());
client.helloString(sOut, sIn);
printf("res:%d, msg:%s\n", sOut.resCode, sOut.msg.c_str());
transport->close();
return 0;
}
服务端代码:
直接用gen-cpp/Hello_server.skeleton.cpp 文件就行,修改一下函数调用的逻辑:
#include "Hello.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;
using namespace ::demo;
class HelloHandler : virtual public HelloIf {
public:
HelloHandler() {
// Your initialization goes here
}
void helloString(helloOut& _return, const helloIn& sIn) {
// Your implementation goes here
printf("server get name:%s, age:%d\n", sIn.name.c_str(), sIn.age);
_return.msg = "Hello";
_return.resCode = 0;
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<HelloHandler> handler(new HelloHandler());
shared_ptr<TProcessor> processor(new HelloProcessor(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;
}
(4) 编译链接:
创建Makefile文件:
CC=g++ -ggdb3 -Wall
CFLAGS = -I. -I/usr/local/include/thrift
LFLAGS = -L/usr/local/lib
LDEXEFLAGS = -lthrift
OBJS = gen-cpp/Hello.o \
gen-cpp/my_types.o \
gen-cpp/my_constants.o
all:client server
gen-cpp/Hello.o: gen-cpp/Hello.cpp
$(CC) $(CFLAGS) -c $^ -o $@
gen-cpp/my_types.o: gen-cpp/my_types.cpp
$(CC) $(CFLAGS) -c $^ -o $@
gen-cpp/my_constants.o: gen-cpp/my_constants.cpp
$(CC) $(CFLAGS) -c $^ -o $@
Hello_server.skeleton.o: gen-cpp/Hello_server.skeleton.cpp
$(CC) $(CFLAGS) -c $^ -o $@
Client.o: myclient.cpp
$(CC) $(CFLAGS) -c $^ -o $@
server: $(OBJS) Hello_server.skeleton.o
$(CC) $(CFLAGS) $(OBJS) Hello_server.skeleton.o $(LFLAGS) $(LDEXEFLAGS) -o $@
client: $(OBJS) Client.o
$(CC) $(CFLAGS) $(OBJS) Client.o $(LFLAGS) $(LDEXEFLAGS) -o $@
clean:
rm -f ./*.o client server gen-cpp/*.o
执行make,会在当前目录下生成server和client两个可执行文件
(如果编译出错,要么是库文件没有引用对,要么是thrift没有安装好。
例如:undefined reference to `apache::thrift::server::TSimpleServer::TSimpleServer)
(5)运行结果:
客户端执行结果:
服务端执行结果: