7.1 Basic introduction to encoding and decoding
-
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
-
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
7.2 Netty's own encoding and decoding mechanism and problem analysis
-
Netty itself provides some codec (codec)
-
Netty provides the encoder-
StringEncoder, encoding string data
ObjectEncoder, encoding Java objects -
Netty provides the decoder
StringDecoder, which decodes string data,
ObjectDecoder, and decodes Java objects -
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
7.4 Protobuf quick start example
Write a program and use Protobuf to complete the following functions
- The client can send a Student PoJo object to the server (encoded by Protobuf)
- The server can receive the Student PoJo object and display the information (decoded by Protobuf)
7.5 Protobuf Quick Start Example 2
- Write a program and use Protobuf to complete the following functions
- The client can randomly send Student PoJo / Worker PoJo objects to the server (encoded by Protobuf)
- The server can receive Student PoJo / Worker PoJo objects (need to determine which type), and display information (decoded by Protobuf)