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)
}
}
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.
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 .
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.
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 .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With