Serialization related concepts

In a distributed scenario, for example, I want to transfer an entity class of service B to service A, and convert the object into byte stream data that can be transmitted.This is serialization.

Converting a data stored in memory into a transmittable and storable object is serialization.

In the rpc framework, serialization must exist.

(1) serialVersionUID serialized version number

This is a version number. If it is not defined, one will be generated by itself. serialVersionUID is a security verification mechanism. The id of the serialized object is the same as the id of the deserialized object, so that it can be serialized. Security is an identification. If the id during serialization is inconsistent with the id of deserialization, serialization is not allowed.

The serialVersionUID generation rule is based on the field attributes of the object to generate

When using it, you must be careful not to forget to define it.Otherwise, some programmers may see that the serialVersionUID is not defined, and they owe themselves to define one, then the serialized object and the deserialized object will be inconsistent and an exception will be thrown.

(二)Transient

It is the way to control the serialization of member attributes.If transient is added to the member attributes, this member will be removed during serialization.

You can declare writeObject and readObject in the serialized class, and you can read the member variables modified by transient.

public class User implements Serializable {
    
    
  
  
    private static final long serialVersionUID = -434539422310062943L;
  
    private String name;
    private int age;
  
  
    private void writeObject(java.io.ObjectOutputStream s) throws IOException {
    
    
        s.defaultWriteObject();
  
        s.writeObject(name);
    }
  
    private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {
    
    
        s.defaultReadObject();
        name=(String)s.readObject();
    }

(Three) jdk built-in serialization problem

Serialization involves converting objects into data streams, and deserialization is converting data classes into objects.

Factors affecting rpc communication performance

1. The computational performance of serialization
2. The size of the data packet
3. The distributed architecture is cross-language (language diversity), so serialization needs to have cross-language requirements.

xml serialization is to convert an object into xml and then into a data stream
json serialization is to convert an object into json and then into a data stream 
Hessian is a cross-language binary serialization protocol

Guess you like

Origin blog.csdn.net/qq_41489540/article/details/114460221