首先安装好Protocol Buffer的编译器
Protocol Buffer:
version:2.6.1
link: 链接直达
根据电脑环境进行下载,Widnwos 32/64位就选择win32是没问题的,楼主亲测
1.proto文件编写
Person.proto
public class Person {
String name;
int id;
String email;
}
syntax = "proto2";
option java_outer_classname="PersonPOJO";
message Person {
required string name = 1;
required int32 id = 2;
required string email = 3;
}
2.生成PersonProto文件
接下来我们要根据上面编写好的Person.proto
文件生成Java对应的Proto对象
运行指令
.\protoc.exe --java_out=. Person.proto
如果没有报错就是成功了,有的运行指令会报错
再仔细核对下上面的proto文件即可,这里采用的proto2版本进行生成的,一定要注意版本号的区别,我们下载的版本号就是2.6.1,不同的编译文件可能会有略微差异,但是不影响。
3.PersonPOJO.java
上面的JavaProto文件类名我指定成了PersonPOJO(也就是JavaProto对象),这里可以看到,里面生成了很多代码,而我们Person实体类属性才只有3个
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: Person.proto
public final class PersonPOJO {
private PersonPOJO() {}
public static void registerAllExtensions(
com.google.protobuf.ExtensionRegistry registry) {
}
public interface PersonOrBuilder extends
// @@protoc_insertion_point(interface_extends:Person)
com.google.protobuf.MessageOrBuilder {
/**
* <code>required string name = 1;</code>
*/
boolean hasName();
/**
* <code>required string name = 1;</code>
*/
java.lang.String getName();
/**
* <code>required string name = 1;</code>
*/
com.google.protobuf.ByteString
getNameBytes();
/**
* <code>required int32 id = 2;</code>
*/
boolean hasId();
/**
* <code>required int32 id = 2;</code>
*/
int getId();
/**
* <code>required string email = 3;</code>
*/
boolean hasEmail();
/**
* <code>required string email = 3;</code>
*/
java.lang.String getEmail();
/**
* <code>required string email = 3;</code>
*/
com.google.protobuf.ByteString
getEmailBytes();
}
/**
* Protobuf type {@code Person}
*/
public static final class Person extends
com.google.protobuf.GeneratedMessage implements
// @@protoc_insertion_point(message_implements:Person)
PersonOrBuilder {
// Use Person.newBuilder() to construct.
private Person(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
super(builder);
this.unknownFields = builder.getUnknownFields();
}
private Person(boolean noInit) { this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); }
private static final Person defaultInstance;
public static Person getDefaultInstance() {
return defaultInstance;
}
public Person getDefaultInstanceForType() {
return defaultInstance;
}
private final com.google.protobuf.UnknownFieldSet unknownFields;
@java.lang.Override
public final com.google.protobuf.UnknownFieldSet
getUnknownFields() {
return this.unknownFields;
}
private Person(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
initFields();
int mutable_bitField0_ = 0;
com.google.protobuf.UnknownFieldSet.Builder unknownFields =
com.google.protobuf.UnknownFieldSet.newBuilder();
try {
boolean done = false;
while (!done) {
int tag = input.readTag();
switch (tag) {
case 0:
done = true;
break;
default: {
if (!parseUnknownField(input, unknownFields,
extensionRegistry, tag)) {
done = true;
}
break;
}
case 10: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000001;
name_ = bs;
break;
}
case 16: {
bitField0_ |= 0x00000002;
id_ = input.readInt32();
break;
}
case 26: {
com.google.protobuf.ByteString bs = input.readBytes();
bitField0_ |= 0x00000004;
email_ = bs;
break;
}
}
}
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw e.setUnfinishedMessage(this);
} catch (java.io.IOException e) {
throw new com.google.protobuf.InvalidProtocolBufferException(
e.getMessage()).setUnfinishedMessage(this);
} finally {
this.unknownFields = unknownFields.build();
makeExtensionsImmutable();
}
}
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return PersonPOJO.internal_static_Person_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return PersonPOJO.internal_static_Person_fieldAccessorTable
.ensureFieldAccessorsInitialized(
PersonPOJO.Person.class, PersonPOJO.Person.Builder.class);
}
public static com.google.protobuf.Parser<Person> PARSER =
new com.google.protobuf.AbstractParser<Person>() {
public Person parsePartialFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return new Person(input, extensionRegistry);
}
};
@java.lang.Override
public com.google.protobuf.Parser<Person> getParserForType() {
return PARSER;
}
private int bitField0_;
public static final int NAME_FIELD_NUMBER = 1;
private java.lang.Object name_;
/**
* <code>required string name = 1;</code>
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required string name = 1;</code>
*/
public java.lang.String getName() {
java.lang.Object ref = name_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
name_ = s;
}
return s;
}
}
/**
* <code>required string name = 1;</code>
*/
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
public static final int ID_FIELD_NUMBER = 2;
private int id_;
/**
* <code>required int32 id = 2;</code>
*/
public boolean hasId() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>required int32 id = 2;</code>
*/
public int getId() {
return id_;
}
public static final int EMAIL_FIELD_NUMBER = 3;
private java.lang.Object email_;
/**
* <code>required string email = 3;</code>
*/
public boolean hasEmail() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>required string email = 3;</code>
*/
public java.lang.String getEmail() {
java.lang.Object ref = email_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
email_ = s;
}
return s;
}
}
/**
* <code>required string email = 3;</code>
*/
public com.google.protobuf.ByteString
getEmailBytes() {
java.lang.Object ref = email_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
email_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
private void initFields() {
name_ = "";
id_ = 0;
email_ = "";
}
private byte memoizedIsInitialized = -1;
public final boolean isInitialized() {
byte isInitialized = memoizedIsInitialized;
if (isInitialized == 1) return true;
if (isInitialized == 0) return false;
if (!hasName()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasId()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasEmail()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
}
public void writeTo(com.google.protobuf.CodedOutputStream output)
throws java.io.IOException {
getSerializedSize();
if (((bitField0_ & 0x00000001) == 0x00000001)) {
output.writeBytes(1, getNameBytes());
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
output.writeInt32(2, id_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
output.writeBytes(3, getEmailBytes());
}
getUnknownFields().writeTo(output);
}
private int memoizedSerializedSize = -1;
public int getSerializedSize() {
int size = memoizedSerializedSize;
if (size != -1) return size;
size = 0;
if (((bitField0_ & 0x00000001) == 0x00000001)) {
size += com.google.protobuf.CodedOutputStream
.computeBytesSize(1, getNameBytes());
}
if (((bitField0_ & 0x00000002) == 0x00000002)) {
size += com.google.protobuf.CodedOutputStream
.computeInt32Size(2, id_);
}
if (((bitField0_ & 0x00000004) == 0x00000004)) {
size += com.google.protobuf.CodedOutputStream
.computeBytesSize(3, getEmailBytes());
}
size += getUnknownFields().getSerializedSize();
memoizedSerializedSize = size;
return size;
}
private static final long serialVersionUID = 0L;
@java.lang.Override
protected java.lang.Object writeReplace()
throws java.io.ObjectStreamException {
return super.writeReplace();
}
public static PersonPOJO.Person parseFrom(
com.google.protobuf.ByteString data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static PersonPOJO.Person parseFrom(
com.google.protobuf.ByteString data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static PersonPOJO.Person parseFrom(byte[] data)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data);
}
public static PersonPOJO.Person parseFrom(
byte[] data,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws com.google.protobuf.InvalidProtocolBufferException {
return PARSER.parseFrom(data, extensionRegistry);
}
public static PersonPOJO.Person parseFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static PersonPOJO.Person parseFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static PersonPOJO.Person parseDelimitedFrom(java.io.InputStream input)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input);
}
public static PersonPOJO.Person parseDelimitedFrom(
java.io.InputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseDelimitedFrom(input, extensionRegistry);
}
public static PersonPOJO.Person parseFrom(
com.google.protobuf.CodedInputStream input)
throws java.io.IOException {
return PARSER.parseFrom(input);
}
public static PersonPOJO.Person parseFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
return PARSER.parseFrom(input, extensionRegistry);
}
public static Builder newBuilder() { return Builder.create(); }
public Builder newBuilderForType() { return newBuilder(); }
public static Builder newBuilder(PersonPOJO.Person prototype) {
return newBuilder().mergeFrom(prototype);
}
public Builder toBuilder() { return newBuilder(this); }
@java.lang.Override
protected Builder newBuilderForType(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
Builder builder = new Builder(parent);
return builder;
}
/**
* Protobuf type {@code Person}
*/
public static final class Builder extends
com.google.protobuf.GeneratedMessage.Builder<Builder> implements
// @@protoc_insertion_point(builder_implements:Person)
PersonPOJO.PersonOrBuilder {
public static final com.google.protobuf.Descriptors.Descriptor
getDescriptor() {
return PersonPOJO.internal_static_Person_descriptor;
}
protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
internalGetFieldAccessorTable() {
return PersonPOJO.internal_static_Person_fieldAccessorTable
.ensureFieldAccessorsInitialized(
PersonPOJO.Person.class, PersonPOJO.Person.Builder.class);
}
// Construct using PersonPOJO.Person.newBuilder()
private Builder() {
maybeForceBuilderInitialization();
}
private Builder(
com.google.protobuf.GeneratedMessage.BuilderParent parent) {
super(parent);
maybeForceBuilderInitialization();
}
private void maybeForceBuilderInitialization() {
if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
}
}
private static Builder create() {
return new Builder();
}
public Builder clear() {
super.clear();
name_ = "";
bitField0_ = (bitField0_ & ~0x00000001);
id_ = 0;
bitField0_ = (bitField0_ & ~0x00000002);
email_ = "";
bitField0_ = (bitField0_ & ~0x00000004);
return this;
}
public Builder clone() {
return create().mergeFrom(buildPartial());
}
public com.google.protobuf.Descriptors.Descriptor
getDescriptorForType() {
return PersonPOJO.internal_static_Person_descriptor;
}
public PersonPOJO.Person getDefaultInstanceForType() {
return PersonPOJO.Person.getDefaultInstance();
}
public PersonPOJO.Person build() {
PersonPOJO.Person result = buildPartial();
if (!result.isInitialized()) {
throw newUninitializedMessageException(result);
}
return result;
}
public PersonPOJO.Person buildPartial() {
PersonPOJO.Person result = new PersonPOJO.Person(this);
int from_bitField0_ = bitField0_;
int to_bitField0_ = 0;
if (((from_bitField0_ & 0x00000001) == 0x00000001)) {
to_bitField0_ |= 0x00000001;
}
result.name_ = name_;
if (((from_bitField0_ & 0x00000002) == 0x00000002)) {
to_bitField0_ |= 0x00000002;
}
result.id_ = id_;
if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
to_bitField0_ |= 0x00000004;
}
result.email_ = email_;
result.bitField0_ = to_bitField0_;
onBuilt();
return result;
}
public Builder mergeFrom(com.google.protobuf.Message other) {
if (other instanceof PersonPOJO.Person) {
return mergeFrom((PersonPOJO.Person)other);
} else {
super.mergeFrom(other);
return this;
}
}
public Builder mergeFrom(PersonPOJO.Person other) {
if (other == PersonPOJO.Person.getDefaultInstance()) return this;
if (other.hasName()) {
bitField0_ |= 0x00000001;
name_ = other.name_;
onChanged();
}
if (other.hasId()) {
setId(other.getId());
}
if (other.hasEmail()) {
bitField0_ |= 0x00000004;
email_ = other.email_;
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields());
return this;
}
public final boolean isInitialized() {
if (!hasName()) {
return false;
}
if (!hasId()) {
return false;
}
if (!hasEmail()) {
return false;
}
return true;
}
public Builder mergeFrom(
com.google.protobuf.CodedInputStream input,
com.google.protobuf.ExtensionRegistryLite extensionRegistry)
throws java.io.IOException {
PersonPOJO.Person parsedMessage = null;
try {
parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
parsedMessage = (PersonPOJO.Person) e.getUnfinishedMessage();
throw e;
} finally {
if (parsedMessage != null) {
mergeFrom(parsedMessage);
}
}
return this;
}
private int bitField0_;
private java.lang.Object name_ = "";
/**
* <code>required string name = 1;</code>
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required string name = 1;</code>
*/
public java.lang.String getName() {
java.lang.Object ref = name_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
name_ = s;
}
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>required string name = 1;</code>
*/
public com.google.protobuf.ByteString
getNameBytes() {
java.lang.Object ref = name_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
name_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string name = 1;</code>
*/
public Builder setName(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
name_ = value;
onChanged();
return this;
}
/**
* <code>required string name = 1;</code>
*/
public Builder clearName() {
bitField0_ = (bitField0_ & ~0x00000001);
name_ = getDefaultInstance().getName();
onChanged();
return this;
}
/**
* <code>required string name = 1;</code>
*/
public Builder setNameBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000001;
name_ = value;
onChanged();
return this;
}
private int id_ ;
/**
* <code>required int32 id = 2;</code>
*/
public boolean hasId() {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>required int32 id = 2;</code>
*/
public int getId() {
return id_;
}
/**
* <code>required int32 id = 2;</code>
*/
public Builder setId(int value) {
bitField0_ |= 0x00000002;
id_ = value;
onChanged();
return this;
}
/**
* <code>required int32 id = 2;</code>
*/
public Builder clearId() {
bitField0_ = (bitField0_ & ~0x00000002);
id_ = 0;
onChanged();
return this;
}
private java.lang.Object email_ = "";
/**
* <code>required string email = 3;</code>
*/
public boolean hasEmail() {
return ((bitField0_ & 0x00000004) == 0x00000004);
}
/**
* <code>required string email = 3;</code>
*/
public java.lang.String getEmail() {
java.lang.Object ref = email_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
if (bs.isValidUtf8()) {
email_ = s;
}
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <code>required string email = 3;</code>
*/
public com.google.protobuf.ByteString
getEmailBytes() {
java.lang.Object ref = email_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
email_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <code>required string email = 3;</code>
*/
public Builder setEmail(
java.lang.String value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
email_ = value;
onChanged();
return this;
}
/**
* <code>required string email = 3;</code>
*/
public Builder clearEmail() {
bitField0_ = (bitField0_ & ~0x00000004);
email_ = getDefaultInstance().getEmail();
onChanged();
return this;
}
/**
* <code>required string email = 3;</code>
*/
public Builder setEmailBytes(
com.google.protobuf.ByteString value) {
if (value == null) {
throw new NullPointerException();
}
bitField0_ |= 0x00000004;
email_ = value;
onChanged();
return this;
}
// @@protoc_insertion_point(builder_scope:Person)
}
static {
defaultInstance = new Person(true);
defaultInstance.initFields();
}
// @@protoc_insertion_point(class_scope:Person)
}
private static final com.google.protobuf.Descriptors.Descriptor
internal_static_Person_descriptor;
private static
com.google.protobuf.GeneratedMessage.FieldAccessorTable
internal_static_Person_fieldAccessorTable;
public static com.google.protobuf.Descriptors.FileDescriptor
getDescriptor() {
return descriptor;
}
private static com.google.protobuf.Descriptors.FileDescriptor
descriptor;
static {
java.lang.String[] descriptorData = {
"\n\014Person.proto\"1\n\006Person\022\014\n\004name\030\001 \002(\t\022\n" +
"\n\002id\030\002 \002(\005\022\r\n\005email\030\003 \002(\tB\014B\nPersonPOJO"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
public com.google.protobuf.ExtensionRegistry assignDescriptors(
com.google.protobuf.Descriptors.FileDescriptor root) {
descriptor = root;
return null;
}
};
com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData,
new com.google.protobuf.Descriptors.FileDescriptor[] {
}, assigner);
internal_static_Person_descriptor =
getDescriptor().getMessageTypes().get(0);
internal_static_Person_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_Person_descriptor,
new java.lang.String[] { "Name", "Id", "Email", });
}
// @@protoc_insertion_point(outer_class_scope)
}
4.性能比较
再验证之前pom.xml
文件需要导入和我们上面protoc.exe一样的版本号,否则可能会出错,上面的protoc.exe使用的是2.6.1
,pom文件也用2.6.1
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
这里我们再加上FASTJSON,进行三者之间的对比
package cn.tuling.nettybasic.serializable.protobuf;
import com.alibaba.fastjson.JSON;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class PerformTest {
public static void main(String[] args) throws IOException {
long start1 = System.currentTimeMillis();
PersonOuterClass.Person person = PersonOuterClass.Person.newBuilder()
.setId(1).setName("Cover").setEmail("xxxxx@gmail.com").build();
byte[] result = person.toByteArray();
long end1 = System.currentTimeMillis();
System.out.println("Proto serialize length:" + result.length + ", time :" + (end1 - start1));
// FAST JSON
long start3 = System.currentTimeMillis();
Person person2 = new Person();
person2.id = 2;
person2.name = "Cover";
person2.email = "xxxxx@gmail.com";
byte[] bytes = JSON.toJSONString(person2).getBytes();
long end3 = System.currentTimeMillis();
System.out.println("FASTJSON serialize length: " + bytes.length + ", time :" + (end3 - start3));
// JDK
long start2 = System.currentTimeMillis();
Person person1 = new Person();
person1.id = 2;
person1.name = "Cover";
person1.email = "xxxxx@gmail.com";
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(arrayOutputStream);
out.writeObject(person1);
long end2 = System.currentTimeMillis();
System.out.println("JDK serialize length:" + arrayOutputStream.toByteArray().length + ", time :" + (end2 - start2));
}
}
5.序列化结果展示
可以看到,Proto性能在空间复杂度和时间复杂度上总体要比FASTJSON和JDK要有优势,其实还有很多框架序列化和反序列化的性能也不错,下期我们再介绍其他几种框架,Proto的方式相比其他框架要麻烦一点,需要下载特定的编译器才行。它这样做的目的是为了方便地支持跨语言。