gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/Documentation/server-reflection-tutorial.md (about)

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