Apache Thrift使用总结
使用感受
之前对Thrift的理解有点不准确,使用之后发现Thrift比想象中的要简单得多。
Thrift做的事情就是跨语言的分布式RPC,通过编写.thrift文件声明接口类和方法,client调用定义的方法,Server端实现定义的接口。尽管的确RPC是须要网络请求,但不像Netty这样的NIO网络编程库(还要关注许多传输数据中的细节,比方数据怎样序列化、怎样在字节数组里建立结构、怎样在两端解析字节数组、怎样处理Handler里的事件状态、怎样把多个Handler按顺序串起来),Thrift掩盖了传输数据这件事情,开发人员使用的时候就是纯纯的RPC的使用感受。
基本使用
Thrift使用起来差点儿没有不论什么门槛,能够看这篇HelloWorld的文章,尽管有点老,可是看完之后基本使用起来没有不论什么障碍了。
官方给出的这个样例更加全面些,全面在.thrift文件中能够声明的东西列的更全些。
以下看看两个.thrift的定义:
shared.thrift
/**
* This Thrift file can be included by other Thrift files that want to share
* these definitions.
*/ namespace java com.baidu.mordor.sink.service struct SharedStruct {
1: i32 key
2: string value
} service SharedService {
SharedStruct getStruct(1: i32 key)
}
tutorial.thrift
/**
* The first thing to know about are types. The available types in Thrift are:
*
* bool Boolean, one byte
* byte Signed byte
* i16 Signed 16-bit integer
* i32 Signed 32-bit integer
* i64 Signed 64-bit integer
* double 64-bit floating point value
* string String
* binary Blob (byte array)
* map<t1,t2> Map from one type to another
* list<t1> Ordered list of one type
* set<t1> Set of unique elements of one type
*
* Did you also notice that Thrift supports C style comments?
*/ // Just in case you were wondering... yes. We support simple C comments too. /**
* Thrift files can reference other Thrift files to include common struct
* and service definitions. These are found using the current path, or by
* searching relative to any paths specified with the -I compiler flag.
*
* Included objects are accessed using the name of the .thrift file as a
* prefix. i.e. shared.SharedObject
*/
include "shared.thrift" /**
* Thrift files can namespace, package, or prefix their output in various
* target languages.
*/
namespace java com.baidu.mordor.sink.service /**
* Thrift lets you do typedefs to get pretty names for your types. Standard
* C style here.
*/
typedef i32 MyInteger /**
* Thrift also lets you define constants for use across languages. Complex
* types and structs are specified using JSON notation.
*/
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'} /**
* You can define enums, which are just 32 bit integers. Values are optional
* and start at 1 if not supplied, C style again.
*/
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
} /**
* Structs are the basic complex data structures. They are comprised of fields
* which each have an integer identifier, a type, a symbolic name, and an
* optional default value.
*
* Fields can be declared "optional", which ensures they will not be included
* in the serialized output if they aren't set. Note that this requires some
* manual management in some languages.
*/
struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: optional string comment,
} /**
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
1: i32 what,
2: string why
} /**
* Ahh, now onto the cool part, defining a service. Services just need a name
* and can optionally inherit from another service using the extends keyword.
*/
service Calculator extends shared.SharedService { void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), /**
* This method has a oneway modifier. That means the client only makes
* a request and does not listen for any response at all. Oneway methods
* must be void.
*/
oneway void zip() }
Thrift通过IDL(接口定义语言),在.thrift文件中声明接口类和方法,声明struct结构、const、Exception等,还能够include别的.thrift文件,这套语法与C很类似。通过编写IDL和generate代码,做到了不同语言之间的RPC,client实现接口类和使用结构类的时候很easy好用。
上面样例的代码能够从官方下载到,能够放到本地看一下他的使用,很easy。
Thrift重要组件
Thrift API里三个重要组成部分:Protocal,Transport,Server。
Protocal定义了消息怎样序列化。常见的是TBinaryProtocol,TJSONProtocol,TCompactProtocol。
Transport定义了消息在client和服务端怎样通信。常见的是TSocket,TFramedTransport,TNonblockingTransport等。
Server从transport端接收序列化后的消息,依据protocal反序列化回来,然后调用用户实现的消息handler(接口实现类),最后把返回的数据序列化后再传回给client。常见的TServer为TSimpleServer,THsHaServer,TThreadPoolServer,TNonBlockingServer,TThreadedSelectorServer。以下会详细介绍各个Server的特点,开发人员须要选择适合自己场景的一套 Server+相应的Transport+相应的Protocol。
TServer说明
Thrift实现的几种不同的TServer。对于Java而言,版本号按0.9.0为准:
TSimpleServer在sever端仅仅有一个I/O堵塞的单线程,每次仅仅接受并服务一个client,适合測试使用,不能用于线上服务。
TNonblockingServer改动了TSimpleServer里堵塞的缺点,借助NIO里的Selector实现非堵塞I/O,同意多个client连接而且client能够使用select()选择。可是处理消息和select()的是同一个线程,当有大量client连接的时候,性能是不理想的。
THsHaServer(半同步半异步server)在以上基础上,使用一个单独线程来处理网络I/O,一个worker线程池来处理消息。优点是仅仅要有空暇worker线程,消息能够被及时、并行处理,吞吐量会大一些。
TThreadedSelectorServer,与THsHaServer的差别是处理网络I/O也是多线程了,它维护两个线程池,一个负责网络I/O,一个负责数据处理。优点是当网络I/O是瓶颈的情况下,性能比THsHaServer更好。
TThreadPoolServer有一个专用的线程来接收connections,连接被建立后,会从ThreadPoolExecutor里取一个工作线程来负责这次连接,直到连接断开后线程回到线程池里,且线程池大小可配。也就是说,并发性的大小可依据服务器设定,假设不介意开许多线程的话,TThreadPoolServer是个还不错的选择。
全文完 :)
Apache Thrift使用总结的更多相关文章
- Apache thrift RPC 双向通信
在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息.而客户端却得不到服务器的响应. 在不涉及 ...
- Apache Thrift 跨语言服务开发框架
Apache Thrift 是一种支持多种编程语言的远程服务调用框架,由 Facebook 于 2007 年开发,并于 2008 年进入 Apache 开源项目管理.Apache Thrift 通过 ...
- Apache Thrift 环境配置
在 Ubuntu 14.04 下Apache Thrift 的安装方法: 1安装依赖包 sudo apt-get install libboost-dev libboost-test-dev libb ...
- Apache Thrift 服务开发框架学习记录
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架. 前言: 目前流行的服务调用方式有很多种,例如基于 SOAP 消息格式的 Web Servic ...
- Apache Thrift
Baidu Thrift Google Thrift Apache Thrift - 可伸缩的跨语言服务开发框架
- Apache Thrift - 可伸缩的跨语言服务开发框架
To put it simply, Apache Thrift is a binary communication protocol 原文地址:http://www.ibm.com/developer ...
- Apache Thrift学习之二(基础及原理)
Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详细介绍 Apache Thrift 的架构.开发和部署,并且 ...
- Apache Thrift学习之一(入门及Java实例演示)
目录: 概述 下载配置 基本概念 数据类型 服务端编码基本步骤 客户端编码基本步骤 数据传输协议 实例演示(java) thrift生成代码 实现接口Iface TSimpleServer服务模型 T ...
- Apache Thrift入门(安装、测试与java程序编写)
安装Apache Thrift ubuntu linux运行: #!/bin/bash #下载 wget http://mirrors.cnnic.cn/apache/thrift/0.9.1/thr ...
- Apache Thrift的简单使用
Apache Thrift的简单使用 ---------------------- 1. 简介 Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架.它有一个代码生成器来对它所 ...
随机推荐
- iOS - 设置导航栏之标题栏居中、标题栏的背景颜色
本章实现效果: Untitled.gif 前言: 项目中很多需求是要求自定义标题栏居中的,本人最近就遇到这中需求,如果用系统自带的titleView设置的话,不会居中,经过尝试,发现titleview ...
- [学习笔记]BS架构与CS架构
整理自:http://www.iteye.com/problems/102411 前两天面试的时候被问到这个问题,没有回答上来因此在这里学习整理一下. B/S架构 B/S架构的全称为Browser/S ...
- 查询目标的ip地址和详细地理信息(多种方法)
不多说,直接上干货! 至于这里怎FQ,很简单,请见我下面的博客! kali 2.0安装 lantern(成功FQ) shadowsocks(简称SSFQ软件)步骤详解 FQ软件lantern-inst ...
- 【Git 四】一款不错的 Git 客户端
平常做开发使用 git bash 进行代码提交,一直没有使用过 git 相关的客户端. 直到有次同一分支下两个日志进行代码比较时,bash 返回的结果可视化理解起来比较差. 如果更改的部分比较多,问题 ...
- linux上将另一个文件内容快速写入正在编辑的文件内
一.我们看到 www 目录下有两个文件.like.php 内有一行字母,而 loo.php 内什么也没有. 二 .我们来编辑 loo.php. 三.用下面的命令将 like.php 的内容复制到 lo ...
- python ORM理解、元类
元类 参考链接:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143191 ...
- Unity Shader (四)顶点程序示例
1.在顶点函数中实现凸起效果 Shader "Custom/Example" { properties { _R(,))= //圆的半径,也是凸起的范围 _OX(,))= //x轴 ...
- IDEA修改当前工程jdk版本
1.ctrl+shift+alt+s 2.根据实际情况修改jdk版本
- 关于Windows7下创建Cocos2D-X项目的小问题
"新版的Cocos2D-X"已经不支持用上述脚本来创建工程了,而是改为用create-project.py来创建...命令格式: python create-project.py ...
- Codeforces Educational Codeforces Round 8 A. Tennis Tournament
大致题意: 网球比赛,n个參赛者,每场比赛每位选手b瓶水+裁判1瓶水,所有比赛每一个參赛者p条毛巾 每一轮比赛有2^k个人參加比赛(k为2^k<=n中k的最大值),下一轮晋级人数是本轮每场比赛的 ...