Windows 10 Visual Studio 2017 安装配置 Apache Thrift (C++)

最近需要使用Thrift,所以在网上看了很多资料,不过很多教程都不够详细完整,导致我花了不少时间安装配置。在这里我把我配置的过程写下来和大家分享。


1 介绍

Apache Thrift 是一个跨语言的远程过程调用框架(RPC,Remote Procedure Call)。首先使用接口描述语言(IDL,Interface Description Language)编写 .thrift 文件,然后通过 Thrift 编译成C++、JAVA、C# 等语言的代码。这些代码之间可以互相远程调用。Thrift 封装了底层网络通信的内容,用户只需要编写顶层逻辑代码就可以了。

2 测试环境

  • Windows 10
  • Microsoft Visual Studio 2017
  • Apache Thrift 0.9.2.
  • Boost 1.64.0.
  • libevent-2.1.8-stable
  • OpenSSL 1.0.2l

3 使用 Visual Sdutio 2017 编译生成 libthrift.lib

  1. 下载安装Boost,记住{Boost安装目录}。安装过程见Windows 10 Visual Studio 2017 安装配置 Boost
  2. 下载安装OpenSSL,下载网址。安装过程见Windows 10 Visual Studio 2017 安装配置 OpenSSL。记住{OpenSSL 目录}
  3. Apache Thrift 官网下载 Windows 平台的 Thrift 源代码和编译器。
  4. 下载libevent,非阻塞的 Thrift 服务器需要这个库。
  5. 解压缩下载的文件。
  6. {thrift 安装目录}\lib\cpp 目录,点击thrift.sln,打开 VS 项目,里面有两个项目libthriftlibthriftnb
  7. 会有一个对话框询问是否升级,点击升级。
  8. 打开 Developer Command Prompt for VS 2017
  9. 在 Developer Command Prompt 中进入 {libevent 安装目录}
  10. 输入 nmake -f Makefile.nmake 来安装libevent。
  11. 完后后,右键 libthrift项目,点击属性 > C/C++ > 常规
  12. 附加包含目录中添加:
    {boost 安装目录}\boost_1_64_0;{boost 安装目录}\boost_1_64_0\boost;{OpenSSL 目录}\inc32
  13. 点击库管理器 > 附加库目录,添加如下文件:
    {OpenSSL 目录}\out32dll
  14. 右键 libthriftnb项目,点击属性 > C/C++ > 常规。在附加包含目录中添加
    {boost 安装目录}\boost_1_64_0;{boost 安装目录}\boost_1_64_0\boost;{OpenSSL 目录}\inc32;{libevent_install_dir};{libevent_install_dir}\include;{libevent_install_dir}\WIN32-Code;
  15. 点击库管理器 > 附加库目录,添加如下文件:
    {OpenSSL 目录}\out32dll
  16. 然后编译生成文件,如果使用DEBUG模式,会在{thrift 目录}\lib\cpp\DEBUG中生成libthrift.lib。

4 建立 Server、Client 示例

4.1 建立 Thrift C++ Server

  1. 创建Hello.thrift文件
# Hello.thrift
namespace cpp Demo 
service Hello{ 
     string helloString(1:string para) 
     i32 helloInt(1:i32 para) 
     bool helloBoolean(1:bool para) 
     void helloVoid() 
     string helloNull() 
}
  1. 编译生成 C++ 源文件,会生成 gen-cpp文件夹
    thrift -r --gen cpp Hello.thrift
    生成的文件如下:

  2. 新建 Visual Studio 项目,并将生成的文件粘贴入项目文件夹中。

  3. 我们只需要实现Hello_server.skeleton.cpp中的方法即可。

  4. 右键项目,点击属性 > C/C++ > 常规 > 附加包含目录,添加:
    {thrift 安装目录}\lib\cpp\src;{thrift 安装目录}\lib\cpp\src\thrift\windows;{boost 安装目录}\boost\boost_1_64_0;%(AdditionalIncludeDirectories)

  5. 点击链接器 > 常规 > 附加库目录,添加:
    {boost 安装目录}\boost\boost_1_64_0\stage\lib;{thrift 安装目录}\lib\cpp\Debug;%(AdditionalLibraryDirectories)

  6. 点击链接器 > 所有选项 > 附加依赖项,添加:
    libboost_thread-vc141-mt-gd-1_64.lib;libboost_chrono-vc141-mt-gd-1_64.lib;libthrift.lib;

  7. 如果是 0.91 之前的 thrift,还需要在Hello_server.skeleton.cpp源文件main函数前面加上如下代码:

WSADATA wsaData = {};
WORD wVersionRequested = MAKEWORD(2, 2);
int err = WSAStartup(wVersionRequested, &wsaData);
  1. 点击生成,Server端就可以启动了。

这些库的名称要以你自己安装的库为准,你需要去boost文件夹中查看这些库的准确名称。上面 {} 里面的安装目录也是这样。

4.2 生成 Thrift C++ Client

和 Server 的配置过程一样,只不过我们不用Hello_server.skeleton.cpp。我们需要自己编写客户端:

#include "Hello.h"
#include <thrift/transport/TSocket.h>  
#include <thrift/transport/TBufferTransports.h>  
#include <thrift/protocol/TBinaryProtocol.h>  
#include <iostream>
#include <string>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using boost::shared_ptr;



int main(int argc, char **argv) {
	boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
	boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
	boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

        // 只需要实例化 HelloClient,然后就可以远程过程调用了
        Demo::HelloClient client(protocol);   

	transport->open();
	// Your Codes  

	std::cout << client.helloInt(10030341) << std::endl;
	std::string tem = "hello from Client";
	client.helloString(tem, tem);
	std::cout << tem << std::endl;

	transport->close();
	return 0;
}

4.3 演示

4.3.1 Server

4.3.2 Client

posted @ 2017-07-17 14:28  49er  阅读(2765)  评论(1编辑  收藏  举报