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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM