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 ...
随机推荐
- Hadoop2.0NameNode HA和Federation实践
一.背景 天云趋势在2012年下半年开始为某大型国有银行的历史交易数据备份及查询提供基于Hadoop的技术解决方案,由于行业的特殊性,客户对服务的可 用性有着非常高的要求,而HDFS长久以来都被单点故 ...
- python 2 和python 3的 区别
用户交互 input ps:python2:raw_input python3:input 在 python2里 print不需要加括号也可以打印 子python3里 print 必须加括号才能打印
- Loadrunder场景设计篇——IP欺骗
适用协议 LoadRunner的多ip功能允许运行在单一负载生成器上的Vuser可以通过多ip被识别.服务器和路由识别这些vuser为来自不同负载生成器上. 2 在负载生成器(load gene ...
- LLServer--》对LevelDB的应用
http://code.google.com/p/llserver/ 查看libs path的路径 LD_DEBUG=libs /usr/bin/llserver -h
- MMU解读
转:https://blog.csdn.net/yueqian_scut/article/details/24816757 mmu页表也是放在内存中,mmu里有一个寄存器存放页表首地址,从而找到页表( ...
- rest_framework解析器组件源码流程
rest_framework解析器组件源码流程 解析器顾名思义就是对请求体进行解析.为什么要有解析器?原因很简单,当后台和前端进行交互的时候数据类型不一定都是表单数据或者json,当然也有其他类型的数 ...
- Java经纬读坐标的距离计算
问题引出: 今天遇到经纬度坐标转换距离的工作,根据网站登录者的IP确定登录者目前的位置信息,将其经纬度信息与所有的营业厅的经纬度进行对比,网页上显示出距离登录者最近的营业厅地址,本打算就做一个二维坐标 ...
- zero-base coordinate 和one-base coordinate
zero-base和one-base是生信会经常碰到的两套坐标系统. zero-base是半开放式的,是不包括该点的,zero-base的文件有:bed;bam one-base是封闭式的,是包括该点 ...
- 2018-2019-2 20165114《网络对抗技术》Exp4 恶意代码分析
Exp4 恶意代码分析 目录 一.实验目标 (1)监控你自己系统的运行状态,看有没有可疑的程序在运行. (2)分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生指令或sys ...
- jsp基础知识点——思维导图
如图 思维导图图片链接 http://www.edrawsoft.cn/viewer/public/s/0b8cd083478732 有道云笔记图片链接 http://note.youdao.com/ ...