go - 获取 RPC 描述符失败

标签 go protocol-buffers grpc

我有以下原型(prototype)文件:

service_myservice.proto

service MyService {
    rpc GetData(GetRequest) returns (GetResponse) {}
}

服务器.go

type Server struct {
    pb.UnimplementedMyServiceServer
    config util.Config
}

// NewServer creates a new gRPC server.
func NewServer(config util.Config) (*Server, error) {
    server := &Server{
        config: config,
    }

    return server, nil
}

然后在主包中我调用这个函数

func runGrpcServer(config util.Config) {
    server, err := gapi.NewServer(config)
    if err != nil {
        log.Fatal().Err(err).Msg("cannot create server")
    }

    grpcLogger := grpc.UnaryInterceptor(gapi.GrpcLogger)
    grpcServer := grpc.NewServer(grpcLogger)
    pb.RegisterMyServiceServer(grpcServer, server)
    reflection.Register(grpcServer)

    listener, err := net.Listen("tcp", config.GRPCServerAddress)
    if err != nil {
        log.Fatal().Err(err).Msg("cannot create listener")
    }

    log.Info().Msgf("start gRPC server at %s", listener.Addr().String())
    err = grpcServer.Serve(listener)
    if err != nil {
        log.Fatal().Err(err).Msg("cannot start gRPC server")
    }
}

我正在使用 evans ( https://github.com/ktr0731/evans ) 来测试我的 grpc 调用。 我的 gRPC 服务器正在运行,我可以使用 evans 连接到特定端口 (9090) 上的本地主机。

但是,当我在埃文斯调用 show service 时,我得到以下响应:

localhost:9090> show service
+---------+-----+--------------+---------------+
| SERVICE | RPC | REQUEST TYPE | RESPONSE TYPE |
+---------+-----+--------------+---------------+
+---------+-----+--------------+---------------+
localhost:9090> call GetData
command call: failed to get the RPC descriptor for: GetData: service unselected

我认为如果我在 Server 结构中添加 pb.UnimplementedMyServiceServer ,那么这是向前兼容的。但显然,我缺少一些代码。

要使 MyService 向前兼容,缺少什么部分?

最佳答案

REPL模式下使用evans时,需要在调用show service之前选择套餐,在调用之前选择服务>调用 GetData

localhost:9090> show package
+-------------------------+
|         PACKAGE         |
+-------------------------+
| example                 |
| grpc.reflection.v1      |
| grpc.reflection.v1alpha |
+-------------------------+

localhost:9090> package example

example@localhost:9090> show service
+-----------+---------+--------------+---------------+
|  SERVICE  |   RPC   | REQUEST TYPE | RESPONSE TYPE |
+-----------+---------+--------------+---------------+
| MyService | GetData | GetRequest   | GetResponse   |
+-----------+---------+--------------+---------------+

example@localhost:9090> service MyService

example.MyService@localhost:9090> call GetData
id (TYPE_INT32) => 1

关于go - 获取 RPC 描述符失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77334063/

相关文章:

C++ Protobuf 未定义对构造函数/析构函数的引用

protocol-buffers - protobuf 导入 google/protobuf/timestamp.proto

arrays - Golang 从 .. 读取字节数组并显示结果

protocol-buffers - Google Protobuf 服务中的 RPC

datetime - 这是获得明天的最佳方式吗 09 :00?

maven - protoc jar maven 插件 intellij 不创建已编译的 proto 文件

python - 如果子消息没有字段,如何在 protobuf 消息上分配一个字段?

rest - gRPC(HTTP/2) 是否比使用 HTTP/2 的 REST 更快?

windows - 不允许使用 SharePoint token 类型

testing - 生成具有测试/快速、尊重不变量的结构树