protobuf 使用和介绍

简介: protobuf 使用和介绍

介绍

Protobuf 有没有比 JSON 快 5 倍?-InfoQ

(3条消息) protobuf、thrift、avro对比

序列化:ProtoBuf与JSON的比较

Protobuf 使用介绍及原理: Protobuf 使用介绍及原理 (gitee.com)

(2条消息) Protocol Buffer 序列化原理大揭秘 - 为什么Protocol Buffer性能这么好?_Carson带你学Android

使用

Intellij IDEA中使用Protobuf的正确姿势

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);
    }
}


相关文章
|
4月前
|
存储 XML JSON
原来可以这么使用 Protobuf
原来可以这么使用 Protobuf
134 0
|
5月前
|
XML 存储 JSON
一文简单聊聊protobuf
一文简单聊聊protobuf
|
6月前
|
JavaScript Java PHP
Protobuf 3.3 使用总结
Protobuf 3.3 使用总结
53 0
|
9月前
|
XML 存储 Java
Protobuf了解一下?
Protobuf了解一下?
67 0
|
10月前
|
编译器
ProtoBuf的安装
ProtoBuf的安装
|
Java Android开发
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 序列化 | Protobuf 反序列化 )
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 序列化 | Protobuf 反序列化 )
341 0
|
Java Android开发
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )
292 0
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 使用文档 | 创建 Protobuf 源文件 | Protobuf 语法 )
|
Java Android开发
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 源码分析 | 创建 Protobuf 对象 )
【Android Protobuf 序列化】Protobuf 使用 ( Protobuf 源码分析 | 创建 Protobuf 对象 )
168 0
|
SQL 关系型数据库 数据库
Protobuf协议精品应用
  Protobuf应用广泛,尤其作为网络通讯协议最为普遍。本文将详细描述几个让人眼前一亮的protobuf协议设计,对准备应用或已经应用protobuf的开发者会有所启发,甚至可以直接拿过去用。 这里描述的协议设计被用于生产环境的即时通讯、埋点数据采集、消息推送、redis和mysql数据代理。
1196 0