Serialization of Dubbo source code

1. Demand

         1) Understand the serialization of Dubbo

        2) Understand the source code of Dubbo serialization of multiple serialization methods

2. Source code

    Serialize

 Serialize is the core interface of Dubbo. Convert the object to a stream of bytes. Used for network transmission, and converting byte streams to objects, which are used to restore to objects after receiving byte stream data.

interface diagram

    1. Serialization interface

@SPI("hessian2")
public interface Serialization {

    /**
     * get content type id
     *
     * @return content type id
     */
    byte getContentTypeId();

    /**
     * get content type
     *
     * @return content type
     */
    String getContentType();

    /**
     * create serializer
     *
     * @param url
     * @param output
     * @return serializer
     * @throws IOException
     */
    @Adaptive
    ObjectOutput serialize(URL url, OutputStream output) throws IOException;

    /**
     * create deserializer
     *
     * @param url
     * @param input
     * @return deserializer
     * @throws IOException
     */
    @Adaptive
    ObjectInput deserialize(URL url, InputStream input) throws IOException;

}

 

The SPI annotation specifies that the default implementation is hessian2

Since serialization relies on jdk's OutputStream and InputStream

Dubbo defines DataOutput DataInput as various serialized output and input interfaces

       2. Serialization method

        NativeJavaSerialization : Java's own serialization implementation

       CompactedJavaSerialization : compressed java serialization, mainly based on native java serialization, realizes the writing and reading of self-defined class description descriptors

        The class descriptor that writes the Object type only writes the class name, not the complete information about the class. In this way, when there are many Object types, the size after serialization can be reduced

       JavaSerialization : Just a wrapper around native java serialization and compressed java serialization

      JsonSerialization : Self-implemented JSON serialization implementation

     FastJsonSerialization : Serialization implemented using Ali's FastJson

    Hessian2Serialization: Serialization implemented using Hessian2's IO mechanism (default implementation)

    DubboSerialization: Dubbo's own defined serialization implementation

 

  3. Hessian2Serialization implementation

 

public class Hessian2Serialization implements Serialization {

    public static final byte ID = 2;

    public byte getContentTypeId() {
        return ID;
    }

    public String getContentType() {
        return "x-application/hessian2";
    }

    public ObjectOutput serialize(URL url, OutputStream out) throws IOException {
        return new Hessian2ObjectOutput(out);
    }

    public ObjectInput deserialize(URL url, InputStream is) throws IOException {
        return new Hessian2ObjectInput(is);
    }

}

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325894517&siteId=291194637