编译grpc-gateway
1,执行protoc -I . --go_out=plugins=grpc,Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:./server/ google/api/*.proto若出现:--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC错误,则需要安装protoc-gen-go,命令如下:go get github.com/golang/protobuf/protoc-gen-go
然后再重新protoc就可以得到对应的go文件,在google.golang.org下面
2,再执行protoc -I . --go_out=plugins=grpc,Mgoogle/api/annotations.proto=github.com/jergoo/go-grpc-example/proto/google/api:. ./server/server.proto命令,生成server的go文件
若出现protoc-gen-go: unable to determine Go import path for "server/server.proto"错误,则server.proto增加go_package字段,如:option go_package = "api";
若出现The import path must contain at least one forward slash ('/') character.错误,则修改go_package,如:option go_package = "./;api";
再执行protoc即可得到server.pb.go文件。
其中server.proto文件内容如下:
syntax = "proto3";
package api;
option go_package = "./;api";
import "google/api/annotations.proto";
message StringMessage {
string value = 1;
}
service MyService {
rpc Echo(StringMessage) returns (StringMessage) {
option (google.api.http) = {
post: "/v1/example/echo"
body: "*"
};
}
}
3,编译gateway,执行protoc --grpc-gateway_out=logtostderr=true:./server/ server/server.proto命令
页:
[1]