Thrift框架简介与跨语言特性
此处简要介绍开源框架Thrift的安装流程及其基础使用案例,更深入的解析将在后续内容中展开。
Thrift简述
Thrift是Facebook开发的可扩展跨语言服务框架,现已开源并纳入Apache项目。其核心功能是利用自定义接口定义语言(IDL)生成基于RPC的客户端与服务端代码,这一过程由内置的代码生成器完成。Thrift的跨语言能力体现在它能生成C++、Java、Python、PHP、Ruby、Erlang、Perl、Haskell、C#、Cocoa、JavaScript、Node.js、Smalltalk、OCaml、Delphi等多种语言的代码,并实现这些语言间的透明通信。
Thrift的安装
本次安装使用的版本为:Thrift v0.9.1
操作系统版本:Ubuntu 14.04 64位
基本安装环境要求如下:
g++ 4.2 boost 1.53.0 libssl-dev
由于Thrift编译器即代码生成器由C++编写,因此需要g++进行编译安装。此外,Thrift源码中使用了boost库的相关实现(如shared_ptr),故需预先安装boost库。
Thrift在通信过程中利用SSL对数据进行安全封装。若未安装该库,configure阶段将报错:"Error: libcrypto required."。
Thrift提供TThreadSever、TThreadPoolServer、TNonblockingServer四种服务器框架。TSimpleServer采用单一主线程阻塞方式处理事件;TThreadPoolServer采用多线程阻塞方式提供服务;TNonblockingServer则以多线程非阻塞方式工作。使用TNonblockingServer模型需预先安装libevent和libevent-dev库,因为libevent是包含poll、select、epoll等异步处理函数的程序库。
具体安装步骤如下:
$./configure
$make
#sudo make install
configure执行结果的最后一部分显示,其中"Build TNonblockingServer .. : yes"的结果对于使用异步服务器模型是必须的。
anonymalias@anonymalias-Rev-1-0:~/download/thrift-0.9.1$./configure
......
thrift 0.9.1
Building C++ Library ......... : yes
Building C (GLib) Library .... : no
Building Java Library ........ : no
Building C# Library .......... : no
Building Python Library ...... : yes
Building Ruby Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no
C++ Library:
Build TZlibTransport ...... : yes
Build TNonblockingServer .. : yes
Build TQTcpServer (Qt) .... : no
Python Library:
Using Python .............. : /usr/bin/python
If something is missing that you think should be present,
please skim the output of configure to find the missing
component. Details are present in config.log.
在我的电脑上执行make时会出现以下bug:
ar: .libs/ThriftTest_constants.o: No such file or directory
不清楚Makefile是如何生成的,导致上述编译文件路径出现问题。解决方法有两种:
method1:
解决的方法时直接从Github(git://git.apache.org/thrift.git)上git clone 源码,先运行./bootstrap.sh,在按照configure安装。
method2:
可以将已经编译的test/cpp/*.o复制到test/cpp/.libs后,继续编译就可以了
cp test/cpp/*.o test/cpp/.libs/
Thrift的简单示例
首先创建Thrift语法规则文件,命名为server.thrift,内容如下:
struct message
{
1:i32 seqId,
2:string content
}
service serDemo
{
void put(1:message msg)
}
在shell下执行命令: thrift -gen cpp server.thrift
该语句用于创建C++服务框架。创建成功后,目录下会生成gen-cpp文件夹。随后修改该目录下的serDemo_server.skeleton.cpp,在put函数中添加如下代码:
class serDemoHandler : virtual public serDemoIf {
public:
serDemoHandler() {
// Your initialization goes here
}
void put(const message& msg) {
// Your implementation goes here
printf("receive message: id: %d, content: %s\n", msg.seqId, msg.content.c_str());
}
然后进行编译即可:g++ -o server *.cpp -lthrift
上述是server框架的代码。对于client框架,虽然已创建,但需添加client执行代码。可在该目录下创建client.cpp,输入以下内容。下面的内容可作为SimpleServer client的模板,只需修改注释部分。
// -------------------------替换成对应service名字的.h 文件------------------------
#include "SerDemo.h"
//------------------------------------------------------------------------------
#include
#include
#include
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 socket(new TSocket("localhost", 9090));
boost::shared_ptr transport(new TBufferedTransport(socket));
boost::shared_ptr protocol(new TBinaryProtocol(transport));
transport->open();
// ----------------------------我们的代码写在这里------------------------------
message msg;
msg.seqId = 1;
msg.content = "client message";
client.put(msg);
//--------------------------------------------------------------------------
transport->close();
return 0;
}
然后进行编译:g++ -o client *[^n].cpp - lthrift。也可以将serDemo_server.skeleton.cpp移动到其它目录,使用g++ -o client *.cpp - lthrift命令。
之后即可执行。启动server后,再启动client,server执行结果如下:
anonymalias@anonymalias-Rev-1-0:~/code/thriftSerDemo/gen-cpp$ ./server
receive message: id: 1, content: client message
以上就是小编为大家带来的Thrift的安装方法和简单实例全部内容了,希望大家多多支持本站~