Thrift在Windows下的安装与实例vs.docx
《Thrift在Windows下的安装与实例vs.docx》由会员分享,可在线阅读,更多相关《Thrift在Windows下的安装与实例vs.docx(15页珍藏版)》请在冰豆网上搜索。
Thrift在Windows下的安装与实例vs
Windows下安装Thrift框架及工程实例
本文的不同之处在于,不借助Cygwin或者MinGW,只用VS2010,和Thrift官网下载的源文件,安装Thrift并使用。
本文可用于thrift-0.9.1.tar.gz,thrift-0.10.0.tar.gz版本安装
先从官网 下载这两个文件:
●thrift-0.9.1.tar.gz
●ThriftcompilerforWindows(thrift-0.9.1.exe)
第一个文件是源代码包(包含库文件工程等),第二个可执行文件用于在Windows下生成目标语言的代码(用于thrift转其他语言代码生成等)。
●除此以外,还需要boost库,libevent库,openssl库。
若安装有问题阅读相应thrift版本的README.md使用推荐版本库安装
如:
c++工程下:
thrift-0.10.0\lib\cpp
安装Thrift
0)准备工作
●thrift-0.9.1.tar.gz源码包(thrift-0.10.0.tar.gz)
●安装VS2010(若已安装忽略此步)
●安装boost库,我使用的boost1.51(boost1.63)版本(官网下载后需要相关编译生成库文件)
●安装libevent库,这里用的libevent-2.0.21-stable
●安装openssl库(OpenSSL-Win32)可下载编译好的openssl库(自己编译需要很多工具,且编译过程问题很多)
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
●若使用openssl库,添加相关配置
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步:
用ThriftcompilerforWindows(thrift-0.9.1.exe) ,生成目标语言代码;
第3步:
服务器端程序引入thrift生成的代码,实现RPC业务代码。
第4步:
客户端引入代码,调用远程服务。
图中蓝色Thrift.exe就是从官网下载的第二个文件——“IDL翻译工具”,帮助你把.thrift文件“翻译”成目标语言的RPC代码。
例子
这个例子,远程Server提供一个函数。
客户端调用这个函数。
远程函数的功能很简单,就是输出“Hello Thrift”。
1)写.thrift文件
新建文本文件hello.txt,保存下面的内容后修改扩展名hello.thrift
servicehello
{
voidfunc1()
}
hello.thrift
2)生成目标语言代码
●把官网下载到的第二个文件thrift-0.9.1.exe和hello.thrift放到一个目录(hello)下。
●打开cmd命令行窗口,进入到这个目录,
●执行命令:
(thrift--gen cpp hello.thrift)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文件(注意添加库文件#pragmacomment(lib,"libthrift.lib"))。
●也可创建一个公共源代码文件(在工程中分别添加进去)
●最终解决方案的文件结构是这样的:
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
●若使用OpenSSL添加相关路径依赖及引入库文件
●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
#include"hello.h"
#include
#include
#include
#include
#include
#pragmacomment(lib,"libthrift.lib")//链接库文件
#pragmacomment(lib,"ssleay32.lib")
#pragmacomment(lib,"libeay32.lib")
usingnamespace:
:
apache:
:
thrift;
usingnamespace:
:
apache:
:
thrift:
:
protocol;
usingnamespace:
:
apache:
:
thrift:
:
transport;
usingnamespace:
:
apache:
:
thrift:
:
server;
usingboost:
:
shared_ptr;
intmain(intargc,char**argv){
intport=9090;
shared_ptrsocket(newTSocket("127.0.0.1",9090));
shared_ptrtransport(newTBufferedTransport(socket));
shared_ptrprotocol(newTBinaryProtocol(transport));
helloClientclient(protocol);
try{
transport->open();
client.func1();
transport->close();
}catch(TException&tx){
printf("ERROR:
%s\n",tx.what());
}
getchar();
return0;
}
client.cpp
7)Server代码
hello_server.skeleton.cpp 文件已经有thrift生成的代码,稍作修改,最终如下:
//Thisautogeneratedskeletonfileillustrateshowtobuildaserver.
//Youshouldcopyittoanotherfilenametoavoidoverwritingit.
#include"hello.h"
#include
#include
#include
#include
#pragmacomment(lib,"libthrift.lib")
#pragmacomment(lib,"ssleay32.lib")
#pragmacomment(lib,"libeay32.lib")
usingnamespace:
:
apache:
:
thrift;
usingnamespace:
:
apache:
:
thrift:
:
protocol;
usingnamespace:
:
apache:
:
thrift:
:
transport;
usingnamespace:
:
apache:
:
thrift:
:
server;
usingboost:
:
shared_ptr;
classhelloHandler:
virtualpublichelloIf{
public:
helloHandler(){
//Yourinitializationgoeshere
}
voidfunc1(){
//Yourimplementationgoeshere
printf("func1\n");
}
};
intmain(intargc,char**argv){
intport=9090;
shared_ptrhandler(newhelloHandler());
shared_ptrprocessor(newhelloProcessor(handler));
shared_ptrserverTransport(newTServerSocket(port));
shared_ptrtransportFactory(newTBufferedTransportFactory());
shared_ptrprotocolFactory(newTBinaryProtocolFactory());
TSimpleServerserver(processor,serverTransport,transportFactory,protocolFactory);
server.serve();
return0;
}
hello_server.skeleton.cpp
8)调试运行
先启动Server工程,再启动Client工程。
运行结果:
总结
到这里Thrift的安装和开发基本操作步骤就介绍完了。
Thrift的官方文档不是很完善,本篇介绍的安装方法不在网上众多教程之列,主要区别是没有使用Cygwin或者MinGW。
编译问题
● 若在libthrift库中不使用openssl去除libthrift库工程中文件
TSSLSocket.cpp
TSSLSocket.h
若使用openssl应用程序工程文件中加载Openssl库
#pragmacomment(lib,"ssleay32.lib")
#pragmacomment(lib,"libeay32.lib")
否则链接库出错
●Server工程编译问题
最重要的一步,需要在libthrift库工程项目中引入TServerFramework和TConnectedClient
否则你可能会出现这样的错误:
I'mtestagaintobuildthelib,findthatmustbeaddTConnectedClientandTServerFramework.
路径:
thrift-0.10.0\lib\cpp\src\thrift\server
更多编译问题可以到其管网论坛搜索解决方案https:
//issues.apache.org/jira/browse/THRIFT-3810