我正在尝试使用https://github.com/grpc-ecosystem/grpc-gateway来使我的go grpc方法也可以被http调用。为此,我使用了模块https://github.com/grpc-ecosystem/grpc-gateway。但是,当我使用protoc生成proto文件时,我没有看到示例中所示的方法Register*FromEndpoint。这是我的.proto文件的样子
syntax = "proto3";
package health;
option go_package = "github.com/user/app/api/health";
import "google/api/annotations.proto";
service Health {
rpc Ping (HealthRequest) returns (HealthReply) {
option (google.api.http) = {
get: "/ping"
};
}
}
// The request message containing the user's name
message HealthRequest {
}
// The response message containing the greetings
message HealthReply {
string message = 1;
}这是我的protoc命令的样子
protoc --go_out=api/proto/ --go_opt=paths=source_relative \
--go-grpc_out=./api/proto --go-grpc_opt=paths=source_relative \
--proto_path=internal/api \
--proto_path=third_party \
./internal/api/health/health.proto 生成过程运行良好,没有任何错误,但是生成的health_grpc.pb.go文件没有示例https://github.com/grpc-ecosystem/grpc-gateway中所示的等效RegisterYourServiceHandlerFromEndpoint方法
发布于 2021-04-27 09:47:55
正如grpc-gateway的文档所说,我建议您使用buf而不是protoc,它更容易和友好。
可以参考grpc-gateway#usage的文档
version: v1beta1
plugins:
- name: go
out: gen/go
opt:
- paths=source_relative
- name: go-grpc
out: gen/go
opt:
- paths=source_relative
- name: grpc-gateway
out: gen/go
opt:
- paths=source_relative
- generate_unbound_methods=true如果你仍然想使用protoc,你需要添加--grpc-gateway_opt参数:
protoc -I . --grpc-gateway_out ./gen/go \
--grpc-gateway_opt logtostderr=true \
--grpc-gateway_opt paths=source_relative \
--grpc-gateway_opt generate_unbound_methods=true \
your/service/v1/your_service.proto发布于 2021-05-14 16:41:01
为了生成存根,我们可以使用protoc或buf。protoc是业界广泛使用的更经典的生成体验。尽管如此,它仍然有一个相当陡峭的学习曲线。buf是一个考虑到用户体验和速度的较新工具。它还提供了毛皮和破损变化检测,以及protoc所不提供的功能。
你可以在这里阅读更多关于buf的内容:https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/generating_stubs/using_buf/。
https://stackoverflow.com/questions/67275071
复制相似问题