thrift一个例子 (编译c++ 参数)

35 篇文章 1 订阅
9 篇文章 0 订阅

http://wiki.apache.org/thrift/ThriftUsageC%2B%2B

http://blog.csdn.net/hbuxiaoshe/article/details/6558391

我用的是c++,所以我举一个c++的例子,简单说一下thrift的使用入门。

例子描述是这样的:我们将学生信息(学号,姓名,性别,年龄)由客户端发送到服务端。

实现这个例子,我们大致要做以下几部分事情:

(1)书写.thrift文件

(2)生成cpp文件

(3)编写客户端

(4)编译cpp文件并执行

 

(1)书写.thrift文件

学生信息是有结构的,所以我们使用thrift的struct即可,为了达到通信的目的,我们必须使用service。

所以最后书写成的student.thrift文件内容如下:

struct Student{
 1: i32 sno,
 2: string sname,
 3: bool ssex,
 4: i16 sage,
}
service Serv{
 void put(1: Student s),
}

(2)生成cpp文件

生成cpp文件很简单,只需要一个thrift命令即可:

/home/xiaoshe/opt/bin/thrift -r --gen cpp student.thrift

--gen 后指定生成的语言,生成的cpp存储在目录gen-cpp下

命令执行后,将会在./gen-cpp/目录下生成如下文件:

Serv.cpp

Serv.h

Serv_server.skeleton.cpp

student_constants.cpp

student_constants.h

student_types.cpp

student_types.h

注意文件的大小写:

Serv开头的文件是由service生成的,这个关键字很重要,下面还会见到以它开头的类。

student是根据student.thrift文件的名生成的。

这些文件可以进行编译,生成最初的服务端。

 

(3)编写客户端

使用thrift命令后,我们并没有得到我们想要的客户端client源代码,因此客户端程序要由我们自己编写实现。然而很幸运,我们可以使用下面的代码段来编写我们client程序:

[c-sharp] view plain copy print ?
  1. #include "Serv.h"  // 替换成你的.h  
  2. #include <transport/TSocket.h>  
  3. #include <transport/TBufferTransports.h>  
  4. #include <protocol/TBinaryProtocol.h>   
  5.   
  6. using namespace apache::thrift;  
  7. using namespace apache::thrift::protocol;  
  8. using namespace apache::thrift::transport;  
  9.   
  10. using boost::shared_ptr;  
  11.   
  12. int main(int argc, char **argv) {  
  13.     boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));  
  14.     boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));  
  15.     boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));  
  16.   
  17.     transport->open();  
  18.   
  19.     // 我们的代码写在这里   
  20.   
  21.     transport->close();  
  22.   
  23.     return 0;  
  24. }  

保存成文件client.cpp

(4)编译cpp文件并执行

编译服务端:g++ -g -I/home/xiaoshe/opt/include/thrift -L/home/xiaoshe/opt/lib/ -lthrift Serv.cpp student_types.cpp student_constants.cpp Serv_server.skeleton.cpp -o server

编译客户端:g++ -g -I/home/xiaoshe/opt/include/thrift -L/home/xiaoshe/opt/lib/ -lthrift -lm -pthread -lz -lrt -lssl Serv.cpp student_types.cpp student_constants.cpp client.cpp -o client

运行服务端:./server

运行客户端:./client

 

(5)传输我们的数据Student信息

到此客户端已经连上了服务端,但服务端只有这样的响应(No more data to read),因为二者之间还没有数据交互。

我们把客户端当做发送端,修改client.cpp向服务端发送数据。

在“// 我们的代码写在这里”

写下我们的代码:

// 先创建一个Student类型的变量,Student是我们在student.thrift中定义过的

 Student s;
 s.sno = 123;
 s.sname = "xiaoshe";
 s.ssex = 1;
 s.sage = 30;

// 再定义一个对象client,又是以"Serv"开头的类

 ServClient client(protocol);

// 最后调用put函数向服务端传输数据, put是student.thrift采用service定义的成员函数。

// 调用put后,服务端也调用相应的put()
 client.put(s);

 

 

服务端负责接收数据,也做相应修改:

