介绍
Protobuf 有没有比 JSON 快 5 倍?-InfoQ
Protobuf 使用介绍及原理: Protobuf 使用介绍及原理 (gitee.com)
(2条消息) Protocol Buffer 序列化原理大揭秘 - 为什么Protocol Buffer性能这么好?_Carson带你学Android
使用
syntax="proto3"; option java_package = "com.vince.xq"; option java_multiple_files=true; option java_outer_classname="AddressBookProtos"; message Student { int32 id = 1; string name = 2; string email = 3; }
pom 文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>test-maven</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <grpc.version>1.6.1</grpc.version> <protobuf.version>3.3.0</protobuf.version> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId> <version>${grpc.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>${grpc.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>${grpc.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>${protobuf.version}</version> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.5.0.Final</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.5.0</version> <configuration> <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
点击compile
将在target 中生成的java 文件拷贝到 com.vince.xq 下 -----删除不需要拷贝
Generated Resources Root
package com.vince.xq; public class StudentVo { private int id; private String name; private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
package com.vince.xq; import com.alibaba.fastjson.JSONObject; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.util.JsonFormat; import java.util.List; public class MavenTest { public static void main(String[] args) { //获取Student对象 //这里的Student对象构造器被私有化,我们通过Student的内部类Builder来构建builder Student.Builder builder= Student.newBuilder(); //通过Student的内部类builder提供了构建Student相关属性的set方法 builder.setId(1); builder.setName("凌晨0点0分"); builder.setEmail("31346337@qq.com"); //序列化 Student stu=builder.build(); System.out.println("protobuf数据大小: " + stu.toByteString().size()); //再将封装有数据的对象实例,转换为字节数组,用于数据传输、存储等 byte[] stuByte = stu.toByteArray(); //这里得到了stuBte字节数组后,我们可以将该数据进行数据传输或存储,这里至于用什么技术传输就根据具体情况而定 //假如这里stuByt通过传输,下面的代码接到了该数据 //接收方 ,这里为了方便我们就写在一个类里面 //将字节数据反序列化为对应的对象实例 Student student=null; try { student= Student.parseFrom(stuByte); //这里得到了Student实例了,就可以根据需要来操作里面的数据了 System.out.println("学生ID:"+student.getId()); System.out.println("姓名:"+student.getName()); System.out.println("email:"+student.getEmail()); } catch (InvalidProtocolBufferException e) { e.printStackTrace(); } /*如何快速的进行json格式化*/ StudentVo studentVo=new StudentVo(); studentVo.setId(1); studentVo.setName("凌晨0点0分"); studentVo.setEmail("31346337@qq.com"); //System.out.println("protobuf数据大小: " + studentVo.size()); String jsonObject=""; try { jsonObject= JSONObject.toJSONString(studentVo); } catch (Exception e) { System.out.println(1111); } System.out.println(jsonObject.toString()); System.out.println("json数据大小: "+jsonObject.getBytes().length); } }