前后端数据交互利器–Protobuf

Protobuf 介绍

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

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,新建一个文件夹作为解压目录

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"

创建 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;
}
protoc -I=. --go_out=paths=source_relative:gen/go hello.proto

执行生成命令后会生成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}

Original: https://www.cnblogs.com/arvinhuang/p/15085830.html
Author: 平凡键客
Title: 前后端数据交互利器–Protobuf

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/622355/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Redis锁相关

    Redis锁相关 君不见,高堂明镜悲白发,朝如青丝暮成雪。 背景:面试的时候被问到有哪些锁,很快脱口而出Volatile、Synchronized和ReentrantLock,也能…

    数据库 2023年6月14日
    083
  • 从前,有一个简单的通道系统叫尤娜……

    从前,有个女生宿舍,住着小A、小B、尤娜和我4个人。有天,小A不小心把小B的床板坐塌了。小B非常生气,当场和小A翻脸。不论人缘最好的尤娜怎么中间调解都不管用。一直到毕业,小A和小B…

    数据库 2023年6月6日
    089
  • mysql多实例部署

    在MySQL中配置多实例 1.软件下载 2.配置用户和组并解压二进制程序至/usr/local下 3.创建各实例数据存放的目录 4.初始化各示例 5.配置配置文件/etc/my.c…

    数据库 2023年5月24日
    0100
  • Centos7 离线安装K3s

    1、安装前准备 github地址:https://github.com/k3s-io/k3s/releases k3s二进制文件:k3s下载地址:github地址 / 百度网盘地址…

    数据库 2023年6月14日
    0113
  • SQL学习日记(一) 语法篇

    对象名 关键字 描述 表 table 存储数据的逻辑单元,以行和列存在,行是数据记录,列是(属性)字段 系统表(数据字典) 存放数据库相关信息的表 程序员只可查看,不可修改 约束 …

    数据库 2023年5月24日
    079
  • docker 单机部署redis集群

    docker 部署redis集群 1、创建redis网卡 docker network create redis –subnet 172.38.0.0/16 查看网卡信息 doc…

    数据库 2023年6月11日
    088
  • SSM配置文件的连接

    使用ssm框架配置数据库连接时的问题 如果MySQL数据库版本是8.0.11, url配置成了MySql5.0以上版本需要的驱动类名(com.mysql. cj.jdbc.Driv…

    数据库 2023年6月14日
    085
  • 爬虫基础_正则表达式_补

    正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符、及这些特定字符的组合,组成一个 “规则字符串”,这个 “规则…

    数据库 2023年6月11日
    0147
  • GreatSQL 开源数据库 & NVIDIA InfiniBand存算分离池化方案:实现高性能分布式部署

    NVIDIA InfiniBand是一种被广泛使用的网络互联技术,基于IBTA(InfiniBand Trade Association)而定义的高带宽、低延时、低CPU占用率、大…

    数据库 2023年5月24日
    079
  • 面试记录

    JVM线程属于用户态还是内核态 当进程运行在ring3级别时为用户态,ring0级别时为内核态 有些操作需要有内核权限才能进行,那么有三种由用户态切换到内核态的情况: 系统调用:操…

    数据库 2023年6月16日
    0110
  • CSS样式

    css概述 层叠样式表(cascading style sheet) 层叠是指==将多个样式施加在一个元素(标签)上== 作用: 美化页面 将html代码与样式代码分离 好处: 功…

    数据库 2023年6月16日
    099
  • Activiti 7 源码学习

    启动分析 源码版本是 7.1.0.M6 首先从 ProcessEngineAutoConfiguration 开始 ProcessEngineAutoConfiguration 是…

    数据库 2023年6月14日
    098
  • Arrays.asList()你真的知道怎么用吗?

    发现问题 前几天在看别人的项目的时候,发现一个问题,简单复现一下这个问题 // 注意这是一个Integer对象的数组哦 Integer[] arr = new Integer[]{…

    数据库 2023年6月11日
    077
  • [Unity]如何将两个物体不留缝隙的精确贴合在一起

    比如说想让正方体精确贴合到墙上: 按住V,选中正方体,此时开启了 顶点吸附功能,正方体上的变换工具会变成以某个顶点为变换中心,如下图所示: 拖动中间的白色小方块,就会将正方体的该顶…

    数据库 2023年6月16日
    087
  • 如何使用Intellij IDEA工具导入SVN项目

    步骤一:选择VCS 打开Intellij IDEA开发工具,在导航栏中选择 VCS栏位,如图。 步骤二:创建SVN地址 执行步骤二,可以看见打开了一个SVN Repositorie…

    数据库 2023年6月6日
    087
  • Spring框架完整学习!!!

    1.Spring 1.1、简介 1.2、优点 1.3、组成 1.4、拓展 2.IOC思想解析 2.1、场景模拟 2.2、概念解析 2.3、总结 3.初涉Spring 3.1、 基础…

    数据库 2023年6月16日
    094
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球