安裝 protoc 編譯器
(protoc編輯器教藻,就是把我們的 .proto 文件編譯成不同語言的代碼)
第一步 去下面 github
https://github.com/protocolbuffers/protobuf
點擊 release 赎婚,查看發(fā)行的版本
現(xiàn)在(2019-03-12)最新的是 v3.7.0
2019-10
補充:可能是因為我升級go版本導(dǎo)致 GOROOT下(/usr/local/go)bin目錄中必要的安裝文件都沒有了
重裝一下
最新的protoc是 3.10.0了 (protoc-3.10.0-osx-x86_64)
安裝方式一:(推薦第二種方式,簡單更快)
- 下載 protobuf-all-3.7.0.zip 瞭吃,解壓
cd protobuf-3.7.0 - 然后執(zhí)行下面兩條命令安裝即刻
./configure
make install - 完成后檢測下是否安裝成功:
protoc --help
protoc --version
安裝方式二:
直接在剛才的GitHub的release頁面下載編譯好的包
- mac下載 protoc-3.7.0-osx-x86_64.zip
- 解壓
- 將 protoc-3.7.0-osx-x86_64 文件夾中的 bin 目錄下的 protoc 文件, 拷貝到 GOROOT下的bin目錄里面)
- 將 protoc-3.7.0-osx-x86_64 文件夾中的 include 目錄下的 google文件夾, 拷貝到 /usr/local/include 目錄
(其他系統(tǒng)具體操作可以看下 解壓文件下的 readme.txt 文件里面有說明)
根據(jù)proto文件定義偎蘸,生成對應(yīng)語言代碼(演示的golang)
- 創(chuàng)建一個 hello.proto 文件
先用官方文檔中最簡單的一段測試代碼
syntax = "proto3";
package test;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
- 在該目錄下 執(zhí)行下面命令:
protoc --go_out=. hello.proto
或者
protoc --go_out=plugins=grpc:. hello.proto
或者
protoc --go_out=. *.proto
或者
protoc --go_out=plugins=grpc:. *.proto
會生成文件 hello.pb.go
2019-10
補充:這次執(zhí)行上面的命令會報錯了
protoc-gen-go: program not found or is not executable 。瞬内。迷雪。
protoc-gen-go: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--go_out: protoc-gen-go: Plugin failed with status code 1.
make: *** [proto] Error 1
看來還要安裝 protoc-gen-go
之前是在下面步驟“安裝 grpc-gateway”才安裝 protoc-gen-go
安裝:
go get -d -u github.com/golang/protobuf/protoc-gen-go
go install github.com/golang/protobuf/protoc-gen-go
結(jié)果安裝又報錯了:報錯信息
go install github.com/golang/protobuf/protoc-gen-go: open /usr/local/go/bin/protoc-gen-go: permission denied
看來沒有權(quán)限往/usr/local/go/bin/目錄下安裝寫入
解決:
執(zhí)行命令:
sudo chmod -R 777 /usr/local/go
不要執(zhí)行:
chmod -R 777 /usr/local/go
發(fā)現(xiàn)可能會報很多錯: 所以加上sudo吧
chmod: Unable to change file mode on /usr/local/go/*** Operation not permitted
再次執(zhí)行上面第二步的 go install
go install github.com/golang/protobuf/protoc-gen-go
沒有任何提示表示安裝成功了哈
再次試試上面的 protoc 的編譯命令成功。
安裝 grpc-gateway
github地址:
https://github.com/grpc-ecosystem/grpc-gateway
依次執(zhí)下面go get
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger
go get -u github.com/golang/protobuf/protoc-gen-go
補充 2019-10:
執(zhí)行第一步時又報錯了
go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway
#
cd /Users/dh/Documents/GOPATH/src/gopkg.in/yaml.v2; git pull --ff-only
From https://gopkg.in/yaml.v2
51d6538..f221b84 master -> origin/master
51d6538..970885f v2 -> origin/v2
* [new branch] v3 -> origin/v3
* [new tag] v2.2.4 -> v2.2.4
* [new tag] v2.2.3 -> v2.2.3
error: Your local changes to the following files would be overwritten by merge:
decode.go
decode_test.go
resolve.go
scannerc.go
Please commit your changes or stash them before you merge.
Aborting
package gopkg.in/yaml.v2: exit status 1
進(jìn)到y(tǒng)aml.v2目錄發(fā)現(xiàn)git有修改記錄呢
checkout . 后再試也不行
解決:
刪除 yaml.v2 目錄就可以了
上面執(zhí)行成功后會在 $GOBIN (/usr/local/go/bin)目錄下面 生成3個二進(jìn)制文件
protoc-gen-grpc-gateway
protoc-gen-grpc-swagger
protoc-gen-go
安裝完成了遂鹊,接下來
修改一下剛才的 hello.proto 文件
syntax = "proto3";
package hello;
import "google/api/annotations.proto";
message HelloRequest {
string name = 1;
int32 age = 2;
}
message HelloReply {
string message = 1;
}
service HelloService {
rpc SayHello (HelloRequest) returns (HelloReply){
option (google.api.http) = {
post:"/v1/examples/sayhello"
body:"*"
};
}
}
生成代碼的命令需要變了
上面的proto文件用到了 import google/api 的一些文件
新的生成命令:
- 生成 pb.go
protoc -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --go_out=plugins=grpc:. *.proto
生成 hello.pb.go
- 生成 gateway
protoc -I/usr/local/include -I. -I$GOPATH/src -I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis --grpc-gateway_out=logtostderr=true:. *.proto
生成 hello.pb.gw.go
- 生成 swagger
protoc -I/usr/local/include -I. \
-I$GOPATH/src \
-I$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--swagger_out=logtostderr=true:. *.proto
生成 hello.swagger.json