thrift框架使用C++
来源:互联网 发布:数据库联表的算法思想 编辑:程序博客网 时间:2024/04/30 01:54
1. 编写thrift接口文件student.thrift
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的写法):
生成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端代码:
编译生成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
1
2
3
4
5
6
7
8
9
struct
Student{
1: i32 sno,
2: string sname,
3:
bool
ssex,
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
using
namespace
::apache::thrift;
using
namespace
::apache::thrift::protocol;
using
namespace
::apache::thrift::transport;
using
namespace
::apache::thrift::server;
using
namespace
::apache::thrift::concurrency;
//zml
using
boost::shared_ptr;
#define THREAD_NUM 2
const
int
g_port = 9090;
class
ServHandler :
virtual
public
ServIf {
public
:
ServHandler() {
// Your initialization goes here
}
int32_t put(
const
Student& s) {
// Your implementation goes here
printf
(
"put student.sno=%d\n"
, s.sno);
return
s.sno;
}
};
int
thrift_server_run()
{
//创建thrift server
shared_ptr<ServHandler> handler(
new
ServHandler());
shared_ptr<TProcessor> processor(
new
ServProcessor(handler));
shared_ptr<TProtocolFactory> protocolFactory(
new
TBinaryProtocolFactory());
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(THREAD_NUM);
shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory> (
new
PosixThreadFactory());
//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);
}
return
0;
}
int
main(
int
argc,
char
**argv) {
thrift_server_run();
while
(1) {
sleep(10);
}
return
0;
}
生成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>
using
namespace
apache::thrift;
using
namespace
apache::thrift::protocol;
using
namespace
apache::thrift::transport;
using
boost::shared_ptr;
int
main()
{
boost::shared_ptr<TSocket> socket(
new
TSocket(
"localhost"
, 9090));
//对接nonblockingServer时必须的,对普通server端时用boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TTransport> transport(
new
TFramedTransport(socket));
boost::shared_ptr<TProtocol> protocol(
new
TBinaryProtocol(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;
int
ret = client.put(stu);
printf
(
"client put ret=%d\n"
, ret);
transport->close();
return
0;
}
编译生成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
- Thrift框架使用教程
- thrift框架使用C++
- thrift框架使用C++
- RPC框架-Thrift的使用
- Thrift框架学习笔记 IOS/MAC中使用Thrift框架
- 【Thrift】Thrift框架原理
- IOS/MAC中使用Thrift框架
- 在codeigniter框架里使用thrift
- Thrift框架
- Thrift框架
- 使用C++(通过Thrift) 操作Hbase
- thrift----跨语言框架的魅力(c++,python)
- thrift使用
- Thrift使用
- 跨语言通信框架Apache Thrift在PHP中的使用
- 使用Thrift的网络框架搭建一般性网络应用
- Thrift 框架分析1
- Thrift 框架分析1
- 第七周 课后实践:初次使用VC++ 6.0,纪念一下
- linux select函数用法
- 练习6: 用迭代法求斐波纳契数列第n项的函数, 提高函数设计思维能力
- 逆向分析之花指令1
- 为什么要有Set接口? set接口中的方法和Collection中的一样,求大神给我一个set存在的理由~
- thrift框架使用C++
- 云服务器之崛起
- KMP||扩展KMP(Codeforces 535D - Tavas and Malekas )
- KNN模型
- android:descendantFocusability用法简析
- iOSURL编码 解码
- 第三章 第8题
- SPOJ TTM To The Moon 主席树的区间更新与查询
- 朴素贝叶斯模型