前后端数据交互利器--Protobuf

Protobuf 介绍

Protocol Buffers(又名 protobuf)是 Google 的语言中立、平台中立、可扩展的结构化数据序列化机制。

https://github.com/protocolbuffers/protobuf

简而言之,Protobuf 是 Google 开源的一款用于处理前后端数据交互格式的工具。通常来讲前后端使用的编程语言是不同的,使用 Protobuf无需多虑,前后端只管约定通信协议,之后就可以使用 pb 工具生成代码。

ProtoBuf 使用实战

下载Protobuf

直接上Github 下载最新发行版,注意选择自己电脑的平台。

笔者使用的环境是 Ubuntu 20.10,也就是linux,下面是下载链接

 wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip

安装Protobuf

首先执行上条命令下载Protobuf,新建一个文件夹作为解压目录

mkdir protoc
unzip protoc-3.17.3-linux-x86_64.zip -d ./protoc

配置环境变量

这里的路径需要根据自身情况进行修改

vim ~/.bashrc
export PATH="$PATH:/[替换成protoc所处的路径]/protoc/bin"
source ~./bashrc

使用Protobuf

这里使用Golang 进行演示

  • 使用golang还需要额外安装一些插件,这里贴出插件链接,也可以直接使用下面命令进行下载
插件链接: https://github.com/grpc-ecosystem/grpc-gateway

一键式安装:go get\
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc 这时候检查你的go/bin 目录下就会多出以上几个程序
如果不知道自己go/bin 路径,可以通过 go env 查看 $ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/xxx/go/bin"

定义消息的格式

  1. syntax 指定proto 版本
  2. package 指定包名
  3. proto 生成的路径
  4. 其余可以查看下面代码进行参考
创建 proto 文件hello.proto 文件内容如下:

syntax = "proto3";
package hello; option go_package = "hello/proto/gen/go;hello"; message ProtoInfo {
string version = 1;
}
enum ProtoEnum {
HELLO_ONE = 0;
HELLO_TWO = 1;
} message Hello {
string first = 1;
string second = 2;
int64 number_int64 = 3;
ProtoInfo proto_info = 4;//复合类型
repeated int32 array_int32 = 5;//数组
ProtoEnum proto_enum = 6;
}

生成pb 文件

protoc -I=. --go_out=paths=source_relative:gen/go hello.proto

protobuf demo

执行生成命令后会生成pb 文件,接下来看一下在项目中如何使用吧

package main

import (
"encoding/json"
"fmt"
hello "hello/proto/gen/go" "google.golang.org/protobuf/proto"
) func main() {
helloProto := hello.Hello{
First: "first",
Second: "second",
NumberInt64: 111,
ProtoInfo: &hello.ProtoInfo{
Version: "3",
},
ArrayInt32: []int32{1, 2, 3, 4, 5, 6},
ProtoEnum: hello.ProtoEnum_HELLO_TWO,
}
fmt.Printf("hello:%+v\n", &helloProto) //转成二进制流
b, err := proto.Marshal(&helloProto)
if err != nil {
panic(err)
}
fmt.Printf("%X\n", b) //从二进制流转成结构体
var hello2 hello.Hello
err = proto.Unmarshal(b, &hello2)
if err != nil {
panic(err)
}
fmt.Printf("hello2:%+v\n", &hello2) //转成json
jsonByte, err := json.Marshal(&hello2)
if err != nil {
panic(err)
}
fmt.Printf("string(jsonByte):%+v\n", string(jsonByte)) }

可以看到生成的pb 文件不仅支持二进制传输,同时也支持json 格式

运行demo得到如下结果:


hello:first:"first" second:"second" number_int64:111 proto_info:{version:"3"} array_int32:1 array_int32:2 array_int32:3 array_int32:4 array_int32:5 array_int32:6 proto_enum:HELLO_TWO
0A05666972737412067365636F6E64186F22030A01332A060102030405063001
hello2:first:"first" second:"second" number_int64:111 proto_info:{version:"3"} array_int32:1 array_int32:2 array_int32:3 array_int32:4 array_int32:5 array_int32:6 proto_enum:HELLO_TWO
string(jsonByte):{"first":"first","second":"second","number_int64":111,"proto_info":{"version":"3"},"array_int32":[1,2,3,4,5,6],"proto_enum":1}
上一篇:前后端数据交互处理基于原生JS模板引擎开发


下一篇:对GraphQL-BFF:微服务背景下的前后端数据交互方案的研究-------引用