1.Introduce
Thrift is a software library and set of code-generation tools developed at Facebook to expedite development and implementation of efficient and scalable backend services.
Its primary goal is to enable efficient and reliable communication across programming languages by abstracting the portions of each language that tend to require the most customization into a common library that is implemented in each language.
Specifically, Thrift allows developers to define data types and service interfaces in a single language-neutral file and generate all the necessary code to build RPC clients and servers.
A transparent, high-performance bridge across many programming languages
RPC service framework
C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml
Thrift was open sourced in April 2007 and entered the Apache Incubator in May, 2008.
2.Types
It also does not require that the developer write any code for object serialization or transport.
Thrift
IDL (Interface Definition Language)
The base types supported by Thrift are:
bool A boolean value, true or false
byte A signed byte
i16 A 16-bit signed integer
i32 A 32-bit signed integer
i64 A 64-bit signed integer
double A 64-bit floating point number
string An encoding-agnostic text or binary string
Structs
struct Example {
1:i32 number=10,
2:i64 bigNumber,
3:double decimals,
4:string name="thrifty"
}
Containers
list<type>
set<type>
map<type1,type2>
Exceptions
Services
service StringCache {
void set(1:i32 key, 2:string value),
string get(1:i32 key) throws (1:KeyNotFound knf),
void delete(1:i32 key)
}
3.Transport
3.1Interface
TTransport
open Opens the tranpsort
close Closes the tranport
isOpen Indicates whether the transport is open
read Reads from the transport
write Writes to the transport
flush Forces any pending writes
TServerTransport
open Opens the transport
listen Begins listening for connections
accept Returns a new client transport
close Closes the transport
3.2Implementation
Tsocket
provides a common, simple interface to a TCP/IP stream socket.
TFileTransport
an abstraction of an on-disk file to a datastream.
Utilities
TBufferedTransport
TFramedTransport
TMemoryBuffer
4.Protocol
The Thrift protocol is self-delimiting without any framing and regardless of the encoding format.
Implementation
a space-efficient binary protocol which is used by most backend services
Essentially, it writes all data in a flat binary format. Integer types are converted to network byte order, strings are prepended with their byte length,and all message and field headers are written using the primitive integer serialization constructs. String names for fields are omitted when using generated code, field identifiers are sufficient.
5.Versioning
critical to enable staged rollouts of changes to deployed services
Versioning in Thrift is implemented via field identifiers.
struct Example {
1:i32 number=10,
2:i64 bigNumber,
3:double decimals,
4:string name="thrifty"
}
Isset
When an unexpected field is encountered, it can be safely ignored and discarded. When an expected field is not found, there must be some way to signal to the developer that it was not present.
Case Analysis
Protocol/Transport Versioning
6.Processors
6.1RPC Implementation
Tprocessor
interface TProcessor {
bool process(TProtocol in, TProtocol out)
throws TException
}
Generated Code
all the logic to handle RPC invocations via the process() call
TServer
Implementation Details
7.Facebook Thrift Services
Search
Logging
Mobile
Ads
8.Similar Systems
SOAP
XML-based. Designed for web services via HTTP, excessive XML parsing overhead.
CORBA Relatively comprehensive, debatably overdesigned and heavyweight. Comparably cumbersome software installation.
COM
Embraced mainly in Windows client softare. Not an entirely open solution.
Pillar
Lightweight and high-performance, but missing versioning and abstraction.
Protocol Buffers
Closed-source, owned by Google. Described in Sawzall paper.
9.Practise
thriftTest.thrift
namespace java com.leign.thrift.javabean
struct DataObject
{
1:i64 id,
2:string other,
}
exception ThriftTestException{
1: required string why
}
service ThriftTest{
string getNameById(1: i64 id) throws (1:ThriftTestException tte),
list<i64> getList(1: string dbName, 2: i64 key) throws (1:ThriftTestException tte),
list<DataObject> getObjList(1: string dbName, 2: i64 key) throws (1:ThriftTestException tte),
bool putList(1: string dbName, 2: i64 key, 3: list<i64> value) throws (1:ThriftTestException tte),
}
通过thrift工具在命令行生成java代码
thrift-0.5.0.exe --gen java thriftTest.thrift
pause
同目录下产生gen-java包,里面包括了上面thrift IDL定好的接口的通信、序列化等实现,我们需要做的就是具体的实现我们自己定义的接口的具体业务逻辑。
实现类ThriftTestImpl实现了ThriftTest中Iface接口(我们定义的接口)
然后就是用起来
Server端代码
/**
* @author leign
*
* 2011-5-23
*/
public class ThriftTestServer {
static final int port = 8011;
public static void main(String[] args){
try {
TServerSocket serverTransport = new TServerSocket(port);
ThriftTest.Processor processor = new ThriftTest.Processor(new ThriftTestImpl());
Factory protFactory = new TBinaryProtocol.Factory(true, true);
TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
server.serve();
} catch (TTransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("thrift test server started...");
}
}
Client端代码
/**
* @author leign
*
* 2011-5-23
*/
public class ThriftTestClient {
static final int port = 8011;
public static void main(String[] args){
try {
TTransport transport = new TSocket("localhost", port);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
try {
Client client = new Client(protocol);
String result1 = client.getNameById(123L);
String result2 = client.getNameById(124L);
System.out.println("result1==="+result1);
System.out.println("result2==="+result2);
} catch (ThriftTestException e) {
e.printStackTrace();
}
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
}
DEMO源代码和thrift工具见附件
文件上传有些问题,后面再补上
Apache Thrift官网
http://thrift.apache.org/
- 大小: 38.3 KB
分享到:
相关推荐
主要是对thrift0.9.0 TSimpleServer、TThreadPoolServer 、TNonblockingServer、THsHaServer等服务模型实例和AsynClient 异步客户端实例代码的演示
这个是java service对thrift的调用,thrift客户端生成,一个单独的例子
本设计源码提供了一个基于Scala的Spark Thrift Server。...文件类型包括3539个...该系统适合用于学习和实践Scala、Java、Python、Shell、JavaScript、CSS、HTML、Ruby和C技术,以及开发Spark Thrift Server相关的系统。
这个是对应thrift的单点登录内容,需要在user-edge-service先写redis key为token的set缓存内容,再在user-edge-service里Ctrl写authentication接口,不然不成功哦。
节俭聊天会员13512014穆罕默德·亚菲13512066卡尔文·萨德瓦(Calvin Sadewa) 使用Apache Thrift的简单聊天应用程序。先决条件安装MongoDB。 确保mongod在27017端口上运行安装Java JDK 1.8 安装Gradle,将其添加到...
17_Protobuf使用最佳实践与Apache Thrift介绍;18_Apache Thrift应用详解与实例剖析;19_Apache Thrift原理与架构解析;20_通过Apache Thrift实现Java与Python的RPC调用;21_gRPC深入详解 ;22_gRPC实践 ;23_Gradle...
17_Protobuf使用最佳实践与Apache Thrift介绍 18_Apache Thrift应用详解与实例剖析 19_Apache Thrift原理与架构解析 20_通过Apache Thrift实现Java与Python的RPC调用 21_gRPC深入详解 22_gRPC实践 23_Gradle Wrapper...
这些源码经过测试和验证,可以直接运行,方便学生快速上手并开始实践。 【应用场景】: 竞赛准备:适用于各种教育部认可的竞赛,如全国电子设计大赛、全国大学生智能汽车竞赛等,他们可以借助这些资料了解竞赛的...
14_Protobuf完整实例详解 15_Protobuf集成Netty与多协议消息传 递 16_Protobuf多协议消息支援与工程最佳实践 17_Protobuf使用最佳实践与Apache Thrift介绍 18_Apache Thrift应用详解与实例剖析 19_Apache Thrift原理...
第17讲:Protobuf使用最佳实践与Apache Thrift介绍 第18讲:Apache Thrift应用详解与实例剖析 第19讲:Apache Thrift原理与架构解析 第20讲:通过Apache Thrift实现Java与Python的RPC调用 第21讲:gRPC深入详解 ...
该项目已升级至 #城堡平台 本人承诺该项目完全开放,不会有任何的收费计划。 ... Castle Platform的目标是打造一个高级,高扩展性的java开发平台,完成通用的管理功能。采用了后台管理集中部署,会员业务系统可分离...
用户指南 ...服务化最佳实践 分包 粒度 版本 兼容性 枚举值 序列化 异常 调用 推荐用法 容量规划 基准测试工具包 性能测试报告 测试说明 测试环境 测试目的 测试脚本 测试结果 测试分析 测试覆盖率报告
Difeye是一款超轻量级PHP框架,前身由Kohana框架演变而来,在企业实践开发中,我们不断对框架做了精简和修改,抽取最核心模块,并改写底层功能,使之更容易部署、易于上手、简单安全、迁移维护方便、适于单机集群...
Difeye是一款超轻量级PHP框架,前身由Kohana框架演变而来,在企业实践开发中,我们不断对框架做了精简和修改,抽取最核心模块,并改写底层功能,使之更容易部署、易于上手、简单安全、迁移维护方便、适于单机集群...
而这本书提供了很多有意义的答案,无论你是否正在评估这种非关系型数据库或者正试图立刻把它付诸实践。 ■探讨与hadoop的高度集成如何使hbase的可伸缩性变得简单 ■把大型数据集分布到相对廉价的商业服务器集群...
而这本书提供了很多有意义的答案,无论你是否正在评估这种非关系型数据库或者正试图立刻把它付诸实践。 ■探讨与hadoop的高度集成如何使hbase的可伸缩性变得简单 ■把大型数据集分布到相对廉价的商业服务器集群...
3,结合工作实践及分析应用,培养解决实际问题的能力。 4,每一块知识点, 都有配套案例, 学习不再迷茫。 适用人群 1、对大数据感兴趣的在校生及应届毕业生。 2、对目前职业有进一步提升要求,希望从事大数据行业...