在类ServHandler()的put()中:

printf("sno=%d sname=%s ssex=%d sage=%d/n", s.sno, s.sname.c_str(), s.ssex, s.sage);

 

最后编译,运行服务端,启动客户端后,服务端收到消息,显示结果为:

put
sno=123 sname=xiaoshe ssex=1 sage=30

至此,客户端已能向服务端发送数据了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Thrift 示例,包含服务器和客户端,使用 C++ 编写。 1. 首先,我们需要定义一个 Thrift IDL 文件。在这个例子中,我们定义了一个简单的服务,可以将两个整数相加并返回结果。在一个名为 "math.thrift" 的文件中,我们写下以下代码: ``` namespace cpp tutorial service Calculator { i32 add(1:i32 num1, 2:i32 num2) } ``` 2. 接下来,我们需要使用 Thrift 编译器生成 C++ 代码。在终端中运行以下命令: ``` thrift --gen cpp math.thrift ``` 这将生成一个 "gen-cpp" 文件夹,其中包含 Thrift 自动生成的 C++ 代码。 3. 现在,我们可以着手编写服务器端代码。在一个名为 "server.cpp" 的文件中,我们编写以下代码: ```cpp #include <iostream> #include <thrift/protocol/TBinaryProtocol.h> #include <thrift/server/TSimpleServer.h> #include <thrift/transport/TServerSocket.h> #include <thrift/transport/TBufferTransports.h> #include "gen-cpp/Calculator.h" using namespace std; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using namespace apache::thrift::server; using namespace cpp_tutorial; class CalculatorHandler : public CalculatorIf { public: CalculatorHandler() {} int32_t add(const int32_t num1, const int32_t num2) { cout << "Adding " << num1 << " and " << num2 << endl; return num1 + num2; } }; int main(int argc, char** argv) { int port = 9090; shared_ptr<CalculatorHandler> handler(new CalculatorHandler()); shared_ptr<TProcessor> processor(new CalculatorProcessor(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); cout << "Starting the server..." << endl; server.serve(); cout << "Done." << endl; return 0; } ``` 这个代码片段创建了一个 `CalculatorHandler` 类,它实现了 "add" 方法,将两个整数相加并返回结果。然后,我们创建一个 `CalculatorProcessor` 对象来处理客户端请求,并使用 `TSimpleServer` 类来启动服务器。 4. 最后,我们需要编写客户端代码。在一个名为 "client.cpp" 的文件中,我们编写以下代码: ```cpp #include <iostream> #include <thrift/transport/TBufferTransports.h> #include <thrift/protocol/TBinaryProtocol.h> #include <thrift/transport/TSocket.h> #include "gen-cpp/Calculator.h" using namespace std; using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; using namespace cpp_tutorial; int main(int argc, char** argv) { shared_ptr<TTransport> socket(new TSocket("localhost", 9090)); shared_ptr<TTransport> transport(new TBufferedTransport(socket)); shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); CalculatorClient client(protocol); try { transport->open(); int32_t num1 = 3; int32_t num2 = 5; int32_t sum = client.add(num1, num2); cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl; transport->close(); } catch (TException& e) { cerr << "Error: " << e.what() << endl; } return 0; } ``` 这个代码片段创建了一个 `TSocket` 对象,用于连接到服务器的地址和端口。然后,我们使用 `TBufferedTransport` 和 `TBinaryProtocol` 对象创建 `CalculatorClient` 对象,并调用 `add` 方法。最后,我们关闭传输并打印出结果。 5. 最后,我们可以编译并运行服务器和客户端。在终端中分别运行以下命令: ``` g++ -std=c++11 -o server server.cpp gen-cpp/*.cpp -lthrift ``` ``` g++ -std=c++11 -o client client.cpp gen-cpp/*.cpp -lthrift ``` 然后,首先运行服务器: ``` ./server ``` 接下来,运行客户端: ``` ./client ``` 输出应该会类似于以下内容: ``` Starting the server... Adding 3 and 5 Done. Sum of 3 and 5 is: 8 ``` 这表明服务器已经启动,并且客户端能够成功调用服务器的方法并接收返回值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值