分享

Thrift了解3:Thrift使用实例--生成各种语言指导

pig2 2014-3-16 23:16:17 发表于 常识型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 29327
本帖最后由 pig2 于 2014-3-16 23:18 编辑
我们了解Thrift是什么,如何使用Thrift,这里进一步了解什么情况下使用Thrift,同时让我们对Thrift的理解加深
1.Thrift的使用场景是什么?
2.多个公司,多个团队,多种技术,该使用什么框架?
3.扩展:Thrift与代码生成器之间的关系是什么?

已有(2)人评论

跳转到指定楼层
pig2 发表于 2014-3-16 23:23:54
本帖最后由 pig2 于 2014-3-16 23:56 编辑
Thrift安装环境:CentOS


一般来说,使用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的软件包,并解压缩:
  1. wget http://mirrors.cnnic.cn/apache/thrift/0.9.0/thrift-0.9.0.tar.gz
  2. tar -xvzf thrift-0.9.0.tar.gz
复制代码
配置、编译、安装Thrift,如下所示:
  1. sudo ./configure
  2. sudo make
  3. sudo make install
复制代码
如果在配置的时候总是报如下错误:

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

sudo yum update

如果需要的话,可以运行测试用例:
  1. sudo make check
复制代码
安装成功以后,可以输入如下命令行进行验证:
  1. thrift --help
  2. Usage: thrift [options] file
  3. Options:
  4.   -version    Print the compiler version
  5.   -o dir      Set the output directory for gen-* packages
  6.                (default: current directory)
  7.   -out dir    Set the ouput location for generated files.
  8.                (no gen-* folder will be created)
  9.   -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#代码,执行命令:
  1. [hadoop@master thrift]$ ls
  2. user.thrift
  3. [hadoop@master thrift]$ thrift --gen java user.thrift
  4. [hadoop@master thrift]$ thrift --gen cpp user.thrift
  5. [hadoop@master thrift]$ thrift --gen php user.thrift
  6. [hadoop@master thrift]$ thrift --gen perl user.thrift
  7. [hadoop@master thrift]$ thrift --gen csharp user.thrift
  8. [hadoop@master thrift]$ thrift --gen py user.thrift
  9. [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. ./user:
  6. constants.py  __init__.py  ttypes.py  UserStorage.py  UserStorage-remote
复制代码
如果想要生成其他编程语言的代码,可以参考Thrift命令支持的语言,如下所示:
  1. Available generators (and options):
  2.   as3 (AS3):
  3.     bindable:          Add [bindable] metadata to all the struct classes.
  4.   c_glib (C, using GLib):
  5.   cocoa (Cocoa):
  6.     log_unexpected:  Log every time an unexpected field ID or type is encountered.
  7.   cpp (C++):
  8.     cob_style:       Generate "Continuation OBject"-style classes.
  9.     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):
复制代码
上一篇:
Thrift了解2: Thrift使用方法

下一篇:
Thrift了解4:C#通过Thrift操作HBase实战



回复

使用道具 举报

xuliang123789 发表于 2016-3-24 10:12:13
谢谢楼主,学习一下,赞~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条