JAVARPC(二)序列化协议杂谈

JAVARPC(⼆)序列化协议杂谈
序列化和反序列化作为Java⾥⼀个较为基础的知识点,⼤家⼼⾥也有那么⼏句要说的,但我相信很多⼩伙伴掌握的也就是那么⼏句⽽已,如果再深究问⼀下Java如何实现序列化和反序列化的,就可能不知所措了!遥记当年也被问了这⼀个问题,⾃信满满的说了⼀⼤堆,什么是序列化、什么是反序列化、什么场景的时候才会⽤到等,然后⾯试官说:那你能说⼀下序列化和反序列化底层是如何实现的吗?⼀脸懵逼,然后回家等通知!
1、什么是序列化和反序列化
(1)Java序列化是指把Java对象转换为字节序列的过程,⽽Java反序列化是指把字节序列恢复为Java对象的过程;
(2)序列化:对象序列化的最主要的⽤处就是在传递和保存对象的时候,保证对象的完整性和可传递性。序列化是把对象转换成有序字节流,以便在⽹络上传输或者保存在本地⽂件中。序列化后的字节流保存了Java对象的状态以及相关的描述信息。序列化机制的核⼼作⽤就是对象状态的保存与重建。
(3)反序列化:客户端从⽂件中或⽹络上获得序列化后的对象字节流后,根据字节流中所保存的对象状态及描述信息,通过反序列化重建对象。
(4)本质上讲,序列化就是把实体对象状态按照⼀定的格式写⼊到有序字节流,反序列化就是从有序字节流重建对象,恢复对象状态。
2、为什么需要序列化与反序列化
我们知道,当两个进程进⾏远程通信时,可以相互发送各种类型的数据,包括⽂本、图⽚、⾳频、视频等,⽽这些数据都会以⼆进制序列的形式在⽹络上传送。
那么当两个Java进程进⾏通信时,能否实现进程间的对象传送呢?答案是可以的!如何做到呢?这就需要Java序列化与反序列化了!
换句话说,⼀⽅⾯,发送⽅需要把这个Java对象转换为字节序列,然后在⽹络上传送;另⼀⽅⾯,接收⽅需要从字节序列中恢复出Java对象。
当我们明晰了为什么需要Java序列化和反序列化后,我们很⾃然地会想Java序列化的好处。其好处⼀是实现了数据的持久化,通过序列化可以把数据永久地保存到硬盘上(通常存放在⽂件⾥),⼆是,利⽤序列化实现远程通信,即在⽹络上传送对象的字节序列。
总的来说可以归结为以下⼏点:
(1)永久性保存对象,保存对象的字节序列到本地⽂件或者数据库中;
(2)通过序列化以字节流的形式使对象在⽹络中进⾏传递和接收;
(3)通过序列化在进程间传递对象;
3、序列化算法⼀般会按步骤做如下事情:
(1)将对象实例相关的类元数据输出。
(2)递归地输出类的超类描述直到不再有超类。
(3)类元数据完了以后,开始从最顶层的超类开始输出对象实例的实际数据值。
(4)从上⾄下递归输出实例的数据
4、各种 Java 的序列化库的性能⽐较测试结果
专门针对Java语⾔的:Kryo,FST等等
跨语⾔的:Protostuff,ProtoBuf,Thrift,Avro,MsgPack等等
图⽚来源于⽹络,可以说明对原⽣序列化来讲,其他三⽅框架提供的序列化协议要快很多,所以我们做RPC技术选型的时候,序列化协议这块⼀定要摒弃原⽣序列化,去选择⼀款⾃⼰熟悉的序列化协议来传输IO流。下⾯选择做⼀个thrift序列化和原⽣序列化的对⽐结果。
原⽣序列化user类
package util.dto;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private Integer integer;
private String address;
public User(String name, Integer integer, String address) {
this.name = name;
this.integer = integer;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getInteger() {
return integer;
}
public void setInteger(Integer integer) {
this.integer = integer;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
原⽣序列化⼯具类
1package util;
2
3import java.io.*;
4import java.util.Arrays;
5
6public class SerializeUtil {
7
8/** 序列化对象
9    * @throws IOException */
10public static byte[] serializeObject(Object object) throws IOException {
11        ByteArrayOutputStream saos = new ByteArrayOutputStream ();
12        ObjectOutputStream oos = new ObjectOutputStream(saos);
13        oos.writeObject(object);
14        oos.flush();
ByteArray();
16    }
17
18/** 反序列化对象
19    * @throws IOException
20    * @throws ClassNotFoundException */
21public static Object deserializeObject(byte[] buf) throws IOException, ClassNotFoundException{
22        Object object=null;
23        ByteArrayInputStream sais=new ByteArrayInputStream(buf);
24        ObjectInputStream ois = new ObjectInputStream(sais);
25        object = adObject();
26return object;
27    }
28 }
由thrift脚本⽣成的User类
1/**
2 * Autogenerated by Thrift Compiler (0.8.0)
3 *
4 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5 *  @generated
6*/
7package util.dtothrift;
8
9import org.apache.thrift.scheme.IScheme;
10import org.apache.thrift.scheme.SchemeFactory;
11import org.apache.thrift.scheme.StandardScheme;
12
13import org.apache.thrift.scheme.TupleScheme;
14import org.apache.thrift.protocol.TTupleProtocol;
15import java.util.List;
16import java.util.ArrayList;
17import java.util.Map;
18import java.util.HashMap;
19import java.util.EnumMap;
20import java.util.Set;
开口料
21import java.util.HashSet;
22import java.util.EnumSet;
23import java.util.Collections;
24import java.util.BitSet;
25import java.nio.ByteBuffer;
26import java.util.Arrays;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30public class User implements org.apache.thrift.TBase<User, User._Fields>, java.io.Serializable, Cloneable {
31private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("User");
32
33private static final org.apache.thrift.protocol.TField STR_FIELD_DESC = new org.apache.thrift.protocol.TField("str", org.apache.thrift.protocol.TType.STRING, (short)1);
34private static final org.apache.thrift.protocol.TField AGE_FIELD_DESC = new org.apache.thrift.protocol.TField("age", org.apache.thrift.protocol.TType.I32, (short)2);
35private static final org.apache.thrift.protocol.TField ADDRESS_FIELD_DESC = new org.apache.thrift.protocol.TField("address", org.apache.thrift.protocol.TType.STRING, (short)3); 36
37private static final Map<Class<? extends IScheme>, SchemeFactory> schemes = new HashMap<Class<? extends IScheme>, SchemeFactory>();
38static {
39    schemes.put(StandardScheme.class, new UserStandardSchemeFactory());
40    schemes.put(TupleScheme.class, new UserTupleSchemeFactory());
41  }
42
43public String str; // required
44public int age; // required
45public String address; // required
46
47/** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
48public enum _Fields implements org.apache.thrift.TFieldIdEnum {
49    STR((short)1, "str"),
50    AGE((short)2, "age"),
51    ADDRESS((short)3, "address");
52
53private static final Map<String, _Fields> byName = new HashMap<String, _Fields>();
54
55static {
56for (_Fields field : EnumSet.allOf(_Fields.class)) {
57        byName.FieldName(), field);
58      }
59    }
60
61/**
62    * Find the _Fields constant that matches fieldId, or null if its not found.
63*/
64public static _Fields findByThriftId(int fieldId) {
65switch(fieldId) {
66case 1: // STR
67return STR;
68case 2: // AGE
69return AGE;
70case 3: // ADDRESS
71return ADDRESS;
72default:
73return null;
74      }
75    }
76
77/**质粒拯救
78    * Find the _Fields constant that matches fieldId, throwing an exception
79    * if it is not found.
80*/
81public static _Fields findByThriftIdOrThrow(int fieldId) {
82      _Fields fields = findByThriftId(fieldId);
83if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!");
84return fields;
85    }
86
87/**
88    * Find the _Fields constant that matches name, or null if its not found.
89*/
90public static _Fields findByName(String name) {
(name);
92    }
93
94private final short _thriftId;
95private final String _fieldName;
96
97    _Fields(short thriftId, String fieldName) {
98      _thriftId = thriftId;
99      _fieldName = fieldName;
100    }
101
102public short getThriftFieldId() {
103return _thriftId;
104    }
105
106public String getFieldName() {
107return _fieldName;
108    }
109  }
110
111// isset id assignments
112private static final int __AGE_ISSET_ID = 0;
113private BitSet __isset_bit_vector = new BitSet(1);
114public static final Map<_Fields, org.a_data.FieldMetaData> metaDataMap;
115static {
116    Map<_Fields, org.a_data.FieldMetaData> tmpMap = new EnumMap<_Fields, org.a_data.FieldMetaData>(_Fields.class); 117    tmpMap.put(_Fields.STR, new org.a_data.FieldMetaData("str", org.apache.thrift.TFieldRequirementType.DEFAULT,
118new org.a_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));
119    tmpMap.put(_Fields.AGE, new org.a_data.FieldMetaData("age", org.apache.thrift.TFieldRequirementType.DEFAULT,
120new org.a_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32)));
121    tmpMap.put(_Fields.ADDRESS, new org.a_data.FieldMetaData("address", org.apache.thrift.TFieldRequirementType.DEFAULT,
122new org.a_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));
123    metaDataMap = Collections.unmodifiableMap(tmpMap);
124    org.a_data.FieldMetaData.addStructMetaDataMap(User.class, metaDataMap);
125  }
126
127public User() {
128  }
129
130public User(
131    String str,
132int age,
133    String address)
134  {
135this();
136this.str = str;
137this.age = age;
智能商用豆浆机138    setAgeIsSet(true);
139this.address = address;
140  }
141
142/**
143  * Performs a deep copy on <i>other</i>.
144*/
145public User(User other) {
146    __isset_bit_vector.clear();
147    __isset_(other.__isset_bit_vector);
148if (other.isSetStr()) {
149this.str = other.str;
150    }
151this.age = other.age;
152if (other.isSetAddress()) {
153this.address = other.address;
154    }
155  }
156
157public User deepCopy() {
158return new User(this);
159  }
160
161  @Override
162public void clear() {
163this.str = null;
164    setAgeIsSet(false);
165this.age = 0;
166this.address = null;
167  }
168
169public String getStr() {
170return this.str;
171  }
172
173public User setStr(String str) {
174this.str = str;
175return this;
176  }
177
178public void unsetStr() {
179this.str = null;
180  }
181
182/** Returns true if field str is set (has been assigned a value) and false otherwise */
183public boolean isSetStr() {
184return this.str != null;
185  }
186
187public void setStrIsSet(boolean value) {
188if (!value) {
189this.str = null;
190    }
191  }
192
193public int getAge() {
194return this.age;
195  }
196
197public User setAge(int age) {
198this.age = age;
199    setAgeIsSet(true);
200return this;
201  }
202
203public void unsetAge() {
204    __isset_bit_vector.clear(__AGE_ISSET_ID);
205  }
206
207/** Returns true if field age is set (has been assigned a value) and false otherwise */
208public boolean isSetAge() {
209return __isset_(__AGE_ISSET_ID);
210  }
211
212public void setAgeIsSet(boolean value) {
213    __isset_bit_vector.set(__AGE_ISSET_ID, value);
214  }
215
216public String getAddress() {
217return this.address;
218  }
219
220public User setAddress(String address) {
221this.address = address;
222return this;
223  }
224
225public void unsetAddress() {
226this.address = null;
227  }
228
229/** Returns true if field address is set (has been assigned a value) and false otherwise */
230public boolean isSetAddress() {
231return this.address != null;
232  }
233
234public void setAddressIsSet(boolean value) {
235if (!value) {
236this.address = null;
237    }
238  }
239
保险箱密码锁
240public void setFieldValue(_Fields field, Object value) {
241switch (field) {
242case STR:
243if (value == null) {
244        unsetStr();
245      } else {
246        setStr((String)value);
247      }
248break;
249
250case AGE:
251if (value == null) {
252        unsetAge();
253      } else {
254        setAge((Integer)value);
255      }
256break;
257
258case ADDRESS:
259if (value == null) {
260        unsetAddress();
261      } else {
262        setAddress((String)value);
263      }
264break;
265
266    }
267  }
268
269public Object getFieldValue(_Fields field) {
270switch (field) {
271case STR:
272return getStr();
273
274case AGE:
275return Integer.valueOf(getAge());
276
277case ADDRESS:
278return getAddress();
279
280    }
281throw new IllegalStateException();
282  }
283
284/** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */ 285public boolean isSet(_Fields field) {
286if (field == null) {
287throw new IllegalArgumentException();
288    }
289
290switch (field) {
291case STR:
292return isSetStr();
293case AGE:
294return isSetAge();
295case ADDRESS:
296return isSetAddress();
297    }
298throw new IllegalStateException();
299  }
300
301  @Override
302public boolean equals(Object that) {
303if (that == null)
304return false;
305if (that instanceof User)
306return this.equals((User)that);
307return false;
308  }
309
310public boolean equals(User that) {
311if (that == null)
312return false;
313
314boolean this_present_str = true && this.isSetStr();
315boolean that_present_str = true && that.isSetStr();
316if (this_present_str || that_present_str) {
317if (!(this_present_str && that_present_str))
318return false;
319if (!this.str.equals(that.str))
320return false;
321    }
322
323boolean this_present_age = true;
324boolean that_present_age = true;
325if (this_present_age || that_present_age) {
326if (!(this_present_age && that_present_age))
327return false;
328if (this.age != that.age)
329return false;
330    }
331
332boolean this_present_address = true && this.isSetAddress();
333boolean that_present_address = true && that.isSetAddress();
334if (this_present_address || that_present_address) {
335if (!(this_present_address && that_present_address))
336return false;
337if (!this.address.equals(that.address))
338return false;
339    }
340
341return true;
342  }
343
344  @Override
345public int hashCode() {
346return 0;
347  }
348
cnnp349public int compareTo(User other) {
350if (!getClass().Class())) {
351return getClass().getName()Class().getName());
352    }
353
354int lastComparison = 0;
355    User typedOther = (User)other;
356
357    lastComparison = Boolean.valueOf(isSetStr())pareTo(typedOther.isSetStr());
358if (lastComparison != 0) {
359return lastComparison;
360    }
361if (isSetStr()) {
362      lastComparison = org.apache.thrift.TBaseHelperpareTo(this.str, typedOther.str);
363if (lastComparison != 0) {
364return lastComparison;
365      }
366    }
367    lastComparison = Boolean.valueOf(isSetAge())pareTo(typedOther.isSetAge());
368if (lastComparison != 0) {
369return lastComparison;
370    }
371if (isSetAge()) {
372      lastComparison = org.apache.thrift.TBaseHelperpareTo(this.age, typedOther.age);
373if (lastComparison != 0) {
374return lastComparison;
375      }
376    }
377    lastComparison = Boolean.valueOf(isSetAddress())pareTo(typedOther.isSetAddress());
378if (lastComparison != 0) {
379return lastComparison;
380    }
381if (isSetAddress()) {
382      lastComparison = org.apache.thrift.TBaseHelperpareTo(this.address, typedOther.address);
383if (lastComparison != 0) {
384return lastComparison;
385      }
386    }
387return 0;
388  }
389
390public _Fields fieldForId(int fieldId) {
391return _Fields.findByThriftId(fieldId);
392  }
393
394public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
395    (Scheme()).getScheme().read(iprot, this);
396  }
397
398public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
399    (Scheme()).getScheme().write(oprot, this);
400  }
401
402  @Override
403public String toString() {
404    StringBuilder sb = new StringBuilder("User(");
405boolean first = true;
406
407    sb.append("str:");
408if (this.str == null) {
409      sb.append("null");
410    } else {
411      sb.append(this.str);
412    }
413    first = false;
414if (!first) sb.append(", ");
415    sb.append("age:");
416    sb.append(this.age);
417    first = false;
418if (!first) sb.append(", ");
419    sb.append("address:");
420if (this.address == null) {
421      sb.append("null");
422    } else {
423      sb.append(this.address);
424    }
425    first = false;
426    sb.append(")");
String();
428  }
429
430public void validate() throws org.apache.thrift.TException {
431// check for required fields
432  }调节臂
433
434private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
435try {
436      write(new org.apache.thrift.protocol.TCompactProtocol(new org.ansport.TIOStreamTransport(out))); 437    } catch (org.apache.thrift.TException te) {
438throw new java.io.IOException(te);

本文发布于:2024-09-22 01:38:13,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/265731.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:序列化   对象   字节   保存
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议