Go gRPC网关不使用http端点方法生成ResiterWeb

Go gRPC网关不使用http端点方法生成ResiterWeb,go,grpc,Go,Grpc,我正在尝试使用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 healt

我正在尝试使用
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 --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      
这就是我的protoc命令的样子

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 --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\u grpc.pb.go
文件没有与此处示例所示的
RegisterYourServiceHandlerFromEndpoint
方法等效的
https://github.com/grpc-ecosystem/grpc-gateway

作为grpc网关的文件,我建议您使用protoc而不是protoc,它更简单、更友好

您可以参考

版本:v1beta1
插件:
-姓名:go
输出:gen/go
选择:
-路径=源\相对
-姓名:go grpc
输出:gen/go
选择:
-路径=源\相对
-名称:grpc网关
输出:gen/go
选择:
-路径=源\相对
-生成未绑定的方法=真
如果仍要使用protoc,则需要添加
--grpc-gateway\u opt
参数:

protoc-I--grpc-gateway\U out./gen/go\
--grpc-gateway_opt logtostderr=true\
--grpc-gateway\u opt paths=源\u相对\
--grpc-gateway_opt generate_unbound_methods=true\
您的/service/v1/your_service.proto

为了生成存根,我们可以使用
protoc
buf
protoc
是业界广泛使用的更经典的一代体验。不过,它有一个相当陡峭的学习曲线
buf
是一款更新的工具,它的设计考虑了用户体验和速度。它还提供了起毛和断裂变化检测,而一些
protoc
不提供的功能


您可以在此处阅读有关
buf
的更多信息:。

您的
protoc
选项似乎不包括触发gRPC网关代码生成的标志,例如
--gRPC-Gateway\u opt logtostderr=true--gRPC-Gateway\u opt path=source\u relative
(这将在中步骤4的顶部介绍)。感谢您的帮助,我添加了这些标志,然后得到以下错误
未知标志:--grpc-gateway\u opt
。我已经运行go安装并安装了github.com/grpc-economy/grpc-gateway/v2/protoc-gen-grpc-gateway\github.com/grpc-economy/grpc-gateway/v2/protoc-gen-openapiv2\google.golang.org/protobuf/cmd/protoc-gen-go\google.golang.org/grpc/cmd/protoc-gen-go-grpc@tmpdev是的,使用protoc有有线问题,只需使用buf,它工作得很好。我最终使用了以下协议\--grpc-gateway \/api/proto \--grpc-gateway \-opt logtostderr=true \--grpc-gateway \-opt path=source \-relative \-go \-out=api/proto \-go \-opt=path=source \-relative \-go-grpc \-out=/api/proto \-go-grpc \-opt=path=source \-relative \---proto_path=internal/api\--proto_path=third_party\--grpc-gateway\u opt generate\u unbound\u methods=true\./internal/api/health/health.proto