You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server-reflection-tutorial.md 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # gRPC Server Reflection Tutorial
  2. gRPC Server Reflection provides information about publicly-accessible gRPC
  3. services on a server, and assists clients at runtime to construct RPC requests
  4. and responses without precompiled service information. It is used by gRPC CLI,
  5. which can be used to introspect server protos and send/receive test RPCs.
  6. ## Enable Server Reflection
  7. gRPC-go Server Reflection is implemented in package
  8. [reflection](https://github.com/grpc/grpc-go/tree/master/reflection). To enable
  9. server reflection, you need to import this package and register reflection
  10. service on your gRPC server.
  11. For example, to enable server reflection in `example/helloworld`, we need to
  12. make the following changes:
  13. ```diff
  14. --- a/examples/helloworld/greeter_server/main.go
  15. +++ b/examples/helloworld/greeter_server/main.go
  16. @@ -40,6 +40,7 @@ import (
  17. "google.golang.org/grpc"
  18. pb "google.golang.org/grpc/examples/helloworld/helloworld"
  19. + "google.golang.org/grpc/reflection"
  20. )
  21. const (
  22. @@ -61,6 +62,8 @@ func main() {
  23. }
  24. s := grpc.NewServer()
  25. pb.RegisterGreeterServer(s, &server{})
  26. + // Register reflection service on gRPC server.
  27. + reflection.Register(s)
  28. if err := s.Serve(lis); err != nil {
  29. log.Fatalf("failed to serve: %v", err)
  30. }
  31. ```
  32. An example server with reflection registered can be found at
  33. `examples/features/reflection/server`.
  34. ## gRPC CLI
  35. After enabling Server Reflection in a server application, you can use gRPC CLI
  36. to check its services. gRPC CLI is only available in c++. Instructions on how to
  37. use gRPC CLI can be found at
  38. [command_line_tool.md](https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md).
  39. To build gRPC CLI:
  40. ```sh
  41. git clone https://github.com/grpc/grpc
  42. cd grpc
  43. make grpc_cli
  44. cd bins/opt # grpc_cli is in directory bins/opt/
  45. ```
  46. ## Use gRPC CLI to check services
  47. First, start the helloworld server in grpc-go directory:
  48. ```sh
  49. $ cd <grpc-go-directory>
  50. $ go run examples/features/reflection/server/main.go
  51. ```
  52. Open a new terminal and make sure you are in the directory where grpc_cli lives:
  53. ```sh
  54. $ cd <grpc-cpp-dirctory>/bins/opt
  55. ```
  56. ### List services
  57. `grpc_cli ls` command lists services and methods exposed at a given port:
  58. - List all the services exposed at a given port
  59. ```sh
  60. $ ./grpc_cli ls localhost:50051
  61. ```
  62. output:
  63. ```sh
  64. grpc.examples.echo.Echo
  65. grpc.reflection.v1alpha.ServerReflection
  66. helloworld.Greeter
  67. ```
  68. - List one service with details
  69. `grpc_cli ls` command inspects a service given its full name (in the format of
  70. \<package\>.\<service\>). It can print information with a long listing format
  71. when `-l` flag is set. This flag can be used to get more details about a
  72. service.
  73. ```sh
  74. $ ./grpc_cli ls localhost:50051 helloworld.Greeter -l
  75. ```
  76. output:
  77. ```sh
  78. filename: helloworld.proto
  79. package: helloworld;
  80. service Greeter {
  81. rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}
  82. }
  83. ```
  84. ### List methods
  85. - List one method with details
  86. `grpc_cli ls` command also inspects a method given its full name (in the
  87. format of \<package\>.\<service\>.\<method\>).
  88. ```sh
  89. $ ./grpc_cli ls localhost:50051 helloworld.Greeter.SayHello -l
  90. ```
  91. output:
  92. ```sh
  93. rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}
  94. ```
  95. ### Inspect message types
  96. We can use`grpc_cli type` command to inspect request/response types given the
  97. full name of the type (in the format of \<package\>.\<type\>).
  98. - Get information about the request type
  99. ```sh
  100. $ ./grpc_cli type localhost:50051 helloworld.HelloRequest
  101. ```
  102. output:
  103. ```sh
  104. message HelloRequest {
  105. optional string name = 1[json_name = "name"];
  106. }
  107. ```
  108. ### Call a remote method
  109. We can send RPCs to a server and get responses using `grpc_cli call` command.
  110. - Call a unary method
  111. ```sh
  112. $ ./grpc_cli call localhost:50051 SayHello "name: 'gRPC CLI'"
  113. ```
  114. output:
  115. ```sh
  116. message: "Hello gRPC CLI"
  117. ```