Thrift Layer
@ Yang Guang · Aug 20, 2019 · 1 min read · Update at Aug 20, 2019

thrift分层结构解析

thrift

thrift分层结构

thrift

Transport层

Transport层提供了一个从网络中简单的抽象化的读写操作。这样使得thrift能将底层传输和其他部分解耦(如:序列化/反序列化)

以下是Transport接口:

  • open
  • close
  • read
  • write
  • flush

除了上面的Transport接口,thrift还使用ServerTransport接口来接受和创建原始传输对象。顾名思义,ServerTransport主要用于服务端,通过传入一个连接来创建新的传输对象。其主要方法有:

  • open
  • listen
  • accept
  • close

以下是Thrift支持语言中的一些可用的传输:

  • file: read/write to/from a file on disk
  • http: 顾名思义

Protocol层

Protocol抽象化的定义了一种将内存数据接口映射到一种线型格式的机制。换句话说,一个Protocol指定了数据类型如何使用底层Transport来对自身进行编码/解码。因此Protocol实现了控制编码方案及负责(反)序列化。这种意义上的协议的一些实例包括JSON,XML,纯文本,compact binary(紧凑型二进制)。

以下是Protocol接口:

  • writeMessageBegin(name, type, seq)
  • writeMessageEnd()
  • writeStructBegin(name)
  • writeStructEnd()
  • writeFieldBegin(name, type, id)
  • writeFieldEnd()
  • writeFieldStop()
  • writeMapBegin(ktype, vtype, size)
  • writeMapEnd()
  • writeListBegin(etype, size)
  • writeListEnd()
  • writeSetBegin(etype, size)
  • writeSetEnd()
  • writeBool(bool)
  • writeByte(byte)
  • writeI16(i16)
  • writeI32(i32)
  • writeI64(i64)
  • writeDouble(double)
  • writeString(string)
  • name, type, seq = readMessageBegin()
  • name, type, seq = readMessageEnd()
  • name = readStructBegin()
  • name = readStructEnd()
  • name, type, id = readFieldBegin()
  • name, type, id = readFieldEnd()
  • k, v, size = readMapBegin()
  • k, v, size = readMapEnd()
  • etype, size = readListBegin()
  • etype, size = readListEnd()
  • etype, size = readSetBegin()
  • etype, size = readSetEnd()
  • bool = readBool()
  • byte = readByte()
  • i16 = readI16()
  • i32 = readI32()
  • i64 = readI64()
  • double = readDouble()
  • string = readString()

Thrift Protocols 是面向流的设计。这里不需要任何明确的框架。例如,在开始序列化之前,它没必要知道一个字符串的长度或者list中元素的数量。Thrift支持的大多数语言可用的一些协议有: * binary:相当于简单的二进制编码–字段的长度和类型被编码为字节,后跟字段的实际值。(Fairly simple binary encoding – the length and type of a field are encoded as bytes followed by the actual value of the field.) * compact:参考 THRIFT-110 * json

Processor层

一个Processor封装了从输入流读取数据和写入数据到输出流的能力。这些输入流和输出流由Protocol对象表示。Processor接口非常的简单

interface TProcessor {
    bool process(TProtocol in, TProtocol out) throws TException
}

服务特定的Processor由编译器生成。Processor实质上是从线路中读取数据(使用输入协议),将处理委托给处理程序(handler,由用户实现),并且通过线路写入响应中(使用输出协议)。

Server层

一个Server(服务)聚集了上述所有的功能:

  • 创建一个Transport
  • 从Transport中创建输入/输出Protocols
  • 基于输入/输出协议(Protocol)创建一个处理器(Processor)
  • 等待传入连接并且交给处理器(Processor)

YangGuang's blog
Knowing and doing

rpc thrift

© 2019 杨洸的博客

Powered by Hugo with theme Dream.

简介