[关闭]
@Yano 2018-11-24T14:46:10.000000Z 字数 3853 阅读 3250

Thrift 对象序列化、反序列化-字节数组分析

Java


说明

本篇博客仅分析Thrift对象的序列化、反序列化的字节数组,以及Thrift对象的序列化、反序列化原理。其他源码分析会另开章节~

准备工作

定义一个 Thrift 文件

  1. struct Person {
  2. 1: required i32 age;
  3. 2: required string name;
  4. }

生成 Java 代码

  1. thrift -r --gen java test.thrift

测试代码

  1. @Test
  2. public void testPerson() throws TException {
  3. Person person = new Person().setAge(18).setName("yano");
  4. System.out.println(person);
  5. TSerializer serializer = new TSerializer();
  6. byte[] bytes = serializer.serialize(person);
  7. System.out.println(Arrays.toString(bytes));
  8. Person parsePerson = new Person();
  9. TDeserializer deserializer = new TDeserializer();
  10. deserializer.deserialize(parsePerson, bytes);
  11. System.out.println(parsePerson);
  12. }

输出结果

  1. com.yano.nankai.spring.thrift.Person(age:18, name:yano)
  2. [8, 0, 1, 0, 0, 0, 18, 11, 0, 2, 0, 0, 0, 4, 121, 97, 110, 111, 0]
  3. com.yano.nankai.spring.thrift.Person(age:18, name:yano)

序列化过程

上述测试用例首先新建了Person对象,这个对象只有两个field。接着调用Thrift的TSerializer对person对象进行序列化。

其生成的字节数组为:

  1. [8, 0, 1, 0, 0, 0, 18, 11, 0, 2, 0, 0, 0, 4, 121, 97, 110, 111, 0]

TSerializer类的serialize方法如下,最终是调用了person对象的write方法。

  1. public byte[] serialize(TBase base) throws TException {
  2. this.baos_.reset();
  3. base.write(this.protocol_);
  4. return this.baos_.toByteArray();
  5. }

Person类的write方法:

  1. public void write(TProtocol oprot) throws TException {
  2. validate();
  3. oprot.writeStructBegin(STRUCT_DESC);
  4. oprot.writeFieldBegin(AGE_FIELD_DESC);
  5. oprot.writeI32(this.age);
  6. oprot.writeFieldEnd();
  7. if (this.name != null) {
  8. oprot.writeFieldBegin(NAME_FIELD_DESC);
  9. oprot.writeString(this.name);
  10. oprot.writeFieldEnd();
  11. }
  12. oprot.writeFieldStop();
  13. oprot.writeStructEnd();
  14. }

其中TProtocol默认为TBinaryProtocol,writeStructBegin()和writeStructEnd()方法为空。

oprot.writeFieldBegin(AGE_FIELD_DESC);

TBinaryProtocol 中的具体实现为:

  1. public void writeFieldBegin(TField field) throws TException {
  2. this.writeByte(field.type);
  3. this.writeI16(field.id);
  4. }

可以看到,首先是将字节数组写入了一个byte表示该字段的类型,而这里的TFiled AGE_FIELD_DESC 为:

  1. private static final TField AGE_FIELD_DESC = new TField("age", TType.I32, (short)1);

在thrift中定义的第一个字段为:

  1. 1: required i32 age;

其中TType的定义如下:

  1. public final class TType {
  2. public static final byte STOP = 0;
  3. public static final byte VOID = 1;
  4. public static final byte BOOL = 2;
  5. public static final byte BYTE = 3;
  6. public static final byte DOUBLE = 4;
  7. public static final byte I16 = 6;
  8. public static final byte I32 = 8;
  9. public static final byte I64 = 10;
  10. public static final byte STRING = 11;
  11. public static final byte STRUCT = 12;
  12. public static final byte MAP = 13;
  13. public static final byte SET = 14;
  14. public static final byte LIST = 15;
  15. public static final byte ENUM = 16;
  16. public TType() {
  17. }
  18. }

那么字节数组的第一个元素就是i32这个类型,为8。

接下来会写入这个字段所定义的id,age字段的id为1(注意这里是占两个字节),所以字节数组接下来的两个元素是 0,1。

对于name字段也是同理。

输出的字节数组每个值所代表的含义:

  1. 8 // 数据类型为i32
  2. 0, 1 // 字段id为1
  3. 0, 0, 0, 18 // 字段id为1(age)的值,占4个字节
  4. 11 // 数据类型为string
  5. 0, 2 // 字段id为2(name)
  6. 0, 0, 0, 4 // 字符串name的长度,占4个字节
  7. 121, 97, 110, 111 // "yano"的4个ASCII码(其实是UTF-8编码)
  8. 0 // 结束

反序列化过程

其反序列化的语句为:

  1. Person parsePerson = new Person();
  2. TDeserializer deserializer = new TDeserializer();
  3. deserializer.deserialize(parsePerson, bytes);

Person类的read函数:

  1. public void read(TProtocol iprot) throws TException {
  2. TField field;
  3. iprot.readStructBegin();
  4. while (true)
  5. {
  6. field = iprot.readFieldBegin();
  7. if (field.type == TType.STOP) {
  8. break;
  9. }
  10. switch (field.id) {
  11. case 1: // AGE
  12. if (field.type == TType.I32) {
  13. this.age = iprot.readI32();
  14. setAgeIsSet(true);
  15. } else {
  16. TProtocolUtil.skip(iprot, field.type);
  17. }
  18. break;
  19. case 2: // NAME
  20. if (field.type == TType.STRING) {
  21. this.name = iprot.readString();
  22. } else {
  23. TProtocolUtil.skip(iprot, field.type);
  24. }
  25. break;
  26. default:
  27. TProtocolUtil.skip(iprot, field.type);
  28. }
  29. iprot.readFieldEnd();
  30. }
  31. iprot.readStructEnd();
  32. // check for required fields of primitive type, which can't be checked in the validate method
  33. if (!isSetAge()) {
  34. throw new TProtocolException("Required field 'age' was not found in serialized data! Struct: " + toString());
  35. }
  36. validate();
  37. }

其代码也很简单清晰,先在字节数组中读取TField(5个字节,1字节类型+4字节id),接着根据id将其赋值给对应的字段。

其中有很多细节,就不一一介绍了。我写得也不如源码清楚。

与 Google Protocol Buffers 的对比

我曾经分析过Google Protocol Buffers 的序列化字节码,Google Protocol Buffers 序列化算法分析。感觉两者在序列化字节数组方面实现差别还是挺大的:

  1. Thrift的字节码并不紧凑,比如每个字段的id占4个字节,类型占1个字节;而Google Protocol Buffers的字段id和类型占同一个字节,而且对于i32等类型还会使用varint减少数组长度。
  2. Thrift生成的Java代码很简洁,代码实现也很简洁;Google Protocol Buffers生成的Java代码动不动就几千行……
  3. Thrift不单单是一个序列化协议,更是一个rpc调用框架;从这方面来说,Google Protocol Buffers是完全做不到的。
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注