序列化扩展

序列化扩展

扩展说明

将对象转成字节流,用于网络传输,以及将字节流转为对象,用于在收到字节流数据后还原成对象。

扩展接口

  • org.apache.dubbo.common.serialize.Serialization
  • org.apache.dubbo.common.serialize.ObjectInput
  • org.apache.dubbo.common.serialize.ObjectOutput

扩展配置

  1. <!-- 协议的序列化方式 -->
  2. <dubbo:protocol serialization="xxx" />
  3. <!-- 缺省值设置,当<dubbo:protocol>没有配置serialization时,使用此配置 -->
  4. <dubbo:provider serialization="xxx" />

已知扩展

  • org.apache.dubbo.common.serialize.dubbo.DubboSerialization
  • org.apache.dubbo.common.serialize.hessian.Hessian2Serialization
  • org.apache.dubbo.common.serialize.java.JavaSerialization
  • org.apache.dubbo.common.serialize.java.CompactedJavaSerialization

扩展示例

Maven 项目结构:

  1. src
  2. |-main
  3. |-java
  4. |-com
  5. |-xxx
  6. |-XxxSerialization.java (实现Serialization接口)
  7. |-XxxObjectInput.java (实现ObjectInput接口)
  8. |-XxxObjectOutput.java (实现ObjectOutput接口)
  9. |-resources
  10. |-META-INF
  11. |-dubbo
  12. |-org.apache.dubbo.common.serialize.Serialization (纯文本文件,内容为:xxx=com.xxx.XxxSerialization)

XxxSerialization.java:

  1. package com.xxx;
  2. import org.apache.dubbo.common.serialize.Serialization;
  3. import org.apache.dubbo.common.serialize.ObjectInput;
  4. import org.apache.dubbo.common.serialize.ObjectOutput;
  5. public class XxxSerialization implements Serialization {
  6. public ObjectOutput serialize(Parameters parameters, OutputStream output) throws IOException {
  7. return new XxxObjectOutput(output);
  8. }
  9. public ObjectInput deserialize(Parameters parameters, InputStream input) throws IOException {
  10. return new XxxObjectInput(input);
  11. }
  12. }

META-INF/dubbo/org.apache.dubbo.common.serialize.Serialization:

  1. xxx=com.xxx.XxxSerialization

最后修改 March 2, 2023: Fix alias forward in docs3-v2 (#2342) (f16c1535592)