Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to register reflection service on gRPC server

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)
    }
}
like image 626
Naresh Avatar asked Jan 02 '17 10:01

Naresh


People also ask

What is gRPC server reflection?

Server Reflection is a gRPC feature that allows 'dynamic' clients, such as command-line tools for debugging, to discover the protocol used by a gRPC server at run time. They can then use this metadata to implement things like completion and sending arbitrary commands.

How do I enable gRPC reflection?

Enable Server Reflection gRPC-go Server Reflection is implemented in package reflection. To enable server reflection, you need to import this package and register reflection service on your gRPC server. An example server with reflection registered can be found at examples/features/reflection/server .

What is a service in gRPC?

gRPC is based on the idea of defining a service and the methods a client can use to operate the service. A gRPC service definition uses protocol buffers as its Interface Definition Language to describe the service. In this series, we will create a useful service to securely store usernames and passwords.

How does gRPC server work?

A gRPC channel provides a connection to a gRPC server on a specified host and port. It is used when creating a client stub. Clients can specify channel arguments to modify gRPC's default behavior, such as switching message compression on or off. A channel has state, including connected and idle .


Video Answer


2 Answers

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

like image 89
menghanl Avatar answered Oct 18 '22 22:10

menghanl


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.

  • grpCurl - curl gRPC services
  • gRPC transcoding - exposing json APIs on gRPC servers
like image 36
swayamraina Avatar answered Oct 18 '22 22:10

swayamraina