Chapter 7 Google Protobuf

7.1 Basic introduction to encoding and decoding

  1. When writing network applications, because the data transmitted in the network is binary bytecode data, it needs to be encoded when sending data, and needs to be decoded when receiving data

  2. There are two components of codec (codec) : decoder (decoder) and encoder (encoder). The encoder is responsible for converting business data into bytecode data, and the decoder is responsible for converting bytecode data into business data
    Insert picture description here

7.2 Netty's own encoding and decoding mechanism and problem analysis

  1. Netty itself provides some codec (codec)

  2. Netty provides the encoder-
    StringEncoder, encoding string data
    ObjectEncoder, encoding Java objects

  3. Netty provides the decoder
    StringDecoder, which decodes string data,
    ObjectDecoder, and decodes Java objects

  4. Netty's own ObjectDecoder and ObjectEncoder can be used to implement the encoding and decoding of POJO objects or various business objects. The bottom layer still uses Java serialization technology, and Java serialization technology itself is not efficient. There are the following problems:
    1 Cannot cross language
    2. The volume after serialization is too large, more than 5 times that of binary encoding.
    3. Serialization performance is too low
    ===> leads to a new solution [Google's Protobuf]

7.3 Protobuf

Insert picture description here
Insert picture description here

7.4 Protobuf quick start example

Write a program and use Protobuf to complete the following functions

  1. The client can send a Student PoJo object to the server (encoded by Protobuf)
  2. The server can receive the Student PoJo object and display the information (decoded by Protobuf)
    Insert picture description here

7.5 Protobuf Quick Start Example 2

  1. Write a program and use Protobuf to complete the following functions
  2. The client can randomly send Student PoJo / Worker PoJo objects to the server (encoded by Protobuf)
  3. The server can receive Student PoJo / Worker PoJo objects (need to determine which type), and display information (decoded by Protobuf)
    Insert picture description here
Published 138 original articles · Like 3 · Visitor 7227

Guess you like

Origin blog.csdn.net/weixin_43719015/article/details/105300193