• 前后端数据交互利器--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
    ", &helloProto)
    
    	//转成二进制流
    	b, err := proto.Marshal(&helloProto)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Printf("%X
    ", b)
    
    	//从二进制流转成结构体
    	var hello2 hello.Hello
    	err = proto.Unmarshal(b, &hello2)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Printf("hello2:%+v
    ", &hello2)
    
    	//转成json
    	jsonByte, err := json.Marshal(&hello2)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Printf("string(jsonByte):%+v
    ", 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}
    
    
  • 相关阅读:
    非诚勿扰骆琦攻略
    IT服务者的困惑与解决之道
    某某银行IT运维管理的三点和四化
    提升CIO地位及IT价值体现,IT治理理念在中国势在必行
    振兴民族软件,险恶的江湖该如何仗剑走天涯
    某连锁饭店IT服务台、自助服务建设
    证券行业ITIL初探助力券商成就IT管理之路
    分享屡见成效的另类方法论保障ITIL软件及ITSM方案落地实施
    城市商业银行IT科技工作管理之痛
    【转】陈天晴:信息化发展要注意规划调整 重视IT治理
  • 原文地址:https://www.cnblogs.com/arvinhuang/p/15085830.html
Copyright © 2020-2023  润新知