Thrift protocol

What is Thrift

** thrift is a commonly used communication protocol for rpc. It uses idl to define rpc methods and data structures, and uses thrift compiler to generate client code and server code in different languages ​​according to idl definitions. These generated codes implement remote methods Resolution of calls and call parameters. **

The role of idl

** Before using thrift protocol to develop rpc method, you need to define the idl file of the method. idl strictly defines the output and input parameters of the method, because the caller code and the code of the callee are generated based on idl, so idl is a strong agreement between the caller and the callee, which guarantees the parameters and data structure of both parties Consistency, both parties can develop in parallel according to idl during development. **

thrift's layered architecture

** As shown in the figure above, thrift is divided into four layers: server, processor, protocal, and transport. The server is generally implemented by the rpc framework itself. The handler is the business logic implemented by business developers, and the processor is the code automatically generated by thrift according to idl, protocal, transport It is a general class library, and the rpc framework chooses it as needed. **

1.Transport layer

** It is an abstraction of tcp and http protocols, providing a total of 5 interfaces: open, close, read, write and flush **
** thrift supports tcp protocol, http protocol and other communication protocols, and provides unified abstraction, the upper layer does not need to care Which protocol is used. So how does the transport layer know which protocol is used? As you will see later, the server layer implemented by the rpc framework will select the communication protocol according to its own needs**

2.Protocal layer

** Defines the conversion mechanism between the data structure in the memory and the wire-format, which is the encode/decode mechanism. The data is encoded into the actual transmission format on the server, supporting json, compression, binary and other formats. **


// encode之前
{
    
    
  uid: 1,
  name: "Mark Slee",
  blurb: "blurb here."
}
// encode之后
[
  1, // Version
  "store", // 服务名
  1, // message类型
  0, // seqid
  {
    
    
    "1": {
    
    
      "rec": {
    
    
        "1": {
    
    
          "i32": 1
        },
        "2": {
    
    
          "str": "Mark Slee"
        },
        "3": {
    
    
          "str": "blurb here."
        }
      }
    }
  }
]

3.Processor

** The first two layers are general and have nothing to do with specific business, the processor is the processing code generated according to idl, and the processor implements the following interfaces. It can be seen that it encapsulates the ability to read input parameters and write return parameters. The processor will read the parameters from the input stream and parse the local objects according to the format definition of idl, and pass the parameters to the handler (implemented by the business developer) for business processing. Finally, the return value of the handler is written to the output stream and returned to the client. **

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

4.Server

** thrift separates the variable parts to the server layer, which is implemented by the rpc framework itself, and the processor is a fixed logic. The rpc framework needs to pass the variable parameter handler, protocol and transport to the processor. For example, whether the transmission uses http or tcp, nio or bio, etc., the server chooses according to performance needs. In addition, a very important function of the server layer is to monitor the tcp port and establish a connection with the client for IO communication, so that a server is truly running. **

Guess you like

Origin blog.csdn.net/HolleDream/article/details/109279607