社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
(protoc编辑器,就是把我们的 .proto 文件编译成不同语言的代码)
第一步 去下面 github
https://github.com/protocolbuffers/protobuf
点击 release ,查看发行的版本
现在(2019-03-12)最新的是 v3.7.0
2019-10
补充:可能是因为我升级go版本导致 GOROOT下(/usr/local/go)bin目录中必要的安装文件都没有了
重装一下
最新的protoc是 3.10.0了 (protoc-3.10.0-osx-x86_64)
直接在刚才的GitHub的release页面下载编译好的包
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;
}
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
补充:这次执行上面的命令会报错了
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
结果安装又报错了:报错信息go install github.com/golang/protobuf/protoc-gen-go: open /usr/local/go/bin/protoc-gen-go: permission denied
看来没有权限往/usr/local/go/bin/目录下安装写入
解决:
执行命令:sudo chmod -R 777 /usr/local/go
不要执行:
chmod -R 777 /usr/local/go
发现可能会报很多错: 所以加上sudo吧
chmod: Unable to change file mode on /usr/local/go/*** Operation not permitted
再次执行上面第二步的 go installgo install github.com/golang/protobuf/protoc-gen-go
没有任何提示表示安装成功了哈
再次试试上面的 protoc 的编译命令成功。
github地址:
https://github.com/grpc-ecosystem/grpc-gateway
依次执下面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:
执行第一步时又报错了
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
进到yaml.v2目录发现git有修改记录呢
checkout . 后再试也不行
解决:
删除 yaml.v2 目录就可以了
上面执行成功后会在 $GOBIN (/usr/local/go/bin)目录下面 生成3个二进制文件
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 的一些文件
新的生成命令:
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
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
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
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!