最近需要使用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
- 下载安装Boost,记住{Boost安装目录}。安装过程见Windows 10 Visual Studio 2017 安装配置 Boost。
- 下载安装OpenSSL,下载网址。安装过程见Windows 10 Visual Studio 2017 安装配置 OpenSSL。记住{OpenSSL 目录}
- 从 Apache Thrift 官网下载 Windows 平台的 Thrift 源代码和编译器。
- 下载libevent,非阻塞的 Thrift 服务器需要这个库。
- 解压缩下载的文件。
- 去{thrift 安装目录}\lib\cpp 目录,点击thrift.sln,打开 VS 项目,里面有两个项目libthrift 和 libthriftnb。
- 会有一个对话框询问是否升级,点击升级。
- 打开 Developer Command Prompt for VS 2017。
- 在 Developer Command Prompt 中进入 {libevent 安装目录}。
- 输入 nmake -f Makefile.nmake 来安装libevent。
- 完后后,右键 libthrift项目,点击属性 > C/C++ > 常规。
- 在附加包含目录中添加:
{boost 安装目录}\boost_1_64_0;{boost 安装目录}\boost_1_64_0\boost;{OpenSSL 目录}\inc32
- 点击库管理器 > 附加库目录,添加如下文件:
{OpenSSL 目录}\out32dll
- 右键 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;
- 点击库管理器 > 附加库目录,添加如下文件:
{OpenSSL 目录}\out32dll
- 然后编译生成文件,如果使用DEBUG模式,会在{thrift 目录}\lib\cpp\DEBUG中生成libthrift.lib。
4 建立 Server、Client 示例
4.1 建立 Thrift C++ Server
- 创建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()
}
-
编译生成 C++ 源文件,会生成 gen-cpp文件夹
thrift -r --gen cpp Hello.thrift
生成的文件如下:
-
新建 Visual Studio 项目,并将生成的文件粘贴入项目文件夹中。
-
我们只需要实现
Hello_server.skeleton.cpp
中的方法即可。 -
右键项目,点击属性 > C/C++ > 常规 > 附加包含目录,添加:
{thrift 安装目录}\lib\cpp\src;{thrift 安装目录}\lib\cpp\src\thrift\windows;{boost 安装目录}\boost\boost_1_64_0;%(AdditionalIncludeDirectories)
-
点击链接器 > 常规 > 附加库目录,添加:
{boost 安装目录}\boost\boost_1_64_0\stage\lib;{thrift 安装目录}\lib\cpp\Debug;%(AdditionalLibraryDirectories)
-
点击链接器 > 所有选项 > 附加依赖项,添加:
libboost_thread-vc141-mt-gd-1_64.lib;libboost_chrono-vc141-mt-gd-1_64.lib;libthrift.lib;
-
如果是 0.91 之前的 thrift,还需要在
Hello_server.skeleton.cpp
源文件main
函数前面加上如下代码:
WSADATA wsaData = {};
WORD wVersionRequested = MAKEWORD(2, 2);
int err = WSAStartup(wVersionRequested, &wsaData);
- 点击生成,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;
}