thrift介绍与实践

[size=large]1.Introduce[/size]
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.

[img]http://dl.iteye.com/upload/attachment/486444/190b34fd-9f69-3e57-8f91-d461eabf946c.jpg[/img]

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.

[size=large]2.Types[/size]
It also does not require that the developer write any code for object serialization or transport.

Thrift [color=red]IDL[/color] (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)
}

[size=large]3.Transport[/size]
3.1Interface
[b]TTransport[/b]
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

[b]TServerTransport[/b]
open Opens the transport
listen Begins listening for connections
accept Returns a new client transport
close Closes the transport

3.2Implementation
[b]Tsocket[/b]
provides a common, simple interface to a TCP/IP stream socket.

[b]TFileTransport[/b]
an abstraction of an on-disk file to a datastream.

[b]Utilities[/b]
TBufferedTransport
TFramedTransport
TMemoryBuffer

[size=large]4.Protocol[/size]
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.

[size=large]5.Versioning[/size]
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"
}

[b]Isset[/b]
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

[size=large]6.Processors[/size]
6.1RPC Implementation
[b]Tprocessor[/b]
interface TProcessor {
bool process(TProtocol in, TProtocol out)
throws TException
}

[b]Generated Code[/b]
all the logic to handle RPC invocations via the process() call

[b]TServer[/b]

Implementation Details

[size=large]7.Facebook Thrift Services[/size]
Search
Logging
Mobile
Ads

[size=large]8.Similar Systems[/size]
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.

[size=large]9.Practise[/size]
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官网
[url]http://thrift.apache.org/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值