Thrift之c++实例
一、c++实例
1.下载与安装thrift工具
http://thrift.apache.org/download/
1)、新建vc工程。
2)、将上面的文件拷贝到工程目录下,Test_server.skeleton.cpp,就是C++服务端的main函数入口文件。
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "Test.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;
class TestHandler : virtual public TestIf {
public:
TestHandler() {
// Your initialization goes here
Init();
}
int32_t add(const int32_t a, const int32_t b) {
// Your implementation goes here
printf("add\n");
return a+b;
}
void getById(User& _return, const int64_t id) {
// Your implementation goes here
User *user;
std::list<User *>::iterator iter = m_listUser.begin(),iterEnd = m_listUser.end();
for(iter; iter!=iterEnd; iter++)
{
user = *iter;
if(user->id == id)
{
_return.id = user->id;
_return.__set_name(user->name.c_str());
_return.age = user->age;
_return.__set_vip(user->vip);
break;
}
}
printf("getById\n");
}
protected:
void Init(){
m_listUser.clear();
int n = 0;
for(n=0; n<10; n++){
User * user = new User();
user->id = n+1;
user->age = 18+n;
sprintf((char *)user->name.data(),"name_%d",n+1);
//插入
m_listUser.push_back(user);
}
}
std::list<User *> m_listUser;
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<TestHandler> handler(new TestHandler());
shared_ptr<TProcessor> processor(new TestProcessor(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;
}
//需要引入thrift对于cpp的lib头文件所在目录,openssl头文件所在目录,boost头文件所在目录。
//需要引用lib文件,LibThrift.lib libeay32MT.libssleay32MT.lib
5、客户端代码
1)、新建vc工程。
2)、将上面的文件(除Test_server.skeleton.cpp外)拷贝到工程目录下。
3)、编写客户端代码。
// Client.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include "Test.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using boost::shared_ptr;
using namespace std;
int main(int argc, char* argv[])
{
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<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
TestClient client(protocol);
try
{
//设置发送、接收、连接超时
socket->setConnTimeout(5000);
socket->setRecvTimeout(5000);
socket->setSendTimeout(5000);
transport->open();
//insert your code here
int nResult = client.add(100,200);
printf("add(%d,%d) = %d\n",100,200,nResult);
User findUser;
findUser.id = -100;
client.getById(findUser,1);
printf("%s\n",findUser.name.c_str());
transport->close();
printf("send success!\n");
}
catch (...)
{
}
return 0;
}
//需要引入thrift对于cpp的lib头文件所在目录,openssl头文件所在目录。
//需要引用lib文件,LibThrift.lib libeay32MT.libssleay32MT.lib
二、VS2008上编译thrift的库文件
1、下载thrift-0.9.3.tar.gz源码,源码针对cpp的编译自带的是vs2010版本。
2、需要第三方库boost、libevent、openssl支持。
libevent-2.0.21-stable.tar.gzhttps://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
boost 1.54.0
http://www.boost.org/users/history/version_1_54_0.html
编译好的boost 1.54.0下载地址:https://sourceforge.net/projects/boost/files/boost-binaries/1.54.0
OpenSSL 1.0.0g
http://blog.csdn.net/akof1314/article/details/7241829
3、新建vc工程,编译。
用Project From Existing Code...创建工程。
将 cpp/src/thrift加入工程,修改项目配置类型修改成静态库。
在Solution Explorer中,
去除 qt目录。
添加 boost包含目录、openssl所包含目录。
添加 src为包含目录,不然 #include <thrift/Thrift.h>出错。
VC2008缺少 stdint.h,创建到thrift/windows目录下,并添加为include目录。
#pragma once
#include <boost/cstdint.hpp>
typedef boost::int8_t int8_t;
typedef boost::uint8_t uint8_t;
typedef boost::int16_t int16_t;
typedef boost::uint16_t uint16_t;
typedef boost::int32_t int32_t;
typedef boost::uint32_t uint32_t;
typedef boost::int64_t int64_t;
typedef boost::uint64_t uint64_t;
#define INT8_MIN ((int8_t)_I8_MIN)
#define INT8_MAX _I8_MAX
#define INT16_MIN ((int16_t)_I16_MIN)
#define INT16_MAX _I16_MAX
#define INT32_MIN ((int32_t)_I32_MIN)
#define INT32_MAX _I32_MAX
#define INT64_MIN ((int64_t)_I64_MIN)
#define INT64_MAX _I64_MAX
#define UINT8_MAX _UI8_MAX
#define UINT16_MAX _UI16_MAX
#define UINT32_MAX _UI32_MAX
#define UINT64_MAX _UI64_MAX
去除VC2010 thrift.sln中没有的文件,无法编译:
Mutex.cpp, PosixThreadFactory.cpp, Monitor.cpp
TEvhttpClientChannel.cpp TEvhttpServer.cpp
TServer.cpp TNonblockingServer.cpp
TSSLSocket.cpp TSSLServerSocket.cpp
TZlibTransport.cpp
加上宏 HAVE_CONFIG_H,或者 force include force_inc.h.
windows/tr1/functional中
#include <functional>
改为
#include <boost/tr1/functional.hpp>
Thrift之c++实例的更多相关文章
- Thrift-java实例
➠更多技术干货请戳:听云博客 Thrift实例1 功能描述:客户端与服务器端分别是两个应用,先启动服务器端,再启动客户端,实现执行客户端运行服务器端的加法方法. 源码截图(源码在附件中): 客户端: ...
- Thrift之java实例
一.java实例 1.下载与安装thrift工具 http://thrift.apache.org/download/ .服务器代码 服务Test实现类 package com.zychen.thri ...
- Thrift全面介绍
官网:http://thrift.apache.org 简介 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java ...
- thrift常见异常及原因分析(updating)
[org.apache.thrift.TException家族] [Thrift架构] 以下是thrift的客户端和服务端交互的一个原理图.可以看到遵循了rpc框架的传输层.协议层和应用层三层.本文提 ...
- Thrift初试
Restful 基于 Http 进行通讯. 开放.标准.简单.兼容性升级容易: 性能略低.在 QPS 高或者对响应时间要求苛刻的服务上,可以用 RPC,RPC采用二进制传输.TCP 通讯,所以通常性能 ...
- python thrift 实现 单端口多服务的过程
Thrift 是一种接口描述语言和二进制通信协议.以前也没接触过,最近有个项目需要建立自动化测试,这个项目之间的微服务都是通过 Thrift 进行通信的,然后写自动化脚本之前研究了一下. 需要定义一个 ...
- 北风风hadoop课程体系
课程一.基于Linux操作系统平台下的Java语言开发(20课时)课程简介本套课程主要介绍了Linux系统下的Java环境搭建及最基础的Java语法知识.学习Linux操作系统下Java语言开发的好处 ...
- Python代码样例列表
扫描左上角二维码,关注公众账号 数字货币量化投资,回复“1279”,获取以下600个Python经典例子源码 ├─algorithm│ Python用户推荐系统曼哈顿算法实现.py│ ...
- Hive 官方手册翻译 -- Hive Transactions (Hive 事务)
由 Alan Gates创建, 最终由 Andrew Sherman修改于2018年8月7日 原文链接:https://cwiki.apache.org/confluence/display/Hive ...
随机推荐
- MySQL 单表查询(Day42)
阅读目录 一,查询语法 二,简单查询 三,where约束 四,having过滤 五,分组查询 group by 六,关键字的执行优先级 七,查询排列 order by 八,使用聚合函数查询 九,whe ...
- Keras实践:实现非线性回归
Keras实践:实现非线性回归 代码 import os os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" import ke ...
- GIT学习笔记(2):时光机穿梭与远程仓库
GIT学习笔记(2):时光机穿梭与远程仓库 撤销操作 1.GIT如何跟踪修改 在我们修改了代码内容后,执行了git add和git commit命令来将其交由Git进行版本控制.我们前面举的例子是这样 ...
- NGUI如何使用汉字
1:准备好字体文件,就是ttf后缀名的那些.. 2:在第一个红线部分,将下拉框选择为Unity,在后面的字体里面选择第一步准备好的字体. 3:创建UILabel,widget里面的Color才是字体的 ...
- js踩过的一些坑
参考我的博客:http://www.isedwardtang.com/2017/08/29/js-bug/
- 菩提树下的杨过.Net 的《hadoop 2.6全分布安装》补充版
对菩提树下的杨过.Net的这篇博客<hadoop 2.6全分布安装>,我真是佩服的五体投地,我第一次见过教程能写的这么言简意赅,但是又能比较准确表述每一步做法的,这篇博客主要就是在他的基础 ...
- 解决Webpack 安装sass时出现的错误
webpack环境下,加载css需要 css-loader 和 style-loader. css-loader:使用类似@import和url(...)的方法实现 require的功能: style ...
- Python3:数字类型和字符串类型的相互转换
Python3:数字类型和字符串类型的相互转换 一.python中字符串转换成数字 方法1: 类中进行导入:import string str='555'num=string.atoi(str)num ...
- jQuery二级下拉菜单
在线演示 本地下载
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...