step 1 编写thrift文件 yang.thrift
struct Student{ 1: i32 sno, 2: string sname, 3: bool ssex, 4: i16 sage, } service Serv{ void put(1: Student s), i32 icall(1: Student s), string scall(1: Student s), /* string& srcall(1: Student s), ----------------------------- -thrift -r --gen cpp student.thrift -error: - [ERROR:/root/test/thrift/student.thrift:12] (last token was '&') - syntax error - [FAILURE:/root/test/thrift/student.thrift:12] Parser error during include pass. ----------------------------- */ Student stcall(1: Student s), }
step 2 生成源代码
比如要生成c++代码,就可以 thrift -r --gen cpp yang.thrift。这样就能在目录下生成一个gen-cpp目录,里面生成了多个文件,其中的Serv_server.skeleton.cpp就是服务器端main函数入口文件。–gen后指定生成的语言,yang.thrift就是编写的thrift魔板。
step 3 编写客户端代码
step 2 生成了服务器端的代码框架(也仅仅是个框架),但是客户端的代码就要自己编写了。
#include "Serv.h" //这里包含的是服务器端生成的头文件,注意路径 #include #include #include 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 socket(new TSocket("localhost", 9090)); boost::shared_ptr transport(new TBufferedTransport(socket)); boost::shared_ptr protocol(new TBinaryProtocol(transport)); transport->open(); //调用server服务 Student s; s.sno = 123; s.sname = "hao973"; s.ssex = 1; s.sage = 30; ServClient client(protocol); printf("sno=%d sname=%s ssex=%d sage=%dn", s.sno, s.sname.c_str(), s.ssex, s.sage); //put client.put(s); //icall scall std::string strname = ""; client.scall(strname, s); printf("icall=%d, scall=%sn", client.icall(s), strname.c_str()); //stcall client.stcall(stu, s); printf("student sno=%d sname=%s ssex=%d sage=%dn", stu.sno, stu.sname.c_str(), stu.ssex, stu.sage); transport->close(); return 0; }
step 4 修改填充服务器端的代码
这里主要是填充服务的方法实现。在Serv_server.skeleton.cpp文件put中添加printf(“sno=%d sname=%s ssex=%d sage=%dn”, s.sno, s.sname.c_str(), s.ssex, s.sage);。其他几个函数也是如此添加。下面是最后的结果。
#include "Serv.h" #include #include #include #include using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using boost::shared_ptr; class ServHandler : virtual public ServIf { public: ServHandler() { // Your initialization goes here } void put(const Student& s) { // Your implementation goes here printf("putn"); printf("sno=%d sname=%s ssex=%d sage=%dn", s.sno, s.sname.c_str(), s.ssex, s.sage); } int32_t icall(const Student& s) { // Your implementation goes here printf("icalln"); printf("sno=%d sname=%s ssex=%d sage=%dn", s.sno, s.sname.c_str(), s.ssex, s.sage); return s.sage; } void scall(std::string& _return, const Student& s) { // Your implementation goes here printf("scalln"); printf("sno=%d sname=%s ssex=%d sage=%dn", s.sno, s.sname.c_str(), s.ssex, s.sage); _return = s.sname; } void stcall(Student& stu, const Student& s) { // Your implementation goes here printf("stcalln"); printf("sno=%d sname=%s ssex=%d sage=%dn", s.sno, s.sname.c_str(), s.ssex, s.sage); stu.sno = s.sno + 1; stu.sname = s.sname + "123"; stu.ssex = s.ssex; stu.sage = s.sage + 10; } }; int main(int argc, char **argv) { int port = 9090; shared_ptr handler(new ServHandler()); shared_ptr processor(new ServProcessor(handler)); shared_ptr serverTransport(new TServerSocket(port)); shared_ptr transportFactory(new TBufferedTransportFactory()); shared_ptr protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; }
step 5 编译链接
client 端:
g++ -DHAVE_NETINET_IN_H -g -Wall -I/usr/local/include/thrift -I/usr/include/boost -I./gen-cpp -I/usr/include Serv.cpp yang_constants.cpp yang_types.cpp client.cpp -L/usr/local/lib/ -lthrift -o client
这里注意需要 -DHAVE_NETINET_IN_H,否则会报一堆找不到定义。
然后是 -I./gen-cpp需要跟client.cpp中的头文件相匹配。因为需要服务器端生成的.h文件
需要指定链接-lthrift库,指定的是载/usr/local/lib中,在运行时中可能会报找不到,需要在/etc/ld.so.conf.d/中弄一个文件,然后ldconfig,更新链接库。
还有一个很坑的是:client.cpp需要服务器端生成的cpp文件,其中有类中方法的实现,所以编译时需要指明。
server端:
g++ -DHAVE_NETINET_IN_H -g -Wall -I/usr/local/include/thrift -I/usr/include/boost -I./ -I/usr/include Serv.cpp Serv_server.skeleton.cpp yang_constants.cpp yang_types.cpp -L/usr/local/lib/*.so -lthrift -o server