21

Protobuf入门与使用示例,高性能的序列化框架

 3 years ago
source link: https://www.pkslow.com/archives/protobuf-introduction
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1 前言

首先还是感叹一下谷歌的可怕,做了这么多开创性的生产级别的开源产品, Protobuf 就是其中一员。它是与开发语言无关、与平台无关的结构化数据的序列化框架。支持的语言有 JavaC/C++PythonRubyJS 等。使用它序列化后的数据比 JsonXML 小很多,所以在网络传输上有更好的性能表现。

但要注意,与 JsonXML 不同,人类无法直接或直观地阅读被 Protobuf 序列化后的结果。所以需要通过把接收到的数据反序列化后再通过各自语言处理展示。

2 安装与配置

根据自己的系统,到 Github Release 下载对应的程序,我这里下载的是 protoc-3.13.0-osx-x86_64.zip 。我下载后放在 /Users/larry/Software/protobuf 目录,然后解压:

$ unzip protoc-3.13.0-osx-x86_64.zip

解压后可执行文件就在 bin 目录下。

配置环境变量如下:

export PROTOBUF_HOME=/Users/larry/Software/protobuf
export PATH=$PATH:$PROTOBUF_HOME/bin

检查是否已经成功安装和配置:

$ protoc --version
libprotoc 3.13.0

正确显示版本号,安装成功。

3 proto文件使用

Protobuf 中,用 proto文件 来定义结构化数据,即 Message 。它是非常易于编写的,也简单直观。接下来我们写一个简单的例子。

3.1 编写proto文件

3.1.1 安装插件

我们将编写一个 WebSite.proto 文件,先装一个 ProtobufIDEA 插件,以方便编写及高亮。

7FnaMn2.png!mobile

3.1.2 编写消息体

我们编写的 proto 文件内容如下:

syntax = "proto3";
option java_package = "com.pkslow.proto";
option java_generate_equals_and_hash = true;
option java_outer_classname = "PkslowWebSite";

message WebSite {
  string name = 1;
  string link = 2;
  int32 age = 3;

  message Server {
    string hostname = 1;
    int32 port = 2;
  }

  Server server = 4;
}

看上是很清晰的:

syntax 指定为 proto3

option 可以指定一些配置;

message 是消息体的定义, stringint32 这些都是数据类型,后面的数据表示序号,不可重复。

我们还定义了子消息体 Server ,嵌入到 WebSite 中去。

3.1.3 数据类型

数据类型如下:

.proto Type C++ Type Java Type Python Type[2] Go Type Ruby Type C# Type PHP Type Dart Type double double double float float64 Float double float double float float float float float32 Float float float double int32 int32 int int int32 Fixnum or Bignum (as required) int integer int int64 int64 long int/long int64 Bignum long integer/string Int64 uint32 uint32 int int/long uint32 Fixnum or Bignum (as required) uint integer int uint64 uint64 long int/long uint64 Bignum ulong integer/string Int64 sint32 int32 int int int32 Fixnum or Bignum (as required) int integer int sint64 int64 long int/long int64 Bignum long integer/string Int64 fixed32 uint32 int int/long uint32 Fixnum or Bignum (as required) uint integer int fixed64 uint64 long int/long uint64 Bignum ulong integer/string Int64 sfixed32 int32 int int int32 Fixnum or Bignum (as required) int integer int sfixed64 int64 long int/long int64 Bignum long integer/string Int64 bool bool boolean bool bool TrueClass/FalseClass bool boolean bool string string String str/unicode string String (UTF-8) string string String bytes string ByteString str []byte String (ASCII-8BIT) ByteString string List

3.1.4 默认值

对于字符串,默认值为空串;

对于字节,默认值为空字节;

对于布尔型,默认值为 false

对于数字类型,默认值为 0

对于枚举值,默认值为第一个定义的枚举值,其实就是 0

对于嵌入式的消息体,不同语言有不同表现。

3.1.5 编译

编写完 proto 文件之后,就可以编译出不同语言对应的类了,我们编译 Java 的类如下:

$ protoc --java_out=./ WebSite.proto

这样在当前目录就会生成 Java 类如下:

EvY73mI.png!mobile

3.2 Java使用输出类

前面我们已经编写了 proto 文件,并生成了对应的 Java 类,现在我们来使用它。

我们重新生成 Java 类,直接放在 Java 的目录下:

$ protoc --java_out=../java WebSite.proto

引入依赖如下:

<dependency>
  <groupId>com.google.protobuf</groupId>
  <artifactId>protobuf-java</artifactId>
  <version>3.9.2</version>
</dependency>

Java 代码及关键注释如下:

package com.pkslow.proto;

import com.google.protobuf.InvalidProtocolBufferException;

public class ProtobufMain {
    public static void main(String[] args) {
        System.out.println("------create message and serialize------");
        //创建嵌入式消息类Server
        PkslowWebSite.WebSite.Server.Builder serverBuilder = PkslowWebSite.WebSite.Server.newBuilder();
        PkslowWebSite.WebSite.Server server = serverBuilder.setHostname("1024.511.10.1")
                .setPort(80)
                .build();

        //创建主消息类WebSite
        PkslowWebSite.WebSite.Builder webSiteBuilder = PkslowWebSite.WebSite.newBuilder();
        PkslowWebSite.WebSite webSite = webSiteBuilder
                .setName("pkslow")
                .setLink("www.pkslow.com")
                .setAge(1)
                .setServer(server)
                .build();

        //打印结果
        System.out.println("webSite: " + webSite);

        //序列化,可用于网络传输等
        byte[] data = webSite.toByteArray();

        System.out.println("------deserialize------");
        //反序列化
        try {
            PkslowWebSite.WebSite deserializedWebSite = PkslowWebSite.WebSite.parseFrom(data);
            //打印结果
            System.out.println("deserializedWebSite: " + deserializedWebSite);

            System.out.println("------compare------");
            //结果为false,说明是另一个类
            System.out.println(webSite == deserializedWebSite);
            //结果为true,说明序列化和反序列化正确
            System.out.println(webSite.equals(deserializedWebSite));
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }
}

程序执行后的输出日志如下:

------create message and serialize------
webSite: name: "pkslow"
link: "www.pkslow.com"
age: 1
server {
  hostname: "1024.511.10.1"
  port: 80
}

------deserialize------
deserializedWebSite: name: "pkslow"
link: "www.pkslow.com"
age: 1
server {
  hostname: "1024.511.10.1"
  port: 80
}

------compare------
false
true

4 总结

本文旨在通过一个从编写到 Java 使用的过程,让大家有一个大概的了解和感性认识。 Protobuf 其实本来也不是什么复杂的东西,但它的应用却是很广的,比如 gRPC

项目的代码在: https://github.com/LarryDpk/pkslow-samples

参考资料:

编写 proto3官网Language Guide (proto3)

欢迎关注微信公众号< 南瓜慢说 >,将持续为你更新...

Y7z2U3.png!mobile


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK