CentOs安装Thrift

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,如下所示:

1 sudo ./configure
2 sudo make
3 sudo make install

如果在配置的时候总是报如下错误:

可能是没有安装openssl-devel,可以安装这个软件包,或者,如果已经安装了这个软件包,可以执行以下命令:
1
sudo yum update

如果需要的话,可以运行测试用例:

1 sudo make check

安装成功以后,可以输入如下命令行进行验证:

01 thrift --help
02 Usage: thrift [options] file
03 Options:
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
02 user.thrift
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-的目录,每个目录 下面都是对应的代码,下面看下,生成的代码:

  • Java代码

    生成2个Java文件:

    1 [hadoop@master thrift]$ cd gen-java/
    2 [hadoop@master gen-java]$ ls
    3 UserProfile.java  UserStorage.java

    具体代码可以查看相应的代码文件。

  • C++代码

    生成多个C++文件:

    1 [hadoop@master thrift]$ cd gen-cpp/
    2 [hadoop@master gen-cpp]$ ls
    3 user_constants.cpp  UserStorage.cpp  UserStorage_server.skeleton.cpp  user_types.h
    4 user_constants.h    UserStorage.h    user_types.cpp

    具体代码可以查看相应的代码文件。

  • PHP代码

    生成2个文件:

    1 [hadoop@master thrift]$ cd gen-php/
    2 [hadoop@master gen-php]$ ls
    3 Types.php  UserStorage.php

    具体代码可以查看相应的代码文件。

  • Perl代码

    生成3个文件:

    1 [hadoop@master thrift]$ cd gen-perl/
    2 [hadoop@master gen-perl]$ ls
    3 Constants.pm  Types.pm  UserStorage.pm

    具体代码可以查看相应的代码文件

  • C#代码

    生成2个文件:

    1 [hadoop@master thrift]$ cd gen-csharp/
    2 [hadoop@master gen-csharp]$ ls
    3 UserProfile.cs  UserStorage.cs

    具体代码可以查看相应的代码文件。

  • Python代码

    生成一个__init__.py文件,和一个目录user:

    1 [hadoop@master thrift]$ cd gen-py/
    2 [hadoop@master gen-py]$ ls -R
    3 .:
    4 __init__.py  user
    5  
    6 ./user:
    7 constants.py  __init__.py  ttypes.py  UserStorage.py  UserStorage-remote

如果想要生成其他编程语言的代码,可以参考Thrift命令支持的语言,如下所示:

01 Available generators (and options):
02   as3 (AS3):
03     bindable:          Add [bindable] metadata to all the struct classes.
04   c_glib (C, using GLib):
05   cocoa (Cocoa):
06     log_unexpected:  Log every time an unexpected field ID or type is encountered.
07   cpp (C++):
08     cob_style:       Generate "Continuation OBject"-style classes.
09     no_client_completion:
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.
15   csharp (C#):
16     async:           Adds Async CTP support.
17     wcf:             Adds bindings for WCF to generated classes.
18     serial:          Add serialization support to generated classes.
19   d (D):
20   delphi (delphi):
21     ansistr_binary:  Use AnsiString as binary properties.
22   erl (Erlang):
23   go (Go):
24   hs (Haskell):
25   html (HTML):
26   java (Java):
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).
33   javame (Java ME):
34   js (Javascript):
35     jquery:          Generate jQuery compatible code.
36     node:            Generate node.js compatible code.
37   ocaml (OCaml):
38   perl (Perl):
39   php (PHP):
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
44   py (Python):
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.
54   rb (Ruby):
55     rubygems:        Add a "require 'rubygems'" line to the top of each generated file.
56   st (Smalltalk):
57   xsd (XSD):
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值