Thrift的网络栈apache
Apache Thrift的网络栈的简单表示以下:json
+-------------------------------------------+ | Server | | (single-threaded, event-driven etc) | +-------------------------------------------+ | Processor | | (compiler generated) | +-------------------------------------------+ | Protocol | | (JSON, compact etc) | +-------------------------------------------+ | Transport | | (raw TCP, HTTP etc) | +-------------------------------------------+
Transport (传输)网络
传输层提供了一个用于向网络读写的简单抽象. 经过这种方式, thrift能够将下层的传输协议与thrift上层的其余部分((例如序列化和反序列化)分离.数据结构
下面是Transport接口提供的一些方法: 函数
(1) open编码
(2) closespa
(3) read设计
(4) writecode
(5) flushserver
除了上面的Transport接口, Thrift还使用ServerTransport接口来接受或者生成基本的transport对象. 正如名字所建议的, ServerTransport主要用在服务端为新来的链接建立新的 Transport对象. 提供的方法以下:
(1) open
(2) listen
(3) accept
(4) close
绝大多数thrift支持的语言均可用的transport以下:
(1) file: 从硬盘中读写文件
(2) http: http网络通讯
Protocol (协议)
Protocol用来定义内存中的数据结构如何使用下层的Transport来序列化和反序列化自身. 因此protocol实现用来控制编码模式, 以及用于序列化和反序列化. protocol的一些实例包括JSON, XML, plain text, 紧凑的二进制编码等.
下面是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() readMessageEnd() name = readStructBegin() readStructEnd() name, type, id = readFieldBegin() readFieldEnd() k, v, size = readMapBegin() readMapEnd() etype, size = readListBegin() readListEnd() etype, size = readSetBegin() readSetEnd() bool = readBool() byte = readByte() i16 = readI16() i32 = readI32() i64 = readI64() double = readDouble() string = readString()
Thrift Protocols是面向字节流设计的. 这里不须要进行显式的分片. 例如, 咱们在序列化时, 不须要知道字符串的长度, 也不须要知道list的元素个数. 对于绝大多数thrift支持的语言都支持的Protocol(协议)有:
(1) binary: 很简单的二进制编码, 现将成员的长度和类型编码成字节, 而后跟上成员的实际值
(2) 紧凑的编码格式 (参考 https://issues.apache.org/jira/browse/THRIFT-110)
(3) json格式
Processor (处理单元)
一个Processor用来封装从输入流读取数据和向输出流写入数据的能力. 输入流和输出流使用Protocol对象来表示. Processor的接口很简单:
interface TProcessor { bool process(TProtocol in, TProtocol out) throws TException }
服务端特定的processor实现由编译器生成. Processor所作的是从输入protocol中读取数据, 而后将这些数据转交给handler(具体处理单元, 用户实现), 而后再将结果写入到输出protocol中.
Server
Server用来将上述特征汇集到一块儿:
(1) 建立一个基于Handler的Processor
(2) 建立一个transport
(3) 建立输入和输出协议(protocol)
基于上面的组员, 建立一个Server, 而后Server调用serve函数, 等待链接, 而后对请求进行处理.
参考示例 (C++):
class SharedServiceHandler : virtual public SharedServiceIf { public: SharedServiceHandler() { // Your initialization goes here } void getStruct(SharedStruct& _return, const int32_t key) { // Your implementation goes here printf("getStruct\n"); } }; int main(int argc, char **argv) { int port = 9090; shared_ptr<SharedServiceHandler> handler(new SharedServiceHandler()); shared_ptr<TProcessor> processor(new SharedServiceProcessor(handler)); shared_ptr<TServerTransport> serverTransport(new TServerSocket(port)); shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory()); shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory); server.serve(); return 0; }