zoukankan      html  css  js  c++  java
  • Apache Thrift 在Windows下的安装与开发

    Windows下安装Thrift框架的教程很多。本文的不同之处在于,不借助Cygwin或者MinGW,只用VS2010,和Thrift官网下载的源文件,安装Thrift并使用。

    先从官网 下载这两个文件:

    · thrift-0.9.1.tar.gz

    · 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

    进入 hrift-0.9.1libcpp,VS2010打开Thrift.sln,有libthrift,libthriftnb两个工程。

    两个工程的区别是,libthriftnb工程是非阻塞(non-blocking)模式的服务器,非阻塞模式需要依赖libevent库。

    2)libthrift工程配置:

    libthrift>属性->C/C++->常规->附加包含目录->oostoost_1_51

    libthrift>属性->库管理器->常规->附加库目录->oostoost_1_51lib

    3)libthriftnb工程配置:

    libthriftnb>属性->C/C++->常规->附加包含目录->

    oostoost_1_51

    libevent-2.0.21-stable

    libevent-2.0.21-stableinclude

    libevent-2.0.21-stableWIN32-Code

    libthriftnb>属性->库管理器->常规->附加库目录->

    oostoost_1_51lib   

    4)编译libthrift和libthriftnb工程

    编译完成后,在 hrift-0.9.1libcppDebug下生成libthrift.lib文件,和libthriftnb.lib文件。

    选择release模式,则在 hrift-0.9.1libcppRelease下生成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

    1 service hello {
    2     void func1( )
    3 }

    2)生成目标语言代码

    把官网下载到的第二个文件thrift-0.9.1.exe和hello.thrift放到一个目录(hello)下。

    打开cmd命令行窗口,进入到这个目录,执行命令:

    C:UsersadminDesktopHello>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++->常规->附加包含目录->oostoost_1_51

    Sever工程 Server>属性->C/C++->常规->附加包含目录-> hrift-0.9.1libcppsrc

    Sever工程 Server>属性->C/C++->常规->附加包含目录-> hrift-0.9.1libcppsrc hrift

    Sever工程 Server>属性->连接器->附加库目录->oostoost_1_51lib

    Sever工程 Server>属性->连接器->附加库目录-> hrift-0.9.1libcppDebug

    附加库目录指向的是刚刚编译出的Debug目录

    类似的,Client工程也做这样的配置。

    Client工程 Client>属性->C/C++->常规->附加包含目录->oostoost_1_51

    Client工程 Client>属性->C/C++->常规->附加包含目录-> hrift-0.9.1libcppsrc

    Client工程 Client>属性->C/C++->常规->附加包含目录-> hrift-0.9.1libcppsrc hrift

    Client工程 Client>属性->连接器->附加库目录->oostoost_1_51lib

    Client工程 Client>属性->连接器->附加库目录-> hrift-0.9.1libcppDebug

    6)Client代码

    client.cpp文件是空的,添加代码:

     1 #include <transport/TSocket.h>
     2 #include "hello.h"
     3 #include <protocol/TBinaryProtocol.h>
     4 #include <server/TSimpleServer.h>
     5 #include <transport/TServerSocket.h>
     6 #include <transport/TBufferTransports.h>
     7 #include <string>
     8 #pragma comment(lib, "libthrift.lib")
     9 using namespace ::apache::thrift;
    10 using namespace ::apache::thrift::protocol;
    11 using namespace ::apache::thrift::transport;
    12 using namespace ::apache::thrift::server;
    13 
    14 using boost::shared_ptr;
    15 
    16 #pragma comment(lib,"libthrift.lib")//链接库文件
    17 
    18 int main(int argc, char** argv) {
    19     int port = 9090;
    20     shared_ptr<TTransport> socket(new TSocket("127.0.0.1", 9090));
    21     shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    22     shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    23     helloClient client(protocol);
    24     try{
    25         transport->open();
    26 
    27         client.func1();
    28 
    29         transport->close();
    30     }catch(TException& tx){
    31         printf("ERROR:%s
    ",tx.what());
    32     }
    33     getchar();
    34     return 0;
    35 }

    7)Server代码

    hello_server.skeleton.cpp 文件已经有thrift生成的代码,稍作修改,最终如下:

     1 // This autogenerated skeleton file illustrates how to build a server.
     2 
     3 // You should copy it to another filename to avoid overwriting it.
     4 
     5  
     6 
     7 #include "hello.h"
     8 
     9 #include <thrift/protocol/TBinaryProtocol.h>
    10 
    11 #include <thrift/server/TSimpleServer.h>
    12 
    13 #include <thrift/transport/TServerSocket.h>
    14 
    15 #include <thrift/transport/TBufferTransports.h>
    16 
    17 #pragma comment(lib, "libthrift.lib")
    18 
    19 using namespace ::apache::thrift;
    20 
    21 using namespace ::apache::thrift::protocol;
    22 
    23 using namespace ::apache::thrift::transport;
    24 
    25 using namespace ::apache::thrift::server;
    26 
    27  
    28 
    29 using boost::shared_ptr;
    30 
    31  
    32 
    33 class helloHandler : virtual public helloIf {
    34 
    35  public:
    36 
    37   helloHandler() {
    38 
    39     // Your initialization goes here
    40 
    41   }
    42 
    43  
    44 
    45   void func1() {
    46 
    47     // Your implementation goes here
    48 
    49     printf("Hello Thrift
    ");
    50 
    51   }
    52 
    53 };
    54 
    55  
    56 
    57 int main(int argc, char **argv) {
    58 
    59     //-----------------------------//
    60     WORD wVersionRequested;
    61 
    62     WSADATA wsaData;
    63 
    64     int err;
    65 
    66     wVersionRequested =MAKEWORD( 2, 2 );
    67 
    68     err = WSAStartup( wVersionRequested, &wsaData );
    69     //-------------------------------//
    70     //对上面这段代码做个说明,这是依赖windows的一段代码
    71     //到2014.9.2官网的稳定版0.9.1,仍需要这段代码才可以在windows下编译通过。
    72     //但是如果用git clone最新版,这个错误已经修正
    73     //最新版注释掉这段代码,一样可以在windows下编译通过。
    74     //备注时间:2014.9.2
    75  
    76 
    77   int port = 9090;
    78 
    79   shared_ptr<helloHandler> handler(new helloHandler());
    80 
    81   shared_ptr<TProcessor> processor(new helloProcessor(handler));
    82 
    83   shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
    84 
    85   shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
    86 
    87   shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    88 
    89  
    90 
    91   TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    92 
    93   server.serve();
    94 
    95   return 0;
    96 
    97 }

    8)调试运行

    先启动Server工程,再启动Client工程。运行结果:

     

    总结

    到这里Thrift的安装和开发基本操作步骤就介绍完了。Thrift的官方文档不是很完善,本篇介绍的安装方法不在网上众多教程之列,主要区别是没有使用Cygwin或者MinGW。对于想使用Visual Studio作为开发环境的同学会很有帮助。

    教程中需要的文件,都可以从网上获取,核心代码在文中已经展示,相信按图索骥一定可以成功的运行第一个Thrift的例子。

    最后十分感谢陈晓苏(北京)同学,本篇教程整理自他/她名字的一个文件夹。我从Thrift交流QQ群193713524的共享中获得的。这里增加了一些图片说明和我的理解。最后的例子,做了简化,是为了直接、清晰的说明安装过程和开发的配置。

    转自:http://blog.csdn.net/colouroo/article/details/38588297

  • 相关阅读:
    CentOS6.9下ssh密钥登录配置步骤(免密码登录)和ssh-keygen 命令常用参数
    Debian9.5下ssh密钥登录配置步骤(免密码登录)和ssh-keygen 命令常用参数
    CentOS6.9下sftp配置和scp用法
    Debian9.5下sftp配置和scp用法
    SSH概述与配置文件说明
    Linux下的权限掩码umask
    gcc 高版本兼容低版本 技巧 :指定 -specs={自定义specs文件} 参数。可以搞定oracle安装问题
    apt 之 最强技能:【欺骗】,文雅点【偷梁换柱】!
    续:纠正:ubuntu 【6.04 LTS】可以安装安装 ! for《Oracle-10.2.0.1,打补丁10.2.0.5:在 debian 版本4【不含4】以上,及 ubuntu 7.04【不含7.04】以上都可以安装!》
    ubuntu 4.10~5.10 :古老的ubuntu上安装oracle10g的情况
  • 原文地址:https://www.cnblogs.com/lpxblog/p/5382007.html
Copyright © 2011-2022 走看看