文章目錄
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("[email protected]");
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: "[email protected]"
傳輸的字節(將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;
}