Go语言 无法获取RPC描述符

x8diyxa7  于 2023-11-14  发布在  Go
关注(0)|答案(1)|浏览(108)

我有以下proto文件:
service_myservice.proto

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

字符串
server.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
}


然后在main包中调用这个函数

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)上的localhost。
然而,当我在evans中调用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要向前兼容,缺少什么部分?

xfb7svmp

xfb7svmp1#

REPL模式下使用evans时,调用show service前需要选择一个包,调用call 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

字符串

相关问题