在微服务架构中,网络带宽成为决定并发的主要因素之一。对象的传递在不同服务直接很常见,对象的传递要经过序列化和反序列化。
序列化:将对象转换成二进制流的过程,称为序列化
反序列化:将二进制流转换成对象的过程,称为反序列化
那么,选择一种高效的序列化和反序列化方法,就可以提升整个架构的性能。
使用java原生方式实现序列化和反序列化:
public void javaObjByte(Person person) throws IOException, ClassNotFoundException {
long startTime = System.nanoTime();
// 序列化
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
out.writeObject(person);
byte[] personByte = os.toByteArray();
System.out.println("java length: " + personByte.length);
// 反序列化
ByteArrayInputStream is = new ByteArrayInputStream(personByte);
ObjectInputStream in = new ObjectInputStream(is);
Person zhangsan = (Person)in.readObject();
long endTime = System.nanoTime();
System.out.println("java: " + (endTime - startTime));
}
使用Hessian进行序列化和反序列化:
public void hessianObjByte(Person person) throws IOException {
long startTime = System.nanoTime();
// 序列化
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(person);
byte[] personByte = os.toByteArray();
System.out.println("hess length: " + personByte.length);
// 反序列化
ByteArrayInputStream is = new ByteArrayInputStream(personByte);
HessianInput hi = new HessianInput(is);
Person zhangsan = (Person)hi.readObject();
long endTime = System.nanoTime();
System.out.println("hess: " + (endTime - startTime));
}
Person类:
public class Person implements java.io.Serializable {
private static final long serialVersionUID = 1L;
String name;
Integer age;
Boolean sex;
Integer height;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", height=" + height + "]";
}
public Person() {
super();
}
public Person(String name, Integer age, Boolean sex, Integer height) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getSex() {
return sex;
}
public void setSex(Boolean sex) {
this.sex = sex;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
}
运行十次结果(Java虚拟机运行的纳秒数):
java length: 267
java: 64303484
hess length: 62
hess: 63746841
++++++++++++++++
java length: 267
java: 402568
hess length: 62
hess: 249284
++++++++++++++++
java length: 267
java: 464198
hess length: 62
hess: 213729
++++++++++++++++
java length: 267
java: 393087
hess length: 62
hess: 227951
++++++++++++++++
java length: 267
java: 495013
hess length: 62
hess: 165531
++++++++++++++++
java length: 267
java: 358717
hess length: 62
hess: 218074
++++++++++++++++
java length: 267
java: 496198
hess length: 62
hess: 175803
++++++++++++++++
java length: 267
java: 427457
hess length: 62
hess: 137086
++++++++++++++++
java length: 267
java: 501729
hess length: 62
hess: 180939
++++++++++++++++
java length: 267
java: 527803
hess length: 62
hess: 179358
++++++++++++++++
可以看到上面的运行结果,除了第一次受到Java虚拟机启动的影响,耗时几乎一样外,其余几次都是Hessian以绝对的优势领先Java内置的序列化方法,并且序列化后对象的体积也只有Java序列化的三分之一。这就意味着,采用hessian序列化的架构,在并发上可以增加两倍以上的流量。
hessian的jar版本为4.0.51
作者:秋名山车神
感言:希望能以此系列文章,记录自己探索T4的路。
T4工程师:腾讯内部的级别。T4专家组是腾讯最为核心的部门,每当项目遇到重大技术问题就会由T4专家组协助,如:QQ空间代码重构,微信摇一摇和春节抢红包亿级并发。代表了编程水平的最高境界,再往上的T5和T6已经不是单纯的技术积累能够做到的了。T4成为T6只有一条路,那就是用自己的技术创造出新的技术,改变人类,改变世界。
热门评论
赞!!!!!!!!!!!!