zoukankan      html  css  js  c++  java
  • Apache Thrift 白皮书

    介绍:

    轻量级、跨语言。

    简洁的抽象和实现:数据传输、序列化、应用逻辑处理。

    IDL及代码生成系统。

    基本架构图如下:

    28种语言支持:28 programming languages

    支持客户端及服务端版本非原子性升级。

    Thrift whitepaper

    一、IDL

    1、基本数据类型:

    • 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

    2、Structs:通用对象

    等同于面向对象语言中的类。

    struct Example {

        1:i32 number=10,

        2:i64 bigNumber

    }

    3、Containers 容器:

    list<>、set<>、map<,>

    4、Exceptions 异常:

    等同于Structs,区别定义。

    5、Services 服务定义:

    等同于接口定义(或面型对象语言中的虚拟类)。

    基本定义:

    service <service-name> {

    <return-type> <method-name>() [throws (<exceptions>)]

    ...

    }

    示例:

    service StringCache {

        void set(1:i32 key, 2:string value),

        string get(1:i32 key) throws (1:KeyNotFound knf),

        void delete(1:i32 key)

    }

    二、Transport

    数据传输

    1、接口 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:接受或创建Transport对象。

    2、实现

    TSocket:TCP/IP数据流操作。

    TFileTransport:文件系统数据流操作。

    扩展:TBufferedTransport(提供读写buffer缓冲)、TFramedTransport(提供帧大小头信息,用以批数据传输或非阻塞操作)、TMemoryBuffer(直接从堆栈中读写)

    三、Protocol

    数据结构和数据传输解耦。

    1、接口,双向消息序列化反序列化,基本方法如下:

    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()

    2、...

    四、RPC实现

    1、TProcessor

    基本定义:

    interface TProcessor {

        bool process(TProtocol in, TProtocol out) throws TException

    }

    任何复杂的系统简而来说节课分为两个端客户端、服务端,用于处理输入输出。

    process() 用于处理方法调用逻辑。

    2、TServer

    基本处理逻辑:

    ->使用TServerTransport接受TTransport

    ->使用TTransportFactory转换TTransport为实际应用的类型(通常应用TBufferedTransportFactory)

    ->使用TProtocolFactory为TTransport创建输入输出

    ->调用TProcessor process()逻辑

    server处理连接;processor处理RPC逻辑。

    不同实现:

    TSimpleServer:单处理线程

    TThreadedServer:每个连接一个处理线程

    TThreadPoolServer:线程池处理

    五、实现细节

    。。。

     

  • 相关阅读:
    python之shutil模块
    python的os模块
    python的map函数
    Web基础知识
    Web基础知识 --- html中的meta元素有什么用?
    使用技巧 --- 与 FireFox 相关
    基础知识之WIN32 API
    资料索引
    基础知识之C++篇
    使用技巧 --- 与 Visual Studio 有关
  • 原文地址:https://www.cnblogs.com/niejunlei/p/12686334.html
Copyright © 2011-2022 走看看