[TOC]
1. Google protocol buffer 使用
1.1 安装过程
-
方式一:https://github.com/protocolbuffers/protobuf/releases 下载合适版本,如 protobuf-java-3.7.1.zip
-
Extract the zip file.
-
$cd protobuf-3.7.1
-
$./configure
-
$make
-
$make check
-
$sudo make install
-
$which protoc
-
$protoc --version
-
-
方式二:brew install安装
- brew tap homebrew/versions
- brew install protobuf371
- brew link --force --overwrite protobuf371 覆盖旧版本
- protoc --version
1.2 使用过程
1.2.1 本地 protoc 命令使用
protoc --java_out=输出java代码的目录 proto文件位置
1.2.2 idea中使用
<build>
<!--extensions是在build过程被激活的产品,os-maven-plugin 是设置各种有用属性(从 OS 中检测的 ${os.name} 和 ${os.arch} 属性)的 Maven 插件 -->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>
<!--os.detected.classifier 当前操作系统信息-->
com.google.protobuf:protoc:3.6.1:exe:${os.detected.classifier}
</protocArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Person.proto
syntax = "proto3";
option java_package = "com.zqh";
option java_outer_classname = "PersonModel";
message Person {
int32 id = 1;
string name = 2;
string email = 3;
}
执行插件编译,会在target目录下找到com.zqh包下找到PersonModel.java,其内容比较多,不再列出,序列化过程和反序列过程如下:
public static void main(String[] args) throws InvalidProtocolBufferException {
PersonModel.Person.Builder builder = PersonModel.Person.newBuilder();
builder.setId(1);
builder.setName("zhuqiuhui");
builder.setEmail("1003505819@qq.com");
PersonModel.Person person = builder.build();
System.out.println("before:" + person);
System.out.println("传输的字节(将protoc对象序列化成了字节流数据) Person Byte:");
for (byte b : person.toByteArray()) {
System.out.print(b);
}
System.out.println("将字节流数据反序列化成protoc对象:");
byte[] byteArray = person.toByteArray();
PersonModel.Person p2 = PersonModel.Person.parseFrom(byteArray);
System.out.println("序列化后的ID:" + p2.getId());
System.out.println("序列化后的name:" + p2.getName());
System.out.println("序列化后的email:" + p2.getEmail());
}
测试输出如下:
before:id: 1
name: "zhuqiuhui"
email: "1003505819@qq.com"
传输的字节(将protoc对象序列化成了字节流数据) Person Byte:
81189122104117113105117104117105261749484851534853564957641131134699111109将字节流数据反序列化成protoc对象:
序列化后的ID:1
序列化后的name:zhuqiuhui
序列化后的email:1003505819@qq.com
2. Google protocol buffer 接入dubbo
Google protocol buffer 已经2018-10-26日已经接入了dubbo,与Hessian2序列化协议接入类似,dubbo项目中集成了protostuff 源码,参考:https://github.com/apache/incubator-dubbo,核心代码如下:
public void writeObject(Object obj) throws IOException {
byte[] bytes;
byte[] classNameBytes;
try {
if (obj == null || WrapperUtils.needWrapper(obj)) {
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
Wrapper wrapper = new Wrapper(obj);
bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer);
classNameBytes = Wrapper.class.getName().getBytes();
} else {
Schema schema = RuntimeSchema.getSchema(obj.getClass());
bytes = GraphIOUtil.toByteArray(obj, schema, buffer);
classNameBytes = obj.getClass().getName().getBytes();
}
} finally {
buffer.clear();
}
dos.writeInt(classNameBytes.length);
dos.writeInt(bytes.length);
dos.write(classNameBytes);
dos.write(bytes);
}
public Object readObject() throws IOException, ClassNotFoundException {
int classNameLength = dis.readInt();
int bytesLength = dis.readInt();
if (classNameLength < 0 || bytesLength < 0) {
throw new IOException();
}
byte[] classNameBytes = new byte[classNameLength];
dis.readFully(classNameBytes, 0, classNameLength);
byte[] bytes = new byte[bytesLength];
dis.readFully(bytes, 0, bytesLength);
String className = new String(classNameBytes);
Class clazz = Class.forName(className);
Object result;
if (WrapperUtils.needWrapper(clazz)) {
Schema<Wrapper> schema = RuntimeSchema.getSchema(Wrapper.class);
Wrapper wrapper = schema.newMessage();
GraphIOUtil.mergeFrom(bytes, wrapper, schema);
result = wrapper.getData();
} else {
Schema schema = RuntimeSchema.getSchema(clazz);
result = schema.newMessage();
GraphIOUtil.mergeFrom(bytes, result, schema);
}
return result;
}
网友评论