Thrift是Apache的一个开源的跨语言服务开发框架,它提供了一个代码生成引擎来构建服务,支持C++,Java,Python,PHP,Ruby,Erlang,Perl,Haskell,C#,Cocoa,JavaScript,Node.js,Smalltalk,OCaml,Delphi等多种编程语言。
一般来说,使用Thrift来开发应用程序,主要建立在两种场景下:
- 第一,在我们开发过程中,一个比较大的项目需要多个团队进行协作,而每个团队的成员在编程技术方面的技能可能不一定相同,为了实现这种跨语言的开发氛围,使用Thrift来构建服务
- 第二,企业之间合作,在业务上不可避免出现跨语言的编程环境,使用Thrift可以达到类似Web Services的跨平台的特性
安装配置Thrift
Thrift的编译器使用C++编写的,在安装编译器之前,首先应该保证操作系统基本环境支持C++的编译,安装相关依赖的软件包,如下所示
1 | sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel |
下载Thrift的软件包,并解压缩:
2 | tar -xvzf thrift-0.9.0. tar .gz |
配置、编译、安装Thrift,如下所示:
如果在配置的时候总是报如下错误:
可能是没有安装openssl-devel,可以安装这个软件包,或者,如果已经安装了这个软件包,可以执行以下命令:
1
sudo yum update
如果需要的话,可以运行测试用例:
安装成功以后,可以输入如下命令行进行验证:
02 | Usage: thrift [options] file |
04 | -version Print the compiler version |
05 | -o dir Set the output directory for gen-* packages |
06 | (default: current directory) |
07 | -out dir Set the ouput location for generated files. |
08 | (no gen-* folder will be created) |
09 | -I dir Add a directory to the list of directories |
10 | searched for include directives |
11 | -nowarn Suppress all compiler warnings (BAD!) |
12 | -strict Strict compiler warnings on |
13 | -v[erbose] Verbose mode |
14 | -r[ecurse] Also generate included files |
15 | -debug Parse debug trace to stdout |
16 | --allow-neg-keys Allow negative field keys (Used to preserve protocol |
17 | compatibility with older .thrift files) |
18 | --allow-64bit-consts Do not print warnings about using 64-bit constants |
19 | --gen STR Generate code with a dynamically-registered generator. |
20 | STR has the form language[:key1=val1[,key2,[key3=val3]]]. |
21 | Keys and values are options passed to the generator. |
22 | Many options will not require values. |
使用Thrift
我们直接使用Thrift官网提供的简单例子,验证一下。Thrift定义文件为user.thrift,如下所示:
struct UserProfile {
1: i32 uid,
2: string name,
3: string blurb
}
service UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}
然后,使用Thrift编译器来进行编译,生成Java、C++、PHP、Perl和C#代码,执行命令:
01 | [hadoop@master thrift]$ ls |
03 | [hadoop@master thrift]$ thrift --gen java user.thrift |
04 | [hadoop@master thrift]$ thrift --gen cpp user.thrift |
05 | [hadoop@master thrift]$ thrift --gen php user.thrift |
06 | [hadoop@master thrift]$ thrift --gen perl user.thrift |
07 | [hadoop@master thrift]$ thrift --gen csharp user.thrift |
08 | [hadoop@master thrift]$ thrift --gen py user.thrift |
09 | [hadoop@master thrift]$ ls |
10 | gen-cpp gen-csharp gen-java gen-perl gen-php gen-py user.thrift |
可以看到,生成了对应的gen-的目录,每个目录 下面都是对应的代码,下面看下,生成的代码:
如果想要生成其他编程语言的代码,可以参考Thrift命令支持的语言,如下所示:
01 | Available generators (and options): |
03 | bindable: Add [bindable] metadata to all the struct classes. |
04 | c_glib (C, using GLib): |
06 | log_unexpected: Log every time an unexpected field ID or type is encountered. |
08 | cob_style: Generate "Continuation OBject"-style classes. |
10 | Omit calls to completion__() in CobClient class. |
11 | templates: Generate templatized reader/writer methods. |
12 | pure_enums: Generate pure enums instead of wrapper classes. |
13 | dense: Generate type specifications for the dense protocol. |
14 | include_prefix: Use full include paths in generated files. |
16 | async: Adds Async CTP support. |
17 | wcf: Adds bindings for WCF to generated classes. |
18 | serial: Add serialization support to generated classes. |
21 | ansistr_binary: Use AnsiString as binary properties. |
27 | beans: Members will be private, and setter methods will return void. |
28 | private-members: Members will be private, but setter methods will return 'this' like usual. |
29 | nocamel: Do not use CamelCase field accessors with beans. |
30 | hashcode: Generate quality hashCode methods. |
31 | android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above). |
32 | java5: Generate Java 1.5 compliant code (includes android_legacy flag). |
35 | jquery: Generate jQuery compatible code. |
36 | node: Generate node.js compatible code. |
40 | inlined: Generate PHP inlined files |
41 | server: Generate PHP server stubs |
42 | oop: Generate PHP with object oriented subclasses |
43 | rest: Generate PHP REST processors |
45 | new_style: Generate new-style classes. |
46 | twisted: Generate Twisted-friendly RPC services. |
47 | utf8strings: Encode/decode strings using utf8 in the generated code. |
48 | slots: Generate code using slots for instance members. |
49 | dynamic: Generate dynamic code, less code generated but slower. |
50 | dynbase=CLS Derive generated classes from class CLS instead of TBase. |
51 | dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase. |
52 | dynimport='from foo.bar import CLS' |
53 | Add an import line to generated code to find the dynbase class. |
55 | rubygems: Add a "require 'rubygems'" line to the top of each generated file. |