Apache Thrift 的使用

Apache Thrift

Thrift是一个跨语言的服务部署框架,最初由Facebook于2007年开发,2008年进入Apache开源项目。Thrift通过IDL(Interface Definition Language,接口定义语言)来定义RPC(Remote Procedure Call,远程过程调用)的接口和数据类型,然后通过thrift编译器生成不同语言的代码(目前支持C++,Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk和OCaml),并由生成的代码负责RPC协议层和传输层的实现。

过程:

  • Call ID 映射
  • 序列化和反序列化
  • 网络传输

何谓RPC远程过程调用?

RPC(Remote Procedure Call)远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。

示例

WhatTime.thrift文件
namespace cpp ld

struct Example{
    1:i32     number=10,
    2:i64     bigNumber,
    3:double  decimals,
    4:string  name="thrifty"
}

service TimeInterface{
    i32  GetTime(),
    void SetTime()
}

generate the source from a thrift file

thrift [-r] --gen <language> <Thrift filename>
# -r 可选

client.cpp

#include "TimeInterface.h"
#include <thrift/transport/TSocket.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;

using boost::shared_ptr;

using namespace  ::ld;

int main(int argc, char **argv) {
    int port = 9090;
    boost::shared_ptr<TSocket> socket(new TSocket("localhost", port)); //注意此处的ip和端口
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    transport->open();

    // 我们的代码写在这里
    TimeInterfaceClient client(protocol);  // 客户端接口,声明于TimeInterface.h中。
    client.SetTime();
    client.GetTime();

    transport->close();

    return 0;
}

makefile

all : server client

server : TimeInterface.cpp  TimeInterface.h  TimeInterface_server.skeleton.cpp  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h
        g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp  TimeInterface.h  TimeInterface_server.skeleton.cpp  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h -o server

client: TimeInterface.cpp  TimeInterface.h  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h client.cpp
        g++ -std=c++11 -g -Ithrift -lthrift TimeInterface.cpp  TimeInterface.h  WhatTime_constants.cpp  WhatTime_constants.h  WhatTime_types.cpp  WhatTime_types.h client.cpp -o client

原文地址:https://www.cnblogs.com/walkinginthesun/p/9559466.html

时间: 2024-07-31 00:35:15

Apache Thrift 的使用的相关文章

Apache Thrift with Java Quickstart(thrift入门及Java实例)

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

Apache Thrift 跨语言服务开发框架

Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 IDL 来定义 RPC 的接口和数据类型,然后通过代码生成工具来生成针对不同编程语言的代码,目前支持 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCa

Apache Thrift使用总结

使用感受 之前对Thrift的理解有点不准确,使用之后发现Thrift比想象中的要简单得多. Thrift做的事情就是跨语言的分布式RPC,通过编写.thrift文件声明接口类和方法,客户端调用定义的方法,Server端实现定义的接口.虽然的确RPC是需要网络请求,但不像Netty这种NIO网络编程库(还要关注很多数据传输中的细节,比如数据如何序列化.如何在字节数组里建立结构.如何在两端解析字节数组.如何处理Handler里的事件状态.如何把多个Handler按顺序串起来),Thrift掩盖了数

Apache Thrift的简单使用

Apache Thrift的简单使用 ---------------------- 1. 简介 Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架.它有一个代码生成器来对它所定义的IDL定义文件自己主动生成服务代码框架.用户仅仅要在其之前进行二次开发即可,对于底层的RPC通讯等都是透明的.眼下它支持的语言有C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCam

比较跨语言通讯框架:Apache Thrift和Google Protobuf

前两天想在微博上发表一个观点:在现在的技术体系中,能用于描述通讯协议的方式很多,xml,json,protobuf,thrift,如果在有如此众多选择的基础上,在设计系统时,还自造协议,自己设计协议类型和解析方式,那么我只能说,您真的落后了,不是技术上,而是思想上.对于xml,和json我们不做过多描述了,参考相关文档就可以了.特别是json,如今在 web系统,页游系统的前后台通讯中,应用非常广泛.本文将重点介绍两种目前在大型系统中,应用比较普遍的两种通讯框架,thrift和Protobuf,

Apache Thrift学习之二(基础及原理)

Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详细介绍 Apache Thrift 的架构.开发和部署,并且针对不同的传输协议和服务类型给出相应的 Java 实例,同时详细介绍 Thrift 异步客户端的实现,最后提出使用 Thrift 需要注意的事项. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其中

Apache Thrift 服务开发框架学习记录

Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Service,基于 JSON 消息格式的 RESTful 服务等.其中所用到的数据传输方式包括 XML,JSON 等,然而 XML 相对体积太大,传输效率低,JSON 体积较小,新颖,但还不够完善. 本文将介绍由 Facebook 开发的远程服务调用框架 Apache Thrift,它采用接口描述语言定义并创

架构设计:系统间通信(14)——RPC实例Apache Thrift 下篇(2)

(接上篇<架构设计:系统间通信(13)--RPC实例Apache Thrift 下篇(1)>) 3.正式开始编码 我已经在CSDN的资源区上传了这个示例工程的所有代码(http://download.csdn.net/detail/yinwenjie/9289999).读者可以直接到资源下载站进行下载(不收积分哦~~^_^).这篇文章将紧接上文,主要介绍这个工程几个主要的类代码. 3-1.编写服务端主程序 服务端主程序的类名:processor.MainProcessor,它负责在服务端启动A

Building Microservices with Spring Boot and Apache Thrift. Part 2. Swifty services

http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part2.html In previous article I showed you how to use pure Apache Thrift to create protocols for your services. This time I will explain how to use Facebook Swift (not the best name for project

Building Apache Thrift on CentOS 6.5

Building Apache Thrift on CentOS 6.5 Starting with a minimal installation, the following steps are required to build Apache Thrift on Centos 6.5. This example builds from source, using the current development master branch. These instructions should