dubbo source code practice - example of serialize layer

Table of contents

1 serialize layer overview

2 Simple example of serialization

2.1 Screenshot of the project

2.2 The source code of the three classes

2.2.1 ABC is an entity class

2.2.2 TestSeriarsWrite serializes ABC objects into files

2.2.3 TestSeriarsRead reads ABC objects from the file

2.2.4 Running Results

3 Show an example of dynamically switching the serialization implementation class through the URL attribute

3.1 Principle Analysis

3.2 Object to file implementation class

3.3 Serialization implementation supported by dubbo

4 Summary

1 serialize layer overview

dubbo official architecture diagram: Framework Design | Apache Dubbo

  • serialize data serialization layer: some reusable tools, the extension interface is Serialization, ObjectInput, ObjectOutput, ThreadPool

The Serialize layer implements two functions:

1) Serialize the object into a disk file.

2) Read the object from the disk file.

There are two scenarios where objects in the JVM need to be serialized:

1) If you want to send an entity class object, you need to send it to another application.

2) I want to store an entity class object in a file.

2 Simple example of serialization

2.1 Screenshot of the project

ABC is an entity class, TestSeriarsWrite serializes ABC objects into files, and TestSeriarsRead reads ABC objects from files.

 

2.2 The source code of the three classes

2.2.1 ABC is an entity class

package org.example.dubbo.serialize;

import java.io.Serializable;

/** Entity class*/
public class ABC implements Serializable {
    private String a;
    private Integer b;
    private int c;
    public String getA() {
        return a;
    }
    public void setA(String a) {
        this.a = a;
    }
    public Integer getB() {
        return b;
    }
    public void setB(Integer b) {
        this.b = b;
    }
    public int getC() {
        return c;
    }
    public void setC(int c) {
        this.c = c;
    }
    @Override
    public String toString() {
        return "ABC{" +
                "a='" + a + '\'' +
                ", b=" + b +
                ", c=" + c +
                '}';
    }
}

2.2.2 TestSeriarsWrite serializes ABC objects into files

The location of the file needs to be modified to the location corresponding to your own machine.

The implementation of Hessian2Serialization can be replaced by other serialization classes supported by dubbo, such as ProtostuffSerialization.

The objectOutput.flushBuffer() method is not called, some serialized classes do not output, and the produced files have no content.

package org.example.dubbo.serialize;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.Serialization;
import org.apache.dubbo.common.serialize.hessian2.Hessian2Serialization;

import java.io.FileOutputStream;
import java.io.IOException;

/** object serialized into a file  */
public class TestSeriarsWrite {
    public static void main(String[] args) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\a\\abc.txt");
        Serialization serialization = new Hessian2Serialization();

        ABC abc = new ABC();
        abc.setA("aaa");
        abc.setB(10);
        abc.setC(20);

        ObjectOutput objectOutput = serialization.serialize(null, fileOutputStream);
        objectOutput.writeObject(abc);
        objectOutput.flushBuffer();
    }
}

2.2.3 TestSeriarsRead reads ABC objects from the file

package org.example.dubbo.serialize;
import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.Serialization;
import org.apache.dubbo.common.serialize.hessian2.Hessian2Serialization;

import java.io.FileInputStream;
import java.io.IOException;

/** load object from file */
public class TestSeriarsRead {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Serialization serialization = new Hessian2Serialization();
        FileInputStream fileInputStream = new FileInputStream("E:\\a\\abc.txt");
        ObjectInput deserialize = serialization.deserialize(null, fileInputStream);
        ABC abc = deserialize.readObject(ABC.class);
        System.out.println(abc);
    }
}

2.2.4 Running results

First run the TestSeriarsWrite class, and then run the TestSeriarsRead class, you will find that the object of the ABC class can be obtained from the file.

document content:

The result of running the TestSeriarsRead class:

3 Show an example of dynamically switching the serialization implementation class through the URL attribute

3.1 Principle Analysis

Through the dubbo source code, you can see that Serialization is an adaptive extension point, so that the ability to switch extensions (i.e. implementation classes) through the URL attribute can be realized.

Since the @Adaptive annotation does not specify a value, the parameter name in the url is the name of the interface. If it is written in camel case, such as XxxYyy, the url parameter is xxx.yyy.

3.2 Object to file implementation class

package org.example.dubbo.serialize;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.Serialization;

import java.io.FileOutputStream;
import java.io.IOException;

/** object serialized into a file  */
public class TestUrlSeriarsWrite {
    public static void main(String[] args) throws IOException {
        String serializtionName = "hessian2";
        if (args.length > 0) {
            serializtionName = args[0];
        }
        System.out.println("The serialization implementation class is:" + serializtionName);
        FileOutputStream fileOutputStream = new FileOutputStream("E:\\a\\abc.txt");

        //Get Adaptive Extensions
        Serialization adaptiveExtension = ExtensionLoader.getExtensionLoader(Serialization.class)
                .getAdaptiveExtension();

        ABC abc = new ABC();
        abc.setA("aaa");
        abc.setB(10);
        abc.setC(20);

        //Create URLs
        URLBuilder urlBuilder = new URLBuilder();
        urlBuilder.addParameter("serialization", serializtionName);
        URL url = urlBuilder.build();

        //Select the corresponding implementation class through the serialization parameter in the URL
        ObjectOutput objectOutput = adaptiveExtension.serialize(url, fileOutputStream);
        objectOutput.writeObject(abc);
        objectOutput.flushBuffer();
    }
}

Run the TestUrlSeriarsWrite class through the idea, and specify the name of the extended implementation class. Currently configured fastjson.

 

After running the program, you can see the corresponding file: E:\a\abc.txt. It is already in json format.

3.3 Serialization implementation supported by dubbo

You can refer to the file of the dubbo jar package: org.apache.dubbo.common.serialize.Serialization.

The name in front of the equal sign is the name of the extension, and the one on the right of the equal sign is the specific java implementation class.

4 Summary

This article describes the simple use of the dubbo serialize layer API. Please search online for the principle (the online description is more detailed, so I won’t make a copy.).

The next article will give an example of the use of the transport layer.

Tags: Java Dubbo source code

Posted by davidb on Tue, 03 Jan 2023 19:47:43 +0300