【gRPC】 protocol buffer 安装

什么是 protocol buffers ?

protocol buffers(协议缓冲区)是Google开发的与语言无关,与平台无关,可扩展的机制,用于对结构化数据进行序列化
您只需定义一次数据的结构化方式,然后就可以使用特殊生成的源代码,轻松地在各种数据流和各种语言之间写入和读取结构化数据。

下载和安装 protocol buffer

Protoc Installation
协议缓冲区编译器 protoc 用于编译 .proto 文件,其中包含 服务消息定义

安装后检查 protoc 的版本(如下所示),以确保它足够新。一些包管理器安装的protoc版本可能相当过时。

从预编译的二进制文件安装是确保使用最新版本的protoc的最佳方法。

  • Linux(命令行)
sudo apt install -y protobuf-compiler
protoc --version
  • 安装预编译的二进制文件(any os)
    1. 从 https://github.com/protocolbuffers/protobuf/releases 下载与操作系统、cpu架构一致的 zip 文件。或者,向下面一样在命令行获取文件:
    PB_REL="https://github.com/protocolbuffers/protobuf/releases"
    curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip
    
    1. 解压文件到 $HOME/.local ,或者你选择的其它目录:
    unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
    
    1. 更新 PATH 环境变量,包含protoc可执行文件的路径
    export PATH="$PATH:$HOME/.local/bin"
    

Go plugins for the protocol compiler

由于 prototol buffer 没有直接支持 Go 语言,所以需要安装插件

  1. 使用下面命令安装 protocol buffer 的 Go 插件
    go install google.golang.org/protobuf/cmd/[email protected]
    go install google.golang.org/grpc/cmd/[email protected]
    
  2. 更新 PATH 环境变量,目的是 protoc 编译器能够找到这个插件:
    export PATH="$PATH:$(go env GOPATH)/bin"
    

你可能感兴趣的:(【gRPC】 protocol buffer 安装)