thrift框架使用C++

来源:互联网 发布:数据库联表的算法思想 编辑:程序博客网 时间:2024/04/30 01:54
1. 编写thrift接口文件student.thrift 
?
1
2
3
4
5
6
7
8
9
structStudent{
 1: i32 sno,
 2: string sname,
 3:boolssex,
 4: i16 sage,
}
service Serv{
 i32 put(1: Student s),
}

2. 用“thrift -r --gen cpp student.thrift”在gen-cpp文件夹中生成cpp及头文件,其中自动生成了Serv_server.skeleton.cpp文件,它是简单的server端代码,可以修改(一般都重新参照来写server端代码) 
可以用g++ -g -DHAVE_NETINET_IN_H -I. -I/usr/local/include/thrift -L/usr/local/lib Serv.cpp student_types.cpp student_constants.cpp Serv_server.skeleton.cpp -o server -lthrift 
生成server可执行程序; 

3. 可以修改Serv_server.skeleton.cpp文件,做成非阻塞server: 
    新建server.cpp文件(参照Serv_server.skeleton.cpp并参照nonblockingServer的写法): 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <concurrency/ThreadManager.h> //zml
#include <concurrency/PosixThreadFactory.h> //zml
#include "Serv.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <server/TNonblockingServer.h> //zml   
usingnamespace::apache::thrift;
usingnamespace::apache::thrift::protocol;
usingnamespace::apache::thrift::transport;
usingnamespace::apache::thrift::server;   
usingnamespace::apache::thrift::concurrency;//zml       
usingboost::shared_ptr;   
 
#define THREAD_NUM 2
constintg_port = 9090;
classServHandler : virtualpublicServIf {
 public:
  ServHandler() {
    // Your initialization goes here
  }
  int32_t put(constStudent& s) {
    // Your implementation goes here
    printf("put student.sno=%d\n", s.sno);
    returns.sno;
  }
};
intthrift_server_run()
{
    //创建thrift server
    shared_ptr<ServHandler> handler(newServHandler());
    shared_ptr<TProcessor> processor(newServProcessor(handler));
    shared_ptr<TProtocolFactory> protocolFactory(newTBinaryProtocolFactory());
     
    shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(THREAD_NUM);
    shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory> (newPosixThreadFactory());//PosixThreadFactory可以自定义(继承于ThreadFactory)
    threadManager->threadFactory(threadFactory);
    threadManager->start();     
    TNonblockingServer server(processor, protocolFactory, g_port, threadManager);
    try{
        server.serve();
    }
    catch(TException e) {
        printf("Server.serve() failed\n");
        exit(-1);
    }
    return0;
}
intmain(intargc,char**argv) {
    thrift_server_run();
    while(1) {
        sleep(10);
    }
    return0;
}
    
    生成server可执行程序: 
    g++ -g -DHAVE_NETINET_IN_H -I. -I/usr/local/include/thrift -L/usr/local/lib Serv.cpp student_types.cpp student_constants.cpp server.cpp -o server -lthriftnb -levent -lthrift -lrt 

4. 编写对接nonblockingServer的client端代码: 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Serv.h"  // 替换成你的.h 
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
usingnamespaceapache::thrift;
usingnamespaceapache::thrift::protocol;
usingnamespaceapache::thrift::transport;
usingboost::shared_ptr;
intmain()
{
    boost::shared_ptr<TSocket> socket(newTSocket("localhost", 9090));
     
    //对接nonblockingServer时必须的,对普通server端时用boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TTransport> transport(newTFramedTransport(socket));
     
    boost::shared_ptr<TProtocol> protocol(newTBinaryProtocol(transport));
    ServClient client(protocol);
     
    //设置发送、接收、连接超时
    socket->setConnTimeout(2000);
    socket->setRecvTimeout(2000);
    socket->setSendTimeout(2000);
     
    transport->open();
     
    //insert your code here 
    Student stu;
    stu.sno = 1;
    stu.sname = "zml";
    stu.ssex = 0;
    stu.sage = 25;
    intret = client.put(stu);
    printf("client put ret=%d\n", ret);
     
    transport->close();
    return0;
}
    
    编译生成client可执行程序: 
    g++ -g -DHAVE_NETINET_IN_H -I/usr/local/include/thrift -L/usr/local/lib/ Serv.cpp student_types.cpp student_constants.cpp client.cpp -o client -lpthread -lthrift -lrt
0 0