Home > Back-end >  grpc service does not implement reflection.GRPCServer
grpc service does not implement reflection.GRPCServer

Time:10-25

I am creating a grpc service in go, I set up my grpc as follows

func setupGrpc() {
    lis, err := net.Listen("tcp", ":9000")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }

    s := followerservice.UserServer{}

    grpcServer := grpc.NewServer()
    gen.RegisterUserServiceServer(grpcServer, &s)

    // for ease of cli dev
    reflection.Register(s) // this doesn't work!

    if err := grpcServer.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %s", err)
    }
}

My UserServer looks like this

type UserServer struct {
    gen.UnimplementedUserServiceServer
}

func (s *UserServer) Create(ctx context.Context, in *gen.CreateUserRequest)(*gen.CreateUserResponse, error) {
    log.Printf("Receive message body from client: %s", in.Email)
    return &gen.CreateUserResponse{Id: "new id!"}, nil
}

At the line marked in func setupGrpc() I get a compile time error:

followerservice.UserServer does not implement reflection.GRPCServer (missing GetServiceInfo method)

but I can't find out how to implement it, I've been googling for hours now. It feels like protoc could generate this for me, but I'm kinda confused.

CodePudding user response:

You have to reflection.Register the *grpc.Server that you got from the grpc.NewServer() constructor, not your own implementation.

    grpcServer := grpc.NewServer()
    gen.RegisterUserServiceServer(grpcServer, &s)

    reflection.Register(grpcServer)
  • Related