`
leign
  • 浏览: 169020 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

thrift介绍与实践

阅读更多
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
分享到:
评论

相关推荐

    spring与thrift集成

    Spring 框架是 Java 企业级应用开发的主流选择,它提供了...Spring 提供了丰富的工具和最佳实践来支持这些需求,而 Thrift 则确保了高效的跨语言通信。通过深入理解这两个框架,你可以构建出稳定、高性能的分布式系统。

    基于Java语言的Thrift框架设计源码学习与实践指南

    本项目为基于Java语言的Thrift框架设计源码学习与实践指南,包含52个文件,其中Java源文件30个,属性文件4个,Git忽略文件3个,XML文件3个,Thrift文件3个,JAR包文件2个,CMD脚本2个,YML文件2个以及Markdown文件1...

    thrift实现http协议案例

    在本案例中,“thrift实现http协议案例”是关于如何利用Thrift来处理HTTP协议通信的一个实践教程。 首先,让我们了解一下Thrift的基本工作原理。Thrift基于接口描述语言(IDL),开发者可以在IDL文件中定义服务接口...

    netty+thrift高并发高性能

    本文将深入探讨Netty与Thrift结合实现高并发高性能的关键技术点。 #### 二、Netty 高性能分析 ##### 2.1 RPC调用性能瓶颈分析 在传统的RPC框架中,主要存在以下三大性能瓶颈: 1. **网络传输方式**:传统的RPC...

    Thrift-java学习小结

    Thrift是Facebook开源的一款高性能、跨语言的服务框架,它的设计目标是高效地在不同编程语言之间进行通信。本文将基于Thrift的Java实现,总结...通过深入理解和实践Thrift,开发者能够更好地实现服务之间的高效通信。

    基于SpringBoot的Apache Thrift RPC框架实践设计源码

    该项目是《RPC框架实践之:Apache Thrift》博文所对应的实践设计源码,总计包含30个文件,其中Java源文件8个,XML配置文件3个,属性文件2个,以及其他文件如DS_Store、LICENSE和md等。该项目通过SpringBoot整合...

    thrift样例操作实例

    首先,`Thrift简介.docx`可能是关于Thrift的入门文档,介绍了Thrift的概念、特点和用途。Thrift的主要特点包括高效、类型安全以及支持多种编程语言,如Java、C++、Python等。它通过定义服务接口,使得不同语言之间...

    maven-thrift-server

    【 Maven-Thrift-Server:构建Thrift服务的Maven实践】 在软件开发中,Thrift是一种高效的跨语言服务开发框架,由Facebook开发并开源。它允许定义数据类型和服务接口,然后自动生成各种编程语言的代码,使得不同...

    thrift + 服务模型实例演示(java)

    主要是对thrift0.9.0 TSimpleServer、TThreadPoolServer 、TNonblockingServer、THsHaServer等服务模型实例和AsynClient 异步客户端实例代码的演示

    thrift阻塞与非阻塞模式下的测试

    通过对Thrift阻塞和非阻塞模式的测试,我们可以得出适合特定业务场景的最佳实践,优化服务性能,提高系统的整体效率。在实际应用中,可能需要结合具体情况,选择单线程阻塞、多线程阻塞、异步IO、NIO或AIO等模式的...

    Windows下QT使用Thrift的样例

    总之,这个样例项目提供了一个很好的起点,帮助开发者了解如何在Windows上用QT与Thrift协同工作,同时利用Boost库增强功能。通过实践,你可以更好地理解和掌握这些技术,为你的项目带来高效、跨平台的解决方案。

    zk+thrift demo

    【标题】"zk+thrift demo" 演示了如何结合 ZooKeeper 和 Thrift 这两个技术在实际应用中的整合。...这个过程涵盖了分布式系统中服务发现、注册与通信的关键环节,对于理解和实践微服务架构有很高的价值。

    Thrift--JSClient

    1. **Thrift IDL**:介绍Thrift接口定义语言,它是如何定义服务接口和数据结构的,以及如何通过`thrift`编译器将这些定义转换为JavaScript代码。 2. **Thrift协议**:讲解Thrift的二进制传输协议,如何高效地序列化...

    thrift操作Hbase数据库

    在IT行业中,Thrift是一种高性能、可扩展的跨语言服务框架,由Facebook开发并开源。它允许定义数据类型和...学习并实践这个项目,不仅可以提升对Thrift和Hbase的理解,也有助于提升在分布式大数据环境下的编程能力。

    windows下vs2010编译的thrift,包含lib和cpp源码

    Thrift是一种开源的跨语言服务开发框架,由Facebook于2007年设计,现在由Apache软件基金会维护。它的主要功能是提供一个定义接口、生成代码和服务通信的...总的来说,这是一个对理解和实践Thrift框架非常有帮助的资源。

    Thrift 示例代码_Java

    Thrift 是一个开源的跨语言服务开发框架,由 Facebook 在 2007 年创建并贡献给了 Apache 基金会。它提供了一种高效、...通过实践和理解这个示例,开发者能够更好地掌握 Thrift 的工作原理,并将其应用于实际项目中。

    thrift1 查询hbase

    在本案例中,"thrift1 查询hbase"是指使用Python通过Thrift1接口来与HBase进行交互,实现数据的查询操作。下面将详细讲解这个过程。 1. **Thrift接口**:Thrift提供了一种序列化和RPC(远程过程调用)机制,允许...

Global site tag (gtag.js) - Google Analytics