Protobuf
grpc
-
struct ,数据
-
grpc
http/2
服务接口
.proto
syntax="proto3";
package xxxx;
message Data{
string name=1; //optional|required; # 默认 optional
int32 age=2;
// repeated array
}
message Response{
string xx=1;
int64 abc=2;
repeated string yy=3;
}
//rpc
service SSS{
rpc Sssa(Data) returns(Response){}
}
generate, compiler
install:
apt update
apt install unzip wget git -y
cd ~
mkdir protoc
#.. golang v1.12.7
wget -c https://dl.google.com/go/go1.12.7.linux-amd64.tar.gz
tar -zxvf go1.12.7.linux-amd64.tar.gz
#1. protoc -> unzip & PATH
# https://github.com/protocolbuffers/protobuf/releases
wget -c https://github.com/protocolbuffers/protobuf/releases/download/v3.9.0/protoc-3.9.0-linux-x86_64.zip && \
unzip -d ~/protoc/ protoc-3.9.0-linux-x86_64.zip
echo PATH="\$PATH:$HOME/protoc/bin:$HOME/go/bin:$GOPATH/bin" >> ~/.bashrc
echo export PATH >> ~/.bashrc
echo export GOPATH=$HOME/gopath >> ~/.bashrc
#2. protoc-gen-go 插件
go get -u -v github.com/golang/protobuf/protoc-gen-go
#3. gprc 插件
go get -u -v google.golang.org/grpc
source .bashrc
go version
protoc --version
compile:
# protoc --xx_out=OUT_DIR file.proto # xx==language
# --xx_out =调用=> protoc-gen-xx
protoc --go_out=./ xxxx.proto # xxxx.pb.go
#gprc### grpc 插件,需要安装
protoc --go_out=plugins=grpc:. xxxx.proto
protoc -I DIR --go_out=plugins=grpc:OUTDIR xxxx.proto
# INDIR/xxxx.proto
# 或者不要 -I 直接在后边写 INDIR/xxxx.proto