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的软件包,并解压缩:
wget http://archive.apache.org/dist/thrift/0.9.3/thrift-0.9.3.tar.gz
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):
参考链接
 
http://thrift.apache.org/
http://thrift.apache.org/docs/install/
http://thrift.apache.org/docs/install/centos/
http://thrift.apache.org/docs/BuildingFromSource/

Thrift CentOS安装和使用相关推荐

  1. CentOS 安装docker.ce报错提示containerd.io >= 1.2.2-3问题

    centos安装docker.ce遇到报错,提示如下 # yum install -y docker-ce Last metadata expiration check: 0:01:49 ago on ...

  2. CentOS安装crontab

    CentOS安装crontab: yum install crontabs 说明: service crond start //启动服务 service crond stop //关闭服务 servi ...

  3. CentOS7:Thrift的安装

    使用Thrift需要进行安装,主要安装两个工具: 接口定义文件(.thrift)的编译器 不同语言的公共基础库程序 1 安装依赖工具和库 yum install automake libtool fl ...

  4. CentOS 安装Apache

    # centOS 安装A M P 环境 [参考简书作者,非常感谢!!!](https://www.jianshu.com/p/bc14ff0ab1c7) ## 一 Apache 环境安装 1 安装Ap ...

  5. centos 安装 NTFS支持

    2019独角兽企业重金招聘Python工程师标准>>> 参考的原文网址: centos安装完之后,默认是不支持NTFS磁盘格式的,解决的方法之一就是安装NTFS-3G模块,但是默认的 ...

  6. centos安装及网络配置

    感谢老师传授,共同学习!谢谢!仅供自己日后复习之用! centos安装关键点: 创建分区: / 系统分区 /boot 启动分区 SWAP 交换分区,虚拟内存.主要是缓解物理内存不足. 虚拟化软件: V ...

  7. centos安装tensorFlow的java环境

    参考问题汇总 centos安装tensorFlow版本的时候会遇到的一些问题,参考这个链接:https://blog.csdn.net/luoyexuge/article/details/783212 ...

  8. centos 安装 svn

    centos 安装svn服务 1. # yum install subversion 2.然后检查下安装的版本号 因为版本号不同可能会出现不同的情况 版本 信息 [root@VM_137_37_cen ...

  9. Linux(CentOS)安装分区方案

    为什么80%的码农都做不了架构师?>>>    Linux(CentOS)安装分区方案 /boot(不是必须的):/boot分区用于引导系统,它包含了操作系统的内核和在启动系统过程中 ...

最新文章

  1. 【Postman】6 Postman 发送post请求-Json格式
  2. php多个域名301重定向到主域名代码,Nginx 301和apache重定向域名规则方法(多个域名,单个域名)...
  3. 我的网站搭建 (第十七天) celery 定时刷新缓存
  4. 德约科维奇横扫纳达尔夺冠 加冕澳网七冠王
  5. vue设置多选框默认勾选_vue中复选框怎么默认全选,至少选择4个才可以点击下一步...
  6. jieba结巴分词--关键词抽取_初学者 | 知否?知否?一文学会Jieba使用方法
  7. 梦的解析 —— 梦知道答案
  8. java飞机大战小游戏
  9. 前端进阶之——CSS背景、字体和文本样式
  10. windows动态库和静态库
  11. 通过 百度网盘 分享文件
  12. 小米5测试机软件,小米手机硬件检测软件
  13. ubuntu系统开机显示BusyBox v1.22.1(Ubuntu 1:1.22.0-15ubuntu1) built-in shell(ash) 问题的解决办法
  14. 推荐系统——Evaluation criteria(评估准则)
  15. html中repeat的作用,html中hover和no-repeat
  16. HCNA之华为ensp基本操作总结
  17. .9图片设置不拉伸的区域被拉伸的问题
  18. Windows下JMC8.1.0以上版本启动失败
  19. 如何使用CMD重置Windows 10中的网络设置
  20. 分享QQ空间营销秘籍

热门文章

  1. 最近很火的桌面小猫代打器 人性化UI设置界面
  2. c 控制mysql数据导出_MySql数据库导入导出的三种方式
  3. mysql内部参数是什么意思_mysql参数及解释
  4. Java快速入门学习笔记2 | Java语言中的基本类型
  5. android缓存的后台进程,Android应用程序进程生命周期详解
  6. 相对路径和绝对路径概念
  7. 付费社群聊天小程序V1.4.5+前端
  8. MooTools教程(3):数组管理DOM元素
  9. 如何在Ubuntu 16.04中创建GIF动图
  10. Magento事件与事件监听