1 # gRPC Server Reflection Tutorial
3 gRPC Server Reflection provides information about publicly-accessible gRPC
4 services on a server, and assists clients at runtime to construct RPC
5 requests and responses without precompiled service information. It is used by
6 gRPC CLI, which can be used to introspect server protos and send/receive test
9 ## Enable Server Reflection
11 gRPC-go Server Reflection is implemented in package [reflection](https://github.com/grpc/grpc-go/tree/master/reflection). To enable server reflection, you need to import this package and register reflection service on your gRPC server.
13 For example, to enable server reflection in `example/helloworld`, we need to make the following changes:
16 --- a/examples/helloworld/greeter_server/main.go
17 +++ b/examples/helloworld/greeter_server/main.go
18 @@ -40,6 +40,7 @@ import (
19 "golang.org/x/net/context"
20 "google.golang.org/grpc"
21 pb "google.golang.org/grpc/examples/helloworld/helloworld"
22 + "google.golang.org/grpc/reflection"
26 @@ -61,6 +62,8 @@ func main() {
29 pb.RegisterGreeterServer(s, &server{})
30 + // Register reflection service on gRPC server.
31 + reflection.Register(s)
32 if err := s.Serve(lis); err != nil {
33 log.Fatalf("failed to serve: %v", err)
37 We have made this change in `example/helloworld`, and we will use it as an example to show the use of gRPC server reflection and gRPC CLI in this tutorial.
41 After enabling Server Reflection in a server application, you can use gRPC CLI to check its services.
42 gRPC CLI is only available in c++. Instructions on how to use gRPC CLI can be found at [command_line_tool.md](https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md).
47 git clone https://github.com/grpc/grpc
50 cd bins/opt # grpc_cli is in directory bins/opt/
53 ## Use gRPC CLI to check services
55 First, start the helloworld server in grpc-go directory:
58 $ cd <grpc-go-directory>
59 $ go run examples/helloworld/greeter_server/main.go
62 Open a new terminal and make sure you are in the directory where grpc_cli lives:
65 $ cd <grpc-cpp-dirctory>/bins/opt
70 `grpc_cli ls` command lists services and methods exposed at a given port:
72 - List all the services exposed at a given port
75 $ ./grpc_cli ls localhost:50051
81 grpc.reflection.v1alpha.ServerReflection
84 - List one service with details
86 `grpc_cli ls` command inspects a service given its full name (in the format of
87 \<package\>.\<service\>). It can print information with a long listing format
88 when `-l` flag is set. This flag can be used to get more details about a
92 $ ./grpc_cli ls localhost:50051 helloworld.Greeter -l
97 filename: helloworld.proto
100 rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}
107 - List one method with details
109 `grpc_cli ls` command also inspects a method given its full name (in the
110 format of \<package\>.\<service\>.\<method\>).
113 $ ./grpc_cli ls localhost:50051 helloworld.Greeter.SayHello -l
118 rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}
121 ### Inspect message types
123 We can use`grpc_cli type` command to inspect request/response types given the
124 full name of the type (in the format of \<package\>.\<type\>).
126 - Get information about the request type
129 $ ./grpc_cli type localhost:50051 helloworld.HelloRequest
134 message HelloRequest {
135 optional string name = 1[json_name = "name"];
139 ### Call a remote method
141 We can send RPCs to a server and get responses using `grpc_cli call` command.
143 - Call a unary method
146 $ ./grpc_cli call localhost:50051 SayHello "name: 'gRPC CLI'"
151 message: "Hello gRPC CLI"