我是新来的协议缓冲区和gRPC的东西。现在,我正在尝试使用grpc +grpc网关构建一个客户机/服务器架构。
我试着学习一些例子,但我总是遇到同样的问题。在用protoc生成代码之后,我运行go build并得到以下错误:
proto/helloworld/hello_world.pb.gw.go:64:2: cannot use msg (type *HelloReply) as type protoreflect.ProtoMessage in return argument:
*HelloReply does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
proto/helloworld/hello_world.pb.gw.go:98:2: cannot use msg (type *HelloReply) as type protoreflect.ProtoMessage in return argument:
*HelloReply does not implement protoreflect.ProtoMessage (missing ProtoReflect method)我是go.mod
module github.com/riccardopedrielli/grpc-gateway-test
go 1.15
require (
github.com/golang/protobuf v1.4.3
github.com/grpc-ecosystem/grpc-gateway/v2 v2.2.0
google.golang.org/genproto v0.0.0-20210207032614-bba0dbe2a9ea
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
)我是hello_world.proto
syntax = "proto3";
package helloworld;
import "google/api/annotations.proto";
option go_package = "github.com/riccardopedrielli/grpc-gateway-test/proto/helloworld";
// Here is the overall greeting service definition where we define all our endpoints
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
get: "/v1/example/echo/{name}"
};
}
}
// The request message containing the user's name
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}这是到存储库的链接:https://github.com/riccardopedrielli/grpc-gateway-test
我看到生成的go文件之间的一个不同之处是它们导入的是不同的protobuf库。
protoc-gen-go生成的是导入github.com/golang/protobuf/proto。
protoc-gen-grpc-gateway生成的是导入google.golang.org/protobuf/proto。
这可能是问题的原因吗?
不过,我还不清楚应该使用哪一台,以及如何在这两台发电机中强制使用。
我对grpc并不熟悉,在这一点上我完全迷失了方向,所以我可以省略一些重要的信息。任何建议都将受到欢迎。
谢谢
发布于 2021-02-11 13:40:57
好吧我解决了这个问题。
我通过snap安装了protoc,而稳定通道有3.11.4版本。
现在我升级到3.14.0,一切都很好。
发布于 2021-05-02 07:12:52
为了生成存根,我们可以使用protoc或buf。protoc是业界广泛使用的最经典的一代体验。然而,它有一个相当陡峭的学习曲线。buf是一个较新的工具,它考虑到用户体验和速度。它还提供衬里和破坏变化检测,和一些protoc没有提供的东西。
您应该查看Gateway的教程系列,即https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/。此外,您还可以参考我的简单hello程序,它使用Gateway,即https://github.com/iamrajiv/helloworld-grpc-gateway。
https://stackoverflow.com/questions/66142486
复制相似问题