preface

Hello everyone, today we start to share with you – Dubbo topic Dubbo serialization. In the previous chapter, we introduced the label routing of Dubbo routing rules. Its implementation principle is as follows: if the consumer passes the label, it matches the configured dynamic and static rules; if the consumer does not pass the label, it matches the locally configured static and dynamic configured labels of the service provider. At the same time, we also give examples of common use scenarios and source code analysis to analyze its implementation principle. In Dubbo, remote call data transfer is used to serialize data. In this chapter we will discuss what serialization methods are available in our Dubbo and how well they perform. Let’s get started quickly!

1. Introduction to serialization

First we need to understand what serialization and deserialization are. Here’s a simple example: When we need to write a data object to a file or transfer it over the network, we convert the data object to binary format for transmission, a process called serialization, whereas when a remote data or local file data needs to be read and parsed into our object, it is called deserialization. In Dubbo RPC, multiple serialization modes are supported simultaneously:

  1. Dubbo serialization: Ali has not yet developed an efficient Java serialization implementation, and ali does not recommend its use in production environments

  2. Hessian2 Serialization: Hessian is an efficient way to serialize binary data across languages. But this is actually not native Hessian2 serialization, but Ali’s modified Hessian Lite, which is the serialization enabled by Dubbo RPC by default

  3. Json serialization: there are two implementations, one is the Fastjson library of Ali, the other is the simple Json library of Dubbo itself, but its implementation is not particularly mature, and the performance of Json text serialization is generally inferior to the above two binary serialization.

  4. Java serialization: mainly using Java Java serialization JDK implementation, performance is not ideal.

Here are the serialization methods currently supported in 2.7.x:

Dubbo RPC uses Hessian2 serialization by default. But Hessian is an older serialization implementation, and it’s cross-language, so it’s not optimized for Java alone. Dubbo RPC is really a Java to Java remote call without the need for cross-language serialization. In recent years, a variety of new efficient serialization methods have been introduced to push the upper limit of serialization performance. Typical examples include:

  1. Java language specific: Kryo, FST, etc

  2. Cross-language: Protostuff, ProtoBuf, Thrift, Avro, MsgPack, etc

Kryo is a very mature serialization implementation that has been widely used on Twitter, Groupon, Yahoo, and other well-known open source projects such as Hive and Storm. FST is a relatively new serialization implementation and currently lacks enough mature use cases.

2. Configuration mode

The following configuration is mainly introduced through XML:

XML way

<dubbo:protocol name="dubbo" serialization="hession2"/>
Copy the code

Serialization is used here to configure the serialization mode.

3. Application scenarios

From the previous introduction, we have a general understanding of what serialization and deserialization are, and serialization and deserialization are required in Dubbo. So Dubbo provides multiple serialization methods, which one should we use? Let’s start with a performance test chart from the official website:

As can be seen from the figure above, serialization methods: KYRO and FST have the best performance. If the serialized class does not contain a constructor with no arguments, performance in Kryo serialization will suffer because we will transparently replace Kryo serialization with Java serialization at the bottom. Therefore, it is a best practice to add a no-argument constructor to every serialized class whenever possible. In addition, neither Kryo nor FST should implement the Serializable interface for all serialized classes, but we recommend that each class implement it to maintain compatibility with Java serialization and Dubbo serialization.

4. Example demonstration

Let me show you how to get a list of books. The project structure is as follows:

Let’s look at the XML configuration of dubo-provider-xml. XML on the service provider side:


      
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <! Hession2 -->
    <dubbo:protocol port="20880"  serialization="hession2"/>

    <dubbo:application name="demo-provider" metadata-type="remote"/>

    <dubbo:registry address=Zookeeper: / / "127.0.0.1:2181"/>

    <bean id="bookFacade" class="com.muke.dubbocourse.serializable.provider.BookFacadeImpl"/>

    <! -- Exposed service for Dubbo service -->
    <dubbo:service interface="com.muke.dubbocourse.common.api.BookFacade" ref="bookFacade" />

</beans>

Copy the code

The serialization mode specified in the configuration file above is hession2.

5. Implementation principle

Below we through the source code of the way simple analysis of their implementation principle. Below we directly to the core of the serialization class org. The apache. Dubbo. Remoting. Transport. CodecSupport we see its deserialize deserialize method:

 public static ObjectInput deserialize(URL url, InputStream is, byte proto) throws IOException {
   // Get the serialized object
   Serialization s = getSerialization(url, proto);
        return s.deserialize(url, is);
    }
Copy the code

Let’s continue with the getSerialization method:

 public static Serialization getSerialization(URL url, Byte id) throws IOException {
        // Find Serialization objects by protocol
        Serialization serialization = getSerializationById(id);
        String serializationName = url.getParameter(Constants.SERIALIZATION_KEY, Constants.DEFAULT_REMOTING_SERIALIZATION);
        / /...
        return serialization;
    }
Copy the code

Find all serialization methods registered through SPI above. Let’s follow the deserialize method. Here we use the Serialization method provided by the Java JDK as an example:

    public ObjectInput deserialize(URL url, InputStream is) throws IOException {
        return new JavaObjectInput(is);
    }
Copy the code

You can see that this wraps the data stream through the JavaObjectInput object provided by the JDK. Other serialization methods are similar, and you can analyze them yourself.

6. Summary

In this section, we mainly studied Dubbo serialization, and we also analyzed the implementation principle of serialization in Dubbo. The underlying realization principle is to use our serialization and deserialization framework to operate on data objects. Meanwhile, we also introduce two methods with high performance for Java serialization, namely Kryo and FST.

The highlights of this lesson are as follows:

  1. Understand Dubbo serialization and deserialization

  2. See how serialization works

  3. Understand serialization framework performance

  4. Understand how serialization works

The author

Personally engaged in the financial industry, I have worked in chongqing’s first-class technical team of Yiji Pay, Sijian Technology and an online car hailing platform, and now I am working in a bank responsible for the construction of unified payment system. I have a strong interest in the financial industry. It also practices big data, data storage, automated integration and deployment, distributed microservices, responsive programming, and artificial intelligence. At the same time, he is also keen on technology sharing, creating public accounts and blog sites to share knowledge system. Concern public number: young IT male get latest technical article push!

Blog: Youngitman.tech