1、Thrift在Windows下的安装与实例vsWindows下安装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转其他语言代码生
2、成等)。 除此以外,还需要boost库,libevent库,openssl库。若安装有问题阅读相应thrift版本的README.md使用推荐版本库安装如:c+ 工程下 :thrift-0.10.0libcpp安装Thrift0)准备工作 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-Win3
3、2)可下载编译好的openssl库(自己编译需要很多工具,且编译过程问题很多)1)解压缩thrift-0.9.1.tar.gz 进入thrift-0.9.1libcpp,VS2010打开Thrift.sln,有libthrift,libthriftnb两个工程。 两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库。2)libthrift工程配置: libthrift属性-C/C+-常规-附加包含目录:boostboost_1_51 libthrift属性-库管理器-常规-附加库目录:boostboost_1_51l
4、ib 若使用openssl库,添加相关配置3)libthriftnb工程配置:libthriftnb属性-C/C+-常规-附加包含目录: boostboost_1_51 libevent-2.0.21-stable libevent-2.0.21-stableinclude libevent-2.0.21-stableWIN32-Codelibthriftnb属性-库管理器-常规-附加库目录: boostboost_1_51lib4)编译libthrift和libthriftnb工程 编译完成后,在thrift-0.9.1libcppDebug下生成libthrift.lib文件,和libth
5、riftnb.lib文件。 选择release模式,则在thrift-0.9.1libcppRelease下生成libthrift.lib文件和libthriftnb.lib文件。若编译存在问题,可能是工程添加路径文件不对或者某些库文件不存在。至此,安装完成。开发步骤安装好thrift后,就可以开始开发了。开发过程分这么几步:第1步:写.thrift文件,也就是接口描述文件(InterfaceDescriptionFile);第2步:用ThriftcompilerforWindows(thrift-0.9.1.exe),生成目标语言代码;第3步:服务器端程序引入thrift生成的代码,实现RP
6、C业务代码。第4步:客户端引入代码,调用远程服务。图中蓝色Thrift.exe就是从官网下载的第二个文件“IDL翻译工具”,帮助你把.thrift文件“翻译”成目标语言的RPC代码。例子这个例子,远程Server提供一个函数。客户端调用这个函数。远程函数的功能很简单,就是输出“HelloThrift”。1)写.thrift文件新建文本文件hello.txt,保存下面的内容后修改扩展名hello.thriftservice hello void func1( )hello.thrift2)生成目标语言代码 把官网下载到的第二个文件thrift-0.9.1.exe和hello.thrift放到一个
7、目录(hello)下。 打开cmd命令行窗口,进入到这个目录, 执行命令:(thrift -gencpphello.thrift)C:UsersadminDesktopHellothrift-0.9.1.exe-gencpphello.thrift 执行成功,在hello目录下,生成一个gen-cpp文件夹。3)创建工程 VisualStudio2010新建win32控制台应用程序。 项目名称server 解决方案名称hello 注意:附加选项中选择勾选空项目。 类似的,在hello解决方案下,再新建一个空项目client。4)为项目添加文件 向Server项目添加文件。 复制gen-cpp文
8、件夹中文件到Server工程,添加到Server工程中。 向Client项目添加文件。 复制gen-cpp文件夹中文件到Client工程,删除hello_server.skeleton.cpp,并额外添加client.cpp文件(注意添加库文件#pragma comment(lib, libthrift.lib)。 也可创建一个公共源代码文件(在工程中分别添加进去) 最终解决方案的文件结构是这样的:5)配置项目属性 Sever工程Server属性-C/C+-常规-附加包含目录:boostboost_1_51 Sever工程Server属性-C/C+-常规-附加包含目录:thrift-0.9.1
9、libcppsrc Sever工程Server属性-C/C+-常规-附加包含目录:thrift-0.9.1libcppsrcthrift 若使用OpenSSL添加相关路径依赖及引入库文件 Sever工程Server属性-连接器-附加库目录:boostboost_1_51lib Sever工程Server属性-连接器-附加库目录:thrift-0.9.1libcppDebug附加库目录指向的是刚刚编译出的Debug目录(此处可将相关库文件放置在你的工程里面,修改附加库目录即可)类似的,Client工程也做这样的配置。 Client工程Client属性-C/C+-常规-附加包含目录:boostbo
10、ost_1_51 Client工程Client属性-C/C+-常规-附加包含目录:thrift-0.9.1libcppsrc Client工程Client属性-C/C+-常规-附加包含目录:thrift-0.9.1libcppsrcthrift Client工程Client属性-连接器-附加库目录:boostboost_1_51lib Client工程Client属性-连接器-附加库目录:thrift-0.9.1libcppDebug6)Client代码client.cpp文件是空的,添加代码:#include #include hello.h#include #include #includ
11、e #include #include #pragma comment(lib, libthrift.lib)/链接库文件#pragma comment (lib, ssleay32.lib)#pragma comment(lib, libeay32.lib)using namespace :apache:thrift;using namespace :apache:thrift:protocol;using namespace :apache:thrift:transport;using namespace :apache:thrift:server;using boost:shared_p
12、tr;int main(int argc, char* argv) int port = 9090; shared_ptr socket(new TSocket(127.0.0.1, 9090); shared_ptr transport(new TBufferedTransport(socket); shared_ptr protocol(new TBinaryProtocol(transport); helloClient client(protocol); try transport-open(); client.func1(); transport-close(); catch(TEx
13、ception& tx) printf(ERROR:%sn,tx.what(); getchar(); return 0;client.cpp7)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 #include
14、 #include #include #pragma comment(lib, libthrift.lib)#pragma comment (lib, ssleay32.lib)#pragma comment(lib, libeay32.lib)using namespace :apache:thrift;using namespace :apache:thrift:protocol;using namespace :apache:thrift:transport;using namespace :apache:thrift:server;using boost:shared_ptr;clas
15、s helloHandler : virtual public helloIf public: helloHandler() / Your initialization goes here void func1() / Your implementation goes here printf(func1n); ;int main(int argc, char *argv) int port = 9090; shared_ptr handler(new helloHandler(); shared_ptr processor(new helloProcessor(handler); shared
16、_ptr serverTransport(new TServerSocket(port); shared_ptr transportFactory(new TBufferedTransportFactory(); shared_ptr protocolFactory(new TBinaryProtocolFactory(); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0;hello_server.skeleton.cpp8
17、)调试运行先启动Server工程,再启动Client工程。运行结果:总结到这里Thrift的安装和开发基本操作步骤就介绍完了。Thrift的官方文档不是很完善,本篇介绍的安装方法不在网上众多教程之列,主要区别是没有使用Cygwin或者MinGW。编译问题 若在libthrift库中不使用openssl去除libthrift库工程中文件TSSLSocket.cppTSSLSocket.h若使用openssl应用程序工程文件中加载Openssl库#pragma comment (lib, ssleay32.lib)#pragma comment(lib, libeay32.lib)否则链接库出错 Server工程编译问题最重要的一步,需要在libthrift库工程项目中引入TServerFramework和TConnectedClient否则你可能会出现这样的错误:Im test again to build the lib, find that must be add TConnectedClient and TServerFramework.路径:thrift-0.10.0libcppsrcthriftserver更多编译问题可以到其管网论坛搜索解决方案https:/issues.apache.org/jira/browse/THRIFT-3810
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1