`
yangyangmyself
  • 浏览: 229901 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Thrift实战准备

阅读更多
为什么用Thrift及各种数据传输方式比较
     目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等。其中所用到的数据传输方式包括 XML,JSON 等,然而 XML 相对体积太大,传输效率低,JSON 体积较小,新颖,但还不够完善。本文将介绍由 Facebook 开发的远程服务调用框架 Apache Thrift,它采用接口描述语言定义并创建服务,支持可扩展的跨语言服务开发,所包含的代码生成引擎可以在多种语言中,如 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk 等创建高效的、无缝的服务,其传输数据采用二进制格式,相对 XML 和 JSON 体积更小,对于高并发、大数据量和多语言的环境更有优势。本文将详细介绍 Thrift 的使用,并且提供丰富的实例代码加以解释说明,帮助使用者快速构建服务。


 
 

    

看看官网怎么说,Thrift经量级,可生成远程过程调用代码,提供数据传输、序列化、应用级别处理,如何编写thrift文件(IDL规范),跨语言平台;大数据框架及相关组件支持thrift,如Hadoop、Hbase等。
      Thrift is a lightweight, language-independent software stack with an associated code generation mechanism for RPC. Thrift provides clean abstractions for data transport, data serialization, and application level processing. Thrift was originally developed by Facebook and now it is open sourced as an Apache project. Apache Thrift is a set of code-generation tools that allows developers to build RPC clients and servers by just defining the data types and service interfaces in a simple definition file. Given this file as an input, code is generated to build RPC clients and servers that communicate seamlessly across programming languages.
      In this tutorial I will describe how Thrift works and provide a guide for build and installation steps, how to write thrift files and how to generate from those files the source code that can be used from different client libraries to communicate with the server. Thrift supports a variety of languages including C++, Java, Python, PHP, Ruby but for simplicity I will focus this tutorial on examples that include Java and Python.

 

 

    一、安装方法(Centos6)

 

下载及安装指导(官网)
Download Thrift: http://thrift.apache.org/download
Detailed information on how to install Thrift can be found here: http://thrift.apache.org/docs/install/
1)安装依赖
Thrift的编译器使用C++编写的,在安装编译器之前,首先应该保证操作系统基本环境支持C++的编译,安装相关依赖的软件包

    

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel 
zlib-devel python-devel ruby-devel

    

启动Java Library 需要安装Ant
/*
* 启动Java Library 需要安装Ant
*/
Install the languages with which you plan to use thrift. To use with Java for example, install a Java JDK you prefer. In this demo I am using Oracle JDK 7 for Ubuntu, but you shouldn’t have problem using the one you like.
• To use with Java you will also need to install Apache Ant
• sudo yum install ant

    

2)安装Thrift
wget http://archive.apache.org/dist/thrift/0.9.0/thrift-0.9.0.tar.gz
tar zxvf thrift-0.9.0
cd thrift-0.9.0
编译安装
./configure
make
make install 
3)Thrift安装验证
$ thrift -version
Thrift version 0.9.0
4)将thrift文件生成Java类
thrift --gen java hello.thrift

 

    hello.thrift文件,编写thrift基本知识及规范,我们下一节再讲

// namespace + 语言 + 生成包路径
namespace java com.java
/**
* 类似Java定义接口
*/
service Hello{
    string getWord(),
    void writeWold(1:string words)
}

    执行上述命令后,在com.java包下生成Hello.java,用于编写服务端及客户调用。

 

    

     二、Windows环境下搭建开发环境

 

     这里不再讲用Cgwin模拟Linux

1)Windows编译工具,用于通过Thrift生成Java类
下载后即可用,不需要按装,下载地址:http://archive.apache.org/dist/thrift/

    

thrift -r --gen java hello.thrift

    

2)Maven开发环境,将编译工具生成的Java类导入项目里,接下来可编写接口实现、服务端、客户端代码
<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.9.2</version>
</dependency>
<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.9</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.5.8</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.12</version>
</dependency>

 

    至此Thrift安装及开发环境准备了,下一节我们讲一下Thrift语法,然后以实例方案讲述开发流程

 

     参考以下网站资源:

 

           http://thrift-tutorial.readthedocs.io/en/latest/intro.html

 

           http://thrift.apache.org/tutorial/

 

  • 大小: 198.4 KB
  • 大小: 19.8 KB
  • 大小: 172.7 KB
  • 大小: 49.9 KB
  • 大小: 60.7 KB
  • 大小: 16 KB
2
1
分享到:
评论

相关推荐

    Thrift实战案例

    NULL 博文链接:https://yangyangmyself.iteye.com/blog/2318104

    thrift开发入门java程序

    Thrift作为可伸缩的跨语言服务开发框架,网上的资源较少,这里是一个简单的入门小程序,文件中的mylib下包含了依赖的jar包,并且在file目录下放了一个简单的thrift文件和生成thrift-0.9.0.exe工具,直接使用 thrift-...

    Apache Thrift Java实战源码,包含了客户端和服务端源码

    Apache Thrift Java实战源码,包含了客户端和服务端源码,客户端和服务端是分开的,如果需要放到一个工程,直接把Client.java文件复制到服务端运行即可。

    基于thrift的RPC调用实例

    RPC(Remote Procedure Call Protocol)远程过程调用协议实例,学习和使用thrift无痛入门代码。具体使用方法,可以看博客详解。

    基于Thrift实现的游戏匹配项目

    thrift实战项目,游戏匹配 game文件夹为游戏节点 match_system文件夹为匹配系统节点 thrift文件夹为thrift相关文件

    thrift源码

    thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码thrift源码...

    the programmer's guide to apache thrift

    Apache Thrift is an open source cross language serialization and RPC framework. With support for over 15 programming languages, Apache Thrift can play an important role in a range of distributed ...

    thrift安装

    thrift 安装包。

    Windows下QT使用Thrift的样例

    网上一直搜不到Windows下QT使用thrift的例子,自己整了个 QT版本 5.8.0 Boost版本 boost_1_61_0 Thrift版本 thrift-0.10.0

    使用wireshark抓取thrift协议接口调用

    使用wireshark抓取thrift协议接口调用

    thrift官方代码+与dubbo集成支持原生thrift协议

    thrift官方代码+与dubbo集成支持原生thrift协议

    thrift-0.9.2.tar.gz

    thrift,Apache Thrift 0.9.2 版本,解压后直接直接安装,可伸缩的跨语言服务开发框架,命令: 解压命令:tar -zxf thrift-0.9.2.tar.gz 安装命令:./configure --with-lua=no && make && make install 查看版本:...

    Thrift之C++初体验

    thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, ...

    thrift-编译工具

    thrift-编译工具

    Thrift入门简介

    通俗简单的介绍了什么是thrift,适用于thrift或RPC扫盲。

    unity3d thrift twisted

    unity3d-thrift-twistedunity3d-thrift-twistedunity3d-thrift-twisted

    Learning.Apache.Thrift.178588274

    Make applications cross-communicate using Apache Thrift! About This Book Leverage Apache Thrift to enable applications written in different programming languages (Java, C++, Python, PHP, Ruby, and so...

    thrift入门

    thrift基础入门,讲解thrift基本语法

    thrift-0.9.1.exe和thrift-0.9.2.exe

    thrift文件生成工具thrift-0.9.1.exe和thrift-0.9.2.exe压缩包

    thrift-Demo

    thrift一个实例

Global site tag (gtag.js) - Google Analytics