Apache Thrift learning (1): Thrift detailed introduction

Overview

Apache Thrift is a software framework that supports scalable and cross-language service development. It combines a software stack and a code generation engine to build services. These services are available in C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell , C#, Cocoa, JavaScript, Nodejs, Smalltalk, OCaml, Delphi and other languages ​​work efficiently and seamlessly. Similar to protobuf, it also has its own definition file-  IDL (Interface Description Language), which uses code generators to build clients and servers in different languages ​​to achieve cross-platform and cross-language data transfer ( socket ), which is typical The CS structure.

Basic data type

Thrift does not support unsigned types, because Java does not support unsigned types.

The data types supported by the RPC framework depend on the communication of the data types of each language it supports .

  • bool: Boolean value (true or false)
  • byte: 8-bit signed integer
  • i16: 16-bit signed integer
  • i32: 32-bit signed integer
  • i64: 64-bit signed integer
  • double: 64-bit floating point number
  • string: a text string encoded with UTF-8 encoding

Container type

The elements in the container collection can be of any type except service, including exceptions . At the same time it is possible to use the < T > generic

  • list : An ordered collection of elements. Similar to STL vector, Java ArrayList, native array of scripting language, etc.
  • set : An unordered collection of elements. Similar to STL set, Java HashSet, set in Python, etc. Note: Since PHP does not support set type, Thrift's set is similar to list in PHP.
  • map : key-value pair type. Similar to STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc. While defaults are provided, the type mappings are not explicitly fixed (I don’t know how to understand this passage, I spit it out). Added custom code generator instructions to allow replacement of custom types in various target languages. .

Structure-struct

Similar to the message in Protocol buffers, the specific details will be added later.

struct Work {
  1: i32 num1 = 0,
  2: i32 num2,
  3: Operation op,
  4: optional string comment,
}

Exception-exception

Thrift supports custom exceptions , similar to struct definitions, except that the keyword is exception .

exception InvalidOperation {
  1: i32 whatOp,
  2: string why
}

Support enumeration

enum Operation {
  ADD = 1,
  SUBTRACT = 2,
  MULTIPLY = 3,
  DIVIDE = 4
}

Define service-service

The definition of service is semantically equivalent to defining an interface (or a pure virtual abstract class) in object-oriented programming, and the Thrift compiler generates a client and server that realizes the complete interface function.

A service consists of a set of named functions, each of which has a parameter list and a return type.

/**
 * Ahh, now onto the cool part, defining a service. Services just need a name
 * and can optionally inherit from another service using the extends keyword.
 */
service Calculator extends shared.SharedService {

  /**
   * A method definition looks like C code. It has a return type, arguments,
   * and optionally a list of exceptions that it may throw. Note that argument
   * lists and exception lists are specified using the exact same syntax as
   * field lists in struct or exception definitions.
   */

   void ping(),

   i32 add(1:i32 num1, 2:i32 num2),

   i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),

   /**
    * This method has a oneway modifier. That means the client only makes
    * a request and does not listen for any response at all. Oneway methods
    * must be void.
    */
   oneway void zip()

}

It should be noted that void is a valid return type in addition to other types defined by Thrift  . In addition, an oneway modifier keyword may be added to a void function, which will generate code that does not wait for a response (I really don’t know how to understand it. The official website seems to be quite important. I will understand it later. Looking back, people who understand what it means can also leave a message and tell me QAQ). Please note that a single void function will return a response to the client to ensure that the operation has been completed on the server side .

Through a one-way method call, the client can only guarantee that the request is successful at the transport layer. One-way method calls of the same client may be executed by the server in a parallel/out of order. (With oneway method calls the client will only be guaranteed that the request succeeded at the transport layer. Oneway method calls of the same client may be executed in parallel/out of order by the server.) I tried my best to understand, in order not to mislead you , Or post the original text.

Constant-const

/**
 * Thrift also lets you define constants for use across languages. Complex
 * types and structs are specified using JSON notation.
 */
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}

namespace

The namespace of Thrift is equivalent to the package in Java, and its main function is to organize code

namespace java tutorial

Definition format: namespace [language name] [path]

File reference-include

/**
 * Thrift files can reference other Thrift files to include common struct
 * and service definitions. These are found using the current path, or by
 * searching relative to any paths specified with the -I compiler flag.
 *
 * Included objects are accessed using the name of the .thrift file as a
 * prefix. i.e. shared.SharedObject
 */
include "shared.thrift"

Other .thrift files can be referenced through include , and the definition of [ reference file ] can be included in the current definition file .

Field modification

You can also define required / optional (required/optional), and also need to identify the field, such as 1: 2: 3: in the following definition

struct Work {
  1: required i32 num1 = 0,
  2: optional i32 num2,
  3: optional string comment,
}

 

Guess you like

Origin blog.csdn.net/qq_25805331/article/details/108416281