Skip to content

序列化协议 ​

深入理解Dubbo序列化协议的实现,掌握编解码器和压缩算法的原理,理解Hessian、Kryo、Protobuf等序列化方式,掌握序列化优化技巧和兼容性处理。

📋 序列化协议概览 ​

序列化是RPC通信的基础,Dubbo支持多种序列化协议,每种协议都有其特点和应用场景。

🎯 一、序列化接口 ​

1.1 Serialization接口 ​

Serialization是Dubbo序列化的核心接口:

java
@SPI("hessian2")
public interface Serialization {
    byte getContentTypeId();
    String getContentType();
    ObjectOutput serialize(URL url, OutputStream output) throws IOException;
    ObjectInput deserialize(URL url, InputStream input) throws IOException;
}

核心方法:

  • serialize:序列化对象到输出流
  • deserialize:从输入流反序列化对象

1.2 ObjectOutput和ObjectInput ​

ObjectOutput和ObjectInput定义了序列化的读写接口:

java
public interface ObjectOutput extends DataOutput {
    void writeObject(Object obj) throws IOException;
}

public interface ObjectInput extends DataInput {
    Object readObject() throws IOException, ClassNotFoundException;
    <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException;
}

🎯 二、Hessian2序列化 ​

2.1 Hessian2特点 ​

Hessian2是Dubbo的默认序列化协议:

  • 兼容性好:支持多种Java类型
  • 性能中等:序列化性能较好
  • 跨语言:支持多种编程语言

2.2 Hessian2实现 ​

Hessian2Serialization:

java
public class Hessian2Serialization implements Serialization {
    
    @Override
    public byte getContentTypeId() {
        return HESSIAN2_SERIALIZATION_ID;
    }
    
    @Override
    public String getContentType() {
        return "x-application/hessian2";
    }
    
    @Override
    public ObjectOutput serialize(URL url, OutputStream out) throws IOException {
        return new Hessian2ObjectOutput(out);
    }
    
    @Override
    public ObjectInput deserialize(URL url, InputStream is) throws IOException {
        return new Hessian2ObjectInput(is);
    }
}

🎯 三、Kryo序列化 ​

3.1 Kryo特点 ​

Kryo是高性能序列化框架:

  • 性能高:序列化速度快,体积小
  • 需要注册:需要注册类以提高性能
  • 兼容性一般:对Java类型支持有限

3.2 Kryo实现 ​

KryoSerialization:

java
public class KryoSerialization implements Serialization {
    
    @Override
    public byte getContentTypeId() {
        return KRYO_SERIALIZATION_ID;
    }
    
    @Override
    public String getContentType() {
        return "x-application/kryo";
    }
    
    @Override
    public ObjectOutput serialize(URL url, OutputStream out) throws IOException {
        return new KryoObjectOutput(out);
    }
    
    @Override
    public ObjectInput deserialize(URL url, InputStream is) throws IOException {
        return new KryoObjectInput(is);
    }
}

🎯 四、Protobuf序列化 ​

4.1 Protobuf特点 ​

Protobuf是Google的序列化协议:

  • 跨语言:支持多种编程语言
  • 体积小:序列化后体积小
  • 性能高:序列化性能优秀

4.2 Protobuf实现 ​

ProtobufSerialization:

java
public class ProtobufSerialization implements Serialization {
    
    @Override
    public byte getContentTypeId() {
        return PROTOBUF_SERIALIZATION_ID;
    }
    
    @Override
    public String getContentType() {
        return "x-application/protobuf";
    }
    
    @Override
    public ObjectOutput serialize(URL url, OutputStream out) throws IOException {
        return new ProtobufObjectOutput(out);
    }
    
    @Override
    public ObjectInput deserialize(URL url, InputStream is) throws IOException {
        return new ProtobufObjectInput(is);
    }
}

🎯 五、编解码器 ​

5.1 Codec接口 ​

Codec定义了编解码的接口:

java
public interface Codec {
    void encode(Channel channel, OutputStream output, Object message) 
            throws IOException;
    Object decode(Channel channel, InputStream input) throws IOException;
}

5.2 DubboCodec ​

DubboCodec是Dubbo的编解码器实现:

java
public class DubboCodec extends ExchangeCodec implements Codec2 {
    
    @Override
    protected void encodeRequestData(Channel channel, ObjectOutput out, 
            Object data) throws IOException {
        RpcInvocation inv = (RpcInvocation) data;
        out.writeUTF(inv.getAttachment(Constants.DUBBO_VERSION_KEY, 
                Version.getProtocolVersion()));
        out.writeUTF(inv.getAttachment(Constants.PATH_KEY));
        out.writeUTF(inv.getAttachment(Constants.VERSION_KEY));
        out.writeUTF(inv.getMethodName());
        out.writeUTF(inv.getParameterTypesDesc());
        Object[] args = inv.getArguments();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                out.writeObject(args[i]);
            }
        }
        out.writeObject(inv.getAttachments());
    }
}

📖 学习资源 ​

视频教程 ​

书籍推荐 ​

  • 《深度剖析Apache Dubbo核心技术内幕》 - 华仔
  • 《分布式系统常用技术及案例分析》 - 柳伟卫

网站资源 ​

✅ 学习检查清单 ​

  • [ ] 已理解序列化接口
  • [ ] 已理解Hessian2序列化
  • [ ] 已理解Kryo序列化
  • [ ] 已理解Protobuf序列化
  • [ ] 已理解编解码器实现
  • [ ] 已理解序列化优化技巧
  • [ ] 已总结核心知识点

上一章节:容错机制
下一章节:网络通信
返回目录:Dubbo源码深度解析