欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Thrift实战准备

程序员文章站 2022-07-12 16:18:37
...
为什么用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 thriftsoapjsonjava 
Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
 
 

    

看看官网怎么说,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.
Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
 

 

    一、安装方法(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/

 

  • Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
  • 大小: 198.4 KB
  • Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
  • 大小: 19.8 KB
  • Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
  • 大小: 172.7 KB
  • Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
  • 大小: 49.9 KB
  • Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
  • 大小: 60.7 KB
  • Thrift实战准备
            
    
    博客分类: Thrift thriftsoapjsonjava 
  • 大小: 16 KB