本文只是简单的讲解Thrift开源框架的安装和简单使用示例,对于详细的讲解,后面在进行阐述。
Thrift简述
Thrift是一款由Fackbook开发的可伸缩、跨语言的服务开发框架,该框架已经开源并且加入的Apache项目。Thrift主要功能是:通过自定义的Interface Definition Language(IDL),可以创建基于RPC的客户端和服务端的服务代码。服务代码的生成是通过Thrift内置的代码生成器来实现的。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时出现configure: error: "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/
新闻热点
疑难解答