Apache的开源项目thrift概述

一、作用

Thrift("Scalable Cross-Language Services Implementation”)最早是Facebook的项目,后来Facebook提供给Apache作为开源项目。

一般情况下的跨机器的通信框架都是跨软件平台的(Linux,windows), 而Thrift最特别之处在于它是跨语言的:例如,你可以用几乎所有流行语言(C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript等等)来实现通讯过程,这样做的好处就是你不用为编程语言发愁,如果服务器端与客户端都需要编写,选择你最拿手或项目规定的语言,就可以生成一个通讯框架;如果编写一个服务器端程序,定义好通讯规则(在Thrift中是.thrift文件)后,你所采用的服务器端实现语言不会影响到客户端,以后使用的人可以采用其他编程语言来实现客户端。

二、Thrift组成

Thrift其实应分成三个部分,一个叫做Thrift代码生成器,一个叫做Thrift应用框架(库),最后一个是它生成的代码。Thrift应用的基本流程如下图所示。

从上图,要生成一个Thrift应用,需用以下文件:

  1. 一个.thrift文件:该文件是通信接口的定义,最主要的是信息流的格式。
  2. 编程语言:这个无需解释。
  3. Thrift代码生成器(Thrift compiler,翻译成代码生成器似乎更合适):这个东西是安装thrift过程中生成的,它可以产生若干符合你约定通信格式的代码。
  4. Thrift应用框架库:这个东西也是在安装过程中产生的。
  5. 其他第三方支撑库:对C++来说,最主要是boost.thread、libevent,log4cxx等,按照运行的模式,生成的代码中可能需用调用这些库。

三、Thrift的安装及使用

1、安装

安装版本为:Thrift v0.9.1

系统版本:Ubuntu 14.04 64位

基本安装环境:

  • g++ 4.2
  • boost 1.53.0
  • libssl-dev

Thrift的编译器即代码生成器是由C++编写的,所以需要g++来进行编译安装,且Thrift源码中用到了boost库中相关实现,例如shared_ptr,所以要事先安装boost库。

Thrift通信过程中使用ssl对数据进行安全包含,如果为安装该库,会在configure时出现 configure: error: "Error: libcrypto required."

Thrift提供了,TThreadSever, TThreadPoolServer, TNonblockingServer四种服务器框架,TSimpleServer以单一主线程阻塞的方式进行事件处理,TThreadPoolServer以多线程阻塞的方式提供服务,TNonblockingServer以多线程非阻塞方式工作。 TNonblockingServer服务模型的使用需要事先安装
libevent,libevent-dev 库,libevent是异步事件处理的程序库,其包含我们常用的poll,select,epoll等异步处理函数。

安装步骤:

$./configure
$make
#sudo make install

configure的结果最后一部分如下,其中 Build TNonblockingServer .. : yes
的结果对于使用异步的服务器模型是必须的。

[email protected]:~/download/thrift-0.9.1$./configure
......
thrift 0.9.1

Building C++ Library ......... : yes
Building C (GLib) Library .... : no
Building Java Library ........ : no
Building C# Library .......... : no
Building Python Library ...... : yes
Building Ruby Library ........ : no
Building Haskell Library ..... : no
Building Perl Library ........ : no
Building PHP Library ......... : no
Building Erlang Library ...... : no
Building Go Library .......... : no
Building D Library ........... : no

C++ Library:
  Build TZlibTransport ...... : yes
  Build TNonblockingServer .. : yes
  Build TQTcpServer (Qt) .... : no

Python Library:
  Using Python .............. : /usr/bin/python

If something is missing that you think should be present,
please skim the output of configure to find the missing
component.  Details are present in config.log.

在本人电脑上make的时候会出现下面的bug,

ar: .libs/ThriftTest_constants.o: No such file or directory

不知道Makefile如何生成的,导致上面这个编译文件路径有问题,解决方法有下面两种:

method1:
解决的方法时直接从Github(git://git.apache.org/thrift.git)上git clone 源码,先运行./bootstrap.sh,在按照configure安装。

method2:
可以将已经编译的test/cpp/*.o复制到test/cpp/.libs后,继续编译就可以了
cp test/cpp/*.o test/cpp/.libs/

2、使用

1. thrift生成代码

创建Thrift文件:G:\test\thrift\demoHello.thrift ,内容如下:

1 namespace java com.micmiu.thrift.demo
2  
3 service  HelloWorldService {
4   string sayHello(1:string username)
5 }

目录结构如下:

G:\test\thrift>tree /F
卷 other 的文件夹 PATH 列表
卷序列号为 D238-BE47
G:.
    demoHello.thrift
    demouser.thrift
    thrift-0.8.0.exe

没有子文件夹

thrift-0.8.0.exe 是官网提供的windows下编译工具,运用这个工具生成相关代码:

1 thrift-0.8.0.exe -r -gen java ./demoHello.thrift

生成后的目录结构如下:

G:\test\thrift>tree /F
卷 other 的文件夹 PATH 列表
卷序列号为 D238-BE47
G:.
│  demoHello.thrift
│  demouser.thrift
│  thrift-0.8.0.exe
│
└─gen-java
    └─com
        └─micmiu
            └─thrift
                └─demo
                        HelloWorldService.java

2. 实现接口Iface

java代码:HelloWorldImpl.java

1 package com.micmiu.thrift.demo;
2  
3 import org.apache.thrift.TException;
4  
5 /**
6  * blog
http://www.micmiu.com
7  *
8  * @author Michael
9  *
10  */
11 public class
HelloWorldImpl implements
HelloWorldService.Iface {
12  
13     public
HelloWorldImpl() {
14     }
15  
16     @Override
17     public
String sayHello(String username) throws
TException {
18         return
"Hi," + username + " welcome to my blog www.micmiu.com";
19     }
20  
21 }

3.TSimpleServer服务端

简单的单线程服务模型,一般用于测试。

编写服务端server代码:HelloServerDemo.java

1 package com.micmiu.thrift.demo;
2  
3 import org.apache.thrift.TProcessor;
4 import org.apache.thrift.protocol.TBinaryProtocol;
5 import org.apache.thrift.protocol.TCompactProtocol;
6 import org.apache.thrift.protocol.TJSONProtocol;
7 import org.apache.thrift.protocol.TSimpleJSONProtocol;
8 import org.apache.thrift.server.TServer;
9 import org.apache.thrift.server.TSimpleServer;
10 import org.apache.thrift.transport.TServerSocket;
11  
12 /**
13  * blog
http://www.micmiu.com
14  *
15  * @author Michael
16  *
17  */
18 public class
HelloServerDemo {
19     public
static final
int
SERVER_PORT = 8090;
20  
21     public
void startServer() {
22         try
{
23             System.out.println("HelloWorld TSimpleServer start ....");
24  
25             TProcessor tprocessor =
new HelloWorldService.Processor<HelloWorldService.Iface>(
26                     new
HelloWorldImpl());
27             // HelloWorldService.Processor<HelloWorldService.Iface> tprocessor =
28             // new HelloWorldService.Processor<HelloWorldService.Iface>(
29             // new HelloWorldImpl());
30  
31             // 简单的单线程服务模型,一般用于测试
32             TServerSocket serverTransport =
new TServerSocket(SERVER_PORT);
33             TServer.Args tArgs =
new TServer.Args(serverTransport);
34             tArgs.processor(tprocessor);
35             tArgs.protocolFactory(new
TBinaryProtocol.Factory());
36             // tArgs.protocolFactory(new TCompactProtocol.Factory());
37             // tArgs.protocolFactory(new TJSONProtocol.Factory());
38             TServer server =
new TSimpleServer(tArgs);
39             server.serve();
40  
41         }
catch (Exception e) {
42             System.out.println("Server start error!!!");
43             e.printStackTrace();
44         }
45     }
46  
47     /**
48      * @param args
49      */
50     public
static void
main(String[] args) {
51         HelloServerDemo server =
new HelloServerDemo();
52         server.startServer();
53     }
54  
55 }

编写客户端Client代码:HelloClientDemo.java

1 package com.micmiu.thrift.demo;
2  
3 import org.apache.thrift.TException;
4 import org.apache.thrift.protocol.TBinaryProtocol;
5 import org.apache.thrift.protocol.TCompactProtocol;
6 import org.apache.thrift.protocol.TJSONProtocol;
7 import org.apache.thrift.protocol.TProtocol;
8 import org.apache.thrift.transport.TSocket;
9 import org.apache.thrift.transport.TTransport;
10 import org.apache.thrift.transport.TTransportException;
11  
12 /**
13  * blog
http://www.micmiu.com
14  *
15  * @author Michael
16  *
17  */
18 public class
HelloClientDemo {
19  
20     public
static final
String SERVER_IP =
"localhost";
21     public
static final
int
SERVER_PORT = 8090;
22     public
static final
int
TIMEOUT = 30000;
23  
24     /**
25      *
26      * @param userName
27      */
28     public
void startClient(String userName) {
29         TTransport transport =
null;
30         try
{
31             transport =
new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
32             // 协议要和服务端一致
33             TProtocol protocol =
new TBinaryProtocol(transport);
34             // TProtocol protocol = new TCompactProtocol(transport);
35             // TProtocol protocol = new TJSONProtocol(transport);
36             HelloWorldService.Client client =
new HelloWorldService.Client(
37                     protocol);
38             transport.open();
39             String result = client.sayHello(userName);
40             System.out.println("Thrify client result =: "
+ result);
41         }
catch (TTransportException e) {
42             e.printStackTrace();
43         }
catch (TException e) {
44             e.printStackTrace();
45         }
finally {
46             if
(null
!= transport) {
47                 transport.close();
48             }
49         }
50     }
51  
52     /**
53      * @param args
54      */
55     public
static void
main(String[] args) {
56         HelloClientDemo client =
new HelloClientDemo();
57         client.startClient("Michael");
58  
59     }
60  
61 }

先运行服务端程序,日志如下:

HelloWorld TSimpleServer start ....

再运行客户端调用程序,日志如下:

Thrify client result =: Hi,Michael welcome to my blog www.micmiu.com

测试成功,和预期的返回信息一致。

参考: http://blog.csdn.net/guxch/article/details/12157151

http://blog.csdn.net/anonymalias/article/details/26154405

http://www.micmiu.com/soa/rpc/thrift-sample/

时间: 2024-08-03 02:57:14

Apache的开源项目thrift概述的相关文章

apache开源项目--thrift

Thrift 是一个软件框架(远程过程调用框架),用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引 擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的.高效的服务. thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器,现在是 Apa

Apache孵化器主席Justin Mclean:如何成为Apache顶级开源项目

摘要: 近日,Apache孵化器主席.Apache基金会成员.Dubbo & RocketMQ等开源项目的导师Justin Mclean来到阿里巴巴西溪园区,与众多开发者分享了如何打造一个Apache顶级项目,以及项目孵化过程会遇到的一些盲点和挑战. 近日,Apache孵化器主席.Apache基金会成员.Dubbo & RocketMQ等开源项目的导师Justin Mclean来到阿里巴巴西溪园区,与众多开发者分享了如何打造一个Apache顶级项目,以及项目孵化过程会遇到的一些盲点和挑战.

【转】apache开源项目的介绍

Jakarta项目是ASF(The Apache Software Foundation)的一部分.ASF是一个非赢利组织,她鼓励基于开放的软件许可下进行合作.注重实效的开发,并提供各个领域的高质量软件,她涉及到Http服务器,编译工具,类库,开发架构,服务器端Java技术,J2EE容器,数据库工具,日志工具,XML解析等等诸多领域.ASF提供的java项目有一部分在Jakarta中,还有一些成为独立的诸如Tomcat的项目,Jakarta项目则提供了多种多样开源的java解决通用方案. 先介绍

www808888webcom基金的一个开源项目19908836661也是apache基金的一个开源

Flume也是apache基金的一个开源项目,由cloudera公司开发的一款分布式.高可靠.高可用的日志传输工具.其以agent为一个单位,agent由source.channel和sink构成.一个agent最少由一个source.channel和sink构成,数量可以自由组合 Source主要用来收集源数据,并对源数据进行反序列化.Source在读取数据文件时,会遍历日志文件中的每一行,并把这一行封装在一个event当中,一个event包含一个header和一个body,header是一个

Linux开源模块移植概述暨交叉编译跨平台移植总结--摘自《嵌入式Linux驱动模板精讲与项目实践》

本文摘自<嵌入式Linux驱动模板精讲与项目实践>一书中的"开发与调试技巧". Linux的强大威力就在于有很多开源项目可以使用,通常很多需求可以通过寻找相关的开源模块做为快速解决方案.要把这些开源模块应用到嵌入式中,其中一个关键点就是要使用交叉编译工具对开源项目进行交叉编译. 根据具体情况,下载的开源项目在组织上有很多情况,在此对各种情况进行归类介绍. 1. 下载的开源软件包找不到Makefile 对于这种开源包通常是采用configure的方式组织的,那么第一步就是使用

apache开源项目--Apache Drill

为了帮助企业用户寻找更为有效.加快Hadoop数据查询的方法,Apache 软件基金会发起了一项名为“Drill”的开源项目.Apache Drill 实现了 Google's Dremel. Apache Drill 在基于 SQL 的数据分析和商业智能(BI)上引入了 JSON 文件模型,这使得用户能查询固定架构,演化架构,以及各种格式和数据存储中的模式无关(schema-free)数据.该体系架构中关系查询引擎和数据库的构建是有先决条件的,即假设所有数据都有一个简单的静态架构. Apach

apache开源项目--nutch

Nutch 是一个开源Java 实现的搜索引擎.它提供了我们运行自己的搜索引擎所需的全部工具.包括全文搜索和Web爬虫. Nutch的创始人是Doug Cutting,他同时也是Lucene.Hadoop和Avro开源项目的创始人. Nutch诞生于2002年8月,是Apache旗下的一个用Java实现的开源搜索引擎项目,自Nutch1.2版本之后,Nutch已经从搜索引擎演化为网络爬虫,接着Nutch进一步演化为两大分支版本:1.X和2.X,这两大分支最大的区别在于2.X对底层的数据存储进行了

apache开源项目--CouchDB

Apache CouchDB 是一个面向文档的数据库管理系统.它提供以 JSON 作为数据格式的 REST 接口来对其进行操作,并可以通过视图来操纵文档的组织和呈现. CouchDB 是 Apache 基金会的顶级开源项目. CouchDB落实到最底层的数据结构就是两类B+Tree . 与现在流行的关系数据库服务器不同,CouchDB 是围绕一系列语义上自包含的文档而组织的. CouchDB 中的文档是没有模式的(schema free),也就是说并不要求文档具有某种特定的结构. CouchDB

15个非常重要的Apache开源项目汇总

15个非常重要的Apache开源项目汇总 自1999年创立以来,Apache软件基金会如今已成了众多重要的开源软件项目之家.本文列举了15个多年来非常重要的Apache项目,这些项目不仅对开源运动来说非常重要,对于与一般的技术世界来说也是非常重要的. 虽然Apache没有去维护有关下载量的完备统计数字,但是像Apache HTTP Server,已成为全球将近5亿多个网站的引擎,再比如OpenOffice,虽然只是前不久才进入Apache的项目库,但也已经被下载了数百万次.Apache还提供更为