背景
项目中开发使用了go-kit框架开发微服务,需要用到protocol buffer工具来做序列化,需要安装protocol buffer以及go相关插件。
开发环境:Ubuntu Linux
安装过程
安装protocol buffer
项目地址:https://github.com/protocolbuffers/protobuf
获取包命令:wget https://github.com/protocolbuffers/protobuf/releases/download/3.15.0-rc1/protobuf-all-3.15.0-rc-1.zip
解压包命令:unzip protobuf-all-3.15.0-rc-1.zip
安装相关依赖:sudo apt-get install autoconf automake libtool curl make g++ unzip libffi-dev -y
编译安装命令:
./autogen.sh
./configure
make
sudo make install
sudo ldconfig
测试命令:protoc -h
安装protoc-gen-go和protoc-gen-go-grpc插件
项目地址:https://github.com/golang/protobuf.git
克隆命令:git clone https://github.com/golang/protobuf.git
编译安装命令:
cd protoc-gen-go/
go build
go install
在GOPATH目录下生成bin文件夹下的protoc-gen-go二进制文件
项目地址:https://github.com/grpc/grpc-go
克隆命令:git clone https://github.com/grpc/grpc-go.git
编译安装命令:
cd protoc-gen-go-grpc/
go build
go install
应用
根据protocol buffer的语法定义写proto接口定义文件,使用相关命令生成go代码
protoc -I ./ --go_out=plugins=grpc:./ xxxx.proto 或
protoc -I ./ *.proto --go_out=:../pb --go-grpc_out=:../pb