Apache Thrift 在Windows下的安装与开发
Windows下安装Thrift框架的教程很多。本文的不同之处在于,不借助Cygwin或者MinGW,只用VS2010,和Thrift官网下载的源文件,安装Thrift并使用。
先从官网 下载这两个文件:
· Thrift compiler for Windows (thrift-0.9.1.exe)
第一个文件是源代码包,第二个可执行文件用于在Windows下生成目标语言的桩代码。
除此以外,还需要boost库和libevent库。
安装Thrift
0)准备工作
thrift-0.9.1.tar.gz源码包
安装VS2010
安装boost库,我使用的boost1.51版本
安装libevent库,这里用的libevent-2.0.21-stable
1)解压缩thrift-0.9.1.tar.gz
进入\thrift-0.9.1\lib\cpp,VS2010打开Thrift.sln,有libthrift,libthriftnb两个工程。
两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库。
2)libthrift工程配置:
libthrift>属性->C/C++->常规->附加包含目录->\boost\boost_1_51
libthrift>属性->库管理器->常规->附加库目录->\boost\boost_1_51\lib
3)libthriftnb工程配置:
libthriftnb>属性->C/C++->常规->附加包含目录->
\boost\boost_1_51
\libevent-2.0.21-stable
\libevent-2.0.21-stable\include
\libevent-2.0.21-stable\WIN32-Code
libthriftnb>属性->库管理器->常规->附加库目录->
\boost\boost_1_51\lib
4)编译libthrift和libthriftnb工程
编译完成后,在\thrift-0.9.1\lib\cpp\Debug下生成libthrift.lib文件,和libthriftnb.lib文件。
选择release模式,则在\thrift-0.9.1\lib\cpp\Release下生成libthrift.lib文件和libthriftnb.lib文件。
至此,安装完成。
开发步骤
安装好thrift后,就可以开始开发了。开发过程分这么几步:
第1步: 写.thrift文件,也就是接口描述文件(Interface Description File);
第2步: 用Thrift compiler for Windows (thrift-0.9.1.exe) ,生成目标语言代码;
第3步: 服务器端程序引入thrift生成的代码,实现RPC业务代码。
第4步: 客户端引入代码,调用远程服务。
图中蓝色Thrift.exe就是从官网下载的第二个文件——“IDL翻译工具”,帮助你把.thrift文件“翻译”成目标语言的RPC代码。
例子
这个例子,远程Server提供一个函数。客户端调用这个函数。远程函数的功能很简单,就是输出“Hello Thrift”。
1)写.thrift文件
新建文本文件hello.txt,保存下面的内容后修改扩展名hello.thrift
service hello {
void func1( )
}
2)生成目标语言代码
把官网下载到的第二个文件thrift-0.9.1.exe和hello.thrift放到一个目录(hello)下。
打开cmd命令行窗口,进入到这个目录,执行命令:
C:\Users\admin\Desktop\Hello>thrift-0.9.1.exe --gen cpp hello.thrift
执行成功,在hello目录下,生成一个gen-cpp文件夹。
3)创建工程
Visual Studio 2010新建win32控制台应用程序。
项目名称 server
解决方案名称 hello
注意:附加选项中选择 勾选 空项目。
类似的,在hello解决方案下,再新建一个空项目client。
4)为项目添加文件
向Server项目添加文件。
复制gen-cpp文件夹中文件到Server工程,添加到Server工程中。
向Client项目添加文件。
复制gen-cpp文件夹中文件到Client工程,删除hello_server.skeleton.cpp,并额外添加client.cpp文件。
最终解决方案的文件结构是这样的:
5)配置项目属性
Sever工程 Server>属性->C/C++->常规->附加包含目录->\boost\boost_1_51
Sever工程 Server>属性->C/C++->常规->附加包含目录->\thrift-0.9.1\lib\cpp\src
Sever工程 Server>属性->C/C++->常规->附加包含目录->\thrift-0.9.1\lib\cpp\src\thrift
Sever工程 Server>属性->连接器->附加库目录->\boost\boost_1_51\lib
Sever工程 Server>属性->连接器->附加库目录->\thrift-0.9.1\lib\cpp\Debug
附加库目录指向的是刚刚编译出的Debug目录
类似的,Client工程也做这样的配置。
Client工程 Client>属性->C/C++->常规->附加包含目录->\boost\boost_1_51
Client工程 Client>属性->C/C++->常规->附加包含目录->\thrift-0.9.1\lib\cpp\src
Client工程 Client>属性->C/C++->常规->附加包含目录->\thrift-0.9.1\lib\cpp\src\thrift
Client工程 Client>属性->连接器->附加库目录->\boost\boost_1_51\lib
Client工程 Client>属性->连接器->附加库目录->\thrift-0.9.1\lib\cpp\Debug
6)Client代码
client.cpp文件是空的,添加代码:
#include <transport/TSocket.h>
#include "hello.h"
#include <protocol/TBinaryProtocol.h>
#include <server/TSimpleServer.h>
#include <transport/TServerSocket.h>
#include <transport/TBufferTransports.h>
#include <string>
#pragma comment(lib, "libthrift.lib")
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server; using boost::shared_ptr; #pragma comment(lib,"libthrift.lib")//链接库文件 int main(int argc, char** argv) {
int port = ;
shared_ptr<TTransport> socket(new TSocket("127.0.0.1", ));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
helloClient client(protocol);
try{
transport->open(); client.func1(); transport->close();
}catch(TException& tx){
printf("ERROR:%s\n",tx.what());
}
getchar();
return ;
}
7)Server代码
hello_server.skeleton.cpp 文件已经有thrift生成的代码,稍作修改,最终如下:
// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.
#include "hello.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>
#pragma comment(lib, "libthrift.lib")
using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;
using boost::shared_ptr;
class helloHandler : virtual public helloIf {
public:
helloHandler() {
// Your initialization goes here
}
void func1() {
// Your implementation goes here
printf("Hello Thrift\n");
}
};
int main(int argc, char **argv) {
//-----------------------------//
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested =MAKEWORD( , );
err = WSAStartup( wVersionRequested, &wsaData );
//-------------------------------//
//对上面这段代码做个说明,这是依赖windows的一段代码
//到2014.9.2官网的稳定版0.9.1,仍需要这段代码才可以在windows下编译通过。
//但是如果用git clone最新版,这个错误已经修正
//最新版注释掉这段代码,一样可以在windows下编译通过。
//备注时间:2014.9.2
int port = ;
shared_ptr<helloHandler> handler(new helloHandler());
shared_ptr<TProcessor> processor(new helloProcessor(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 ;
}
8)调试运行
先启动Server工程,再启动Client工程。运行结果:
总结
到这里Thrift的安装和开发基本操作步骤就介绍完了。Thrift的官方文档不是很完善,本篇介绍的安装方法不在网上众多教程之列,主要区别是没有使用Cygwin或者MinGW。对于想使用Visual Studio作为开发环境的同学会很有帮助。
教程中需要的文件,都可以从网上获取,核心代码在文中已经展示,相信按图索骥一定可以成功的运行第一个Thrift的例子。
最后十分感谢陈晓苏(北京)同学,本篇教程整理自他/她名字的一个文件夹。我从Thrift交流QQ群193713524的共享中获得的。这里增加了一些图片说明和我的理解。最后的例子,做了简化,是为了直接、清晰的说明安装过程和开发的配置。
转自:http://blog.csdn.net/colouroo/article/details/38588297
Apache Thrift 在Windows下的安装与开发的更多相关文章
- mysql 在windows下的安装,开发基础与要点
1:安装(windows下) 官网下载.msi文件 运行安装时只需要安装server就行了 在环境变量中配置到bin目录:e.g:C:\programFile\...mysql\bin 完成后进入wi ...
- Apache+php在windows下的安装和配置
下载和配置php 下载php:http://windows.php.net/download/ php-5.4.16-Win32-VC9-x86.zip 下载apache: http://ht ...
- Windows下单机安装Spark开发环境
机器:windows 10 64位. 因Spark支持java.python等语言,所以尝试安装了两种语言环境下的spark开发环境. 1.Java下Spark开发环境搭建 1.1.jdk安装 安装o ...
- Thrift架构~windows下安装和Hello World及编码引起的错误
最近开始正式接触Thrift架构,很牛B的技术,它被apache收纳了,属于开源中的一员,呵呵. 概念: Thrift源于大名鼎鼎的facebook之手,在2007年facebook提交Apache基 ...
- windows下手动安装 Apache+php+mysql
PHP 为什么先说php,因为apache的配置要写入php的一些路径 http://php.net/downloads.php 选择windows donwload 选择Thread Safe的版 ...
- [转载]Apache在windows下的安装配置
Apache在windows下的安装配置 转载自:http://blog.sina.com.cn/s/blog_536f16b00100cfat.html 1 Apache的下载 Apache ...
- spark在windows下的安装
Windows下最简的开发环境搭建这里的spark开发环境, 不是为apache spark开源项目贡献代码, 而是指基于spark的大数据项目开发. Spark提供了2个交互式shell, 一个 ...
- memcache的windows下的安装和简单使用
原文:memcache的windows下的安装和简单使用 memcache是为了解决网站访问量大,数据库压力倍增的解决方案之一,由于其简单实用,很多站点现在都在使用memcache,但是memcach ...
- Windows下memcache安装使用
Windows下Memcache安装 随着时间的推移,网上现在能找到的在 Windows下安装 Memcache 的文档大多已经过时.雪峰这里再简要介绍一下当下最新版的安装和配置方法. Memcach ...
随机推荐
- $.ajax里一个中文全角逗号引发的惨案
昨天,在制作一个页面时,突然发生一件不可思议的事情--JS失效了! 确实让人匪夷所思,我记得饭前还是正常运作的. 于是慢慢的缩小范围,把下午刚加的语句删掉,删完了页面就正常了. 于是被删除的这部分代码 ...
- 生活娱乐 Wifi机器人的制作流程
思路简单,但是创意无限~~ 动手能力超强 牛人教你做Wifi机器人(图) 一.前言 Wifi机器人(Wifi Robot):其实是一辆能通过互联网,或500米以外的笔记本无线设施来远程控制的遥控汽车. ...
- SolidEdge 装配体中如何快速的搞定一个面上所有螺丝 如何在装配体上进行阵列
1 点击"规则排列" 选择要排列的螺丝 2 选择被规则排列的架子 3 选择所有的圆孔(鼠标滑到任意圆孔位置,左键单击即可选中所有圆孔) 4 选择参考的基准孔(已经上了螺 ...
- angular1.x 组件开发
搜索框组件开发: 1.注册组件 app.js angular.module("myApp",[]) .component("nameSearch",{ temp ...
- c程序设计语言第一章5
练习1.20请编写程序d e t a b
- DASH----Desktop and mobile Architecture for System Hardware----桌面和移动系统硬件架构(DASH)计划
http://baike.baidu.com/subview/813787/11301142.htm http://sites.amd.com/cn/business/it-solutions/man ...
- Win8下怎样安装Win7 or Win7下怎样安装win8?
预计非常多人可能会用U盘安装工具去去做双系统的安装(Win8下安装Win7, Win7下安装Win8).可是在安装过程中你 会发现一个问题:win7下安装win8,提示你mbr硬盘格式不能安装win8 ...
- C语言restrict关键字的使用----可以用来优化代码
C99中新增加了restrict修饰的指针:由restrict修饰的指针是最初唯一对指针所指向的对象进行存取的方法,仅当第二个指针基于第一个时,才能对对象进行存取.对对象的存取都限定于基于由restr ...
- C系列语言终极校对宝典【第一、第二部分】
第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...
- vue 单页面(SPA) history模式调用微信jssdk 跳转后偶尔 "invalid signature"错误解决方案
项目背景 vue-cli生成的单页面项目,router使用history模式.产品会在公众号内使用,需要添加微信JSSDK,做分享相关配置. 遇到的问题 相关配置与JS接口安全域名都已经ok,发布后, ...