Why do we need to register reflection service on gRPC server

34.3k views Asked by At

I was going through this code of gRPC server. Can anyone tell me the need for reflection used here

Code :

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})
    // Register reflection service on gRPC server.
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}
2

There are 2 answers

2
menghanl On BEST ANSWER

Server reflection is not necessary to run the helloworld example.

The helloworld example is also used as a server reflection example, that's why you see the reflection registering code there.

More about server reflection: Server reflection is a service defined to provides information about publicly-accessible gRPC services on a gRPC server. Tutorial available here: https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md

1
swayamraina On

server-based-reflection is something that you will not need to build your day-to-day gRPC APIs.

This is a special instruction which exposes all the publicly accessible gRPC services on a gRPC server.
What this means essentially is that anyone can request your gRPC server to emit out details of the RPC service methods, request-response structures.

Where is this used?
This is used at places where you want to. dynamically call gRPC APIs. By dynamically I mean, the client does not need to hold the proto data-structures and register the RPC client stub.