zoukankan      html  css  js  c++  java
  • Thrift CentOS安装和使用

    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++的编译,安装相关依赖的软件包,如下所示
     
     
    sudo yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel
    下载Thrift的软件包,并解压缩:
    tar -xvzf thrift-0.9.0.tar.gz
    配置、编译、安装Thrift,如下所示:
    sudo ./configure
    sudo make
    sudo make install
    如果在配置的时候总是报如下错误:
     
    可能是没有安装openssl-devel,可以安装这个软件包,或者,如果已经安装了这个软件包,可以执行以下命令:
    sudo yum update
     
    如果需要的话,可以运行测试用例:
     
    sudo make check
    安装成功以后,可以输入如下命令行进行验证:
     
    thrift --help
    Usage: thrift [options] file
    Options:
      -version    Print the compiler version
      -o dir      Set the output directory for gen-* packages
                   (default: current directory)
      -out dir    Set the ouput location for generated files.
                   (no gen-* folder will be created)
      -I dir      Add a directory to the list of directories
                    searched for include directives
      -nowarn     Suppress all compiler warnings (BAD!)
      -strict     Strict compiler warnings on
      -v[erbose]  Verbose mode
      -r[ecurse]  Also generate included files
      -debug      Parse debug trace to stdout
      --allow-neg-keys  Allow negative field keys (Used to preserve protocol
                    compatibility with older .thrift files)
      --allow-64bit-consts  Do not print warnings about using 64-bit constants
      --gen STR   Generate code with a dynamically-registered generator.
                    STR has the form language[:key1=val1[,key2,[key3=val3]]].
                    Keys and values are options passed to the generator.
                    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#代码,执行命令:
     
    [hadoop@master thrift]$ ls
    user.thrift
    [hadoop@master thrift]$ thrift --gen java user.thrift
    [hadoop@master thrift]$ thrift --gen cpp user.thrift
    [hadoop@master thrift]$ thrift --gen php user.thrift
    [hadoop@master thrift]$ thrift --gen perl user.thrift
    [hadoop@master thrift]$ thrift --gen csharp user.thrift
    [hadoop@master thrift]$ thrift --gen py user.thrift
    [hadoop@master thrift]$ ls
    gen-cpp  gen-csharp  gen-java  gen-perl  gen-php  gen-py  user.thrift
    可以看到,生成了对应的gen-的目录,每个目录 下面都是对应的代码,下面看下,生成的代码:
     
    Java代码
    生成2个Java文件:
     
    [hadoop@master thrift]$ cd gen-java/
    [hadoop@master gen-java]$ ls
    UserProfile.java  UserStorage.java
    具体代码可以查看相应的代码文件。
     
    C++代码
    生成多个C++文件:
    [hadoop@master thrift]$ cd gen-cpp/
    [hadoop@master gen-cpp]$ ls
    user_constants.cpp  UserStorage.cpp  UserStorage_server.skeleton.cpp  user_types.h
    user_constants.h    UserStorage.h    user_types.cpp
    具体代码可以查看相应的代码文件。
     
    PHP代码
    生成2个文件:
    [hadoop@master thrift]$ cd gen-php/
    [hadoop@master gen-php]$ ls
    Types.php  UserStorage.php
    具体代码可以查看相应的代码文件。
     
    Perl代码
    生成3个文件:
    [hadoop@master thrift]$ cd gen-perl/
    [hadoop@master gen-perl]$ ls
    Constants.pm  Types.pm  UserStorage.pm
    具体代码可以查看相应的代码文件
     
    C#代码
    生成2个文件:
    [hadoop@master thrift]$ cd gen-csharp/
    [hadoop@master gen-csharp]$ ls
    UserProfile.cs  UserStorage.cs
    具体代码可以查看相应的代码文件。
     
    Python代码
    生成一个__init__.py文件,和一个目录user:
    [hadoop@master thrift]$ cd gen-py/
    [hadoop@master gen-py]$ ls -R
    .:
    __init__.py  user
    ./user:
    constants.py  __init__.py  ttypes.py  UserStorage.py  UserStorage-remote
     
     
    如果想要生成其他编程语言的代码,可以参考Thrift命令支持的语言,如下所示:
    Available generators (and options):
      as3 (AS3):
        bindable:          Add [bindable] metadata to all the struct classes.
      c_glib (C, using GLib):
      cocoa (Cocoa):
        log_unexpected:  Log every time an unexpected field ID or type is encountered.
      cpp (C++):
        cob_style:       Generate "Continuation OBject"-style classes.
        no_client_completion:
                         Omit calls to completion__() in CobClient class.
        templates:       Generate templatized reader/writer methods.
        pure_enums:      Generate pure enums instead of wrapper classes.
        dense:           Generate type specifications for the dense protocol.
        include_prefix:  Use full include paths in generated files.
      csharp (C#):
        async:           Adds Async CTP support.
        wcf:             Adds bindings for WCF to generated classes.
        serial:          Add serialization support to generated classes.
      d (D):
      delphi (delphi):
        ansistr_binary:  Use AnsiString as binary properties.
      erl (Erlang):
      go (Go):
      hs (Haskell):
      html (HTML):
      java (Java):
        beans:           Members will be private, and setter methods will return void.
        private-members: Members will be private, but setter methods will return 'this' like usual.
        nocamel:         Do not use CamelCase field accessors with beans.
        hashcode:        Generate quality hashCode methods.
        android_legacy:  Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
        java5:           Generate Java 1.5 compliant code (includes android_legacy flag).
      javame (Java ME):
      js (Javascript):
        jquery:          Generate jQuery compatible code.
        node:            Generate node.js compatible code.
      ocaml (OCaml):
      perl (Perl):
      php (PHP):
        inlined:         Generate PHP inlined files
        server:          Generate PHP server stubs
        oop:             Generate PHP with object oriented subclasses
        rest:            Generate PHP REST processors
      py (Python):
        new_style:       Generate new-style classes.
        twisted:         Generate Twisted-friendly RPC services.
        utf8strings:     Encode/decode strings using utf8 in the generated code.
        slots:           Generate code using slots for instance members.
        dynamic:         Generate dynamic code, less code generated but slower.
        dynbase=CLS      Derive generated classes from class CLS instead of TBase.
        dynexc=CLS       Derive generated exceptions from CLS instead of TExceptionBase.
        dynimport='from foo.bar import CLS'
                         Add an import line to generated code to find the dynbase class.
      rb (Ruby):
        rubygems:        Add a "require 'rubygems'" line to the top of each generated file.
      st (Smalltalk):
      xsd (XSD):
    参考链接
     
  • 相关阅读:
    羅素悖論和正則公理
    「陶哲軒實分析」 習題 3.4.4
    常微分方程_阿诺尔德 1.1节,问题6 擴張相空間沿時間軸的平移變換將積分曲線變爲積分曲線
    「陶哲軒實分析」 習題 3.5.1
    常微分方程_阿诺尔德 1.1节,问题6 擴張相空間沿時間軸的平移變換將積分曲線變爲積分曲線
    「陶哲軒實分析」 習題 3.4.4
    css3的新属性 新增的颜色 透明度两种渐变定义多张背景图backgroundsize
    使用sessionStorage获取值和设置值
    PAT A1091 Acute Stroke
    杜教筛算法
  • 原文地址:https://www.cnblogs.com/chunguang/p/5662044.html
Copyright © 2011-2022 走看看