xref: /aosp_15_r20/external/grpc-grpc-java/documentation/server-reflection-tutorial.md (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1*e07d83d3SAndroid Build Coastguard Worker# gRPC Server Reflection Tutorial
2*e07d83d3SAndroid Build Coastguard Worker
3*e07d83d3SAndroid Build Coastguard WorkergRPC Server Reflection provides information about publicly-accessible gRPC
4*e07d83d3SAndroid Build Coastguard Workerservices on a server, and assists clients at runtime with constructing RPC
5*e07d83d3SAndroid Build Coastguard Workerrequests and responses without precompiled service information. It is used by
6*e07d83d3SAndroid Build Coastguard Workerthe gRPC command line tool (gRPC CLI), which can be used to introspect server
7*e07d83d3SAndroid Build Coastguard Workerprotos and send/receive test RPCs. Reflection is only supported for
8*e07d83d3SAndroid Build Coastguard Workerproto-based services.
9*e07d83d3SAndroid Build Coastguard Worker
10*e07d83d3SAndroid Build Coastguard Worker## Enable Server Reflection
11*e07d83d3SAndroid Build Coastguard Worker
12*e07d83d3SAndroid Build Coastguard WorkergRPC-Java Server Reflection is implemented by
13*e07d83d3SAndroid Build Coastguard Worker`io.grpc.protobuf.services.ProtoReflectionService` in the `grpc-services`
14*e07d83d3SAndroid Build Coastguard Workerpackage. To enable server reflection, you need to add the
15*e07d83d3SAndroid Build Coastguard Worker`ProtoReflectionService` to your gRPC server.
16*e07d83d3SAndroid Build Coastguard Worker
17*e07d83d3SAndroid Build Coastguard WorkerFor example, to enable server reflection in
18*e07d83d3SAndroid Build Coastguard Worker`examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java`, we
19*e07d83d3SAndroid Build Coastguard Workerneed to make the following changes:
20*e07d83d3SAndroid Build Coastguard Worker
21*e07d83d3SAndroid Build Coastguard Worker```diff
22*e07d83d3SAndroid Build Coastguard Worker--- a/examples/build.gradle
23*e07d83d3SAndroid Build Coastguard Worker+++ b/examples/build.gradle
24*e07d83d3SAndroid Build Coastguard Worker@@ -27,6 +27,7 @@
25*e07d83d3SAndroid Build Coastguard Worker dependencies {
26*e07d83d3SAndroid Build Coastguard Worker   compile "io.grpc:grpc-netty-shaded:${grpcVersion}"
27*e07d83d3SAndroid Build Coastguard Worker   compile "io.grpc:grpc-protobuf:${grpcVersion}"
28*e07d83d3SAndroid Build Coastguard Worker+  compile "io.grpc:grpc-services:${grpcVersion}"
29*e07d83d3SAndroid Build Coastguard Worker   compile "io.grpc:grpc-stub:${grpcVersion}"
30*e07d83d3SAndroid Build Coastguard Worker
31*e07d83d3SAndroid Build Coastguard Worker   testCompile "junit:junit:4.12"
32*e07d83d3SAndroid Build Coastguard Worker--- a/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java
33*e07d83d3SAndroid Build Coastguard Worker+++ b/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java
34*e07d83d3SAndroid Build Coastguard Worker@@ -33,6 +33,7 @@ package io.grpc.examples.helloworld;
35*e07d83d3SAndroid Build Coastguard Worker
36*e07d83d3SAndroid Build Coastguard Worker import io.grpc.Server;
37*e07d83d3SAndroid Build Coastguard Worker import io.grpc.ServerBuilder;
38*e07d83d3SAndroid Build Coastguard Worker+import io.grpc.protobuf.services.ProtoReflectionService;
39*e07d83d3SAndroid Build Coastguard Worker import io.grpc.stub.StreamObserver;
40*e07d83d3SAndroid Build Coastguard Worker import java.io.IOException;
41*e07d83d3SAndroid Build Coastguard Worker import java.util.logging.Logger;
42*e07d83d3SAndroid Build Coastguard Worker@@ -50,6 +51,7 @@ public class HelloWorldServer {
43*e07d83d3SAndroid Build Coastguard Worker     int port = 50051;
44*e07d83d3SAndroid Build Coastguard Worker     server = ServerBuilder.forPort(port)
45*e07d83d3SAndroid Build Coastguard Worker         .addService(new GreeterImpl())
46*e07d83d3SAndroid Build Coastguard Worker+        .addService(ProtoReflectionService.newInstance())
47*e07d83d3SAndroid Build Coastguard Worker         .build()
48*e07d83d3SAndroid Build Coastguard Worker         .start();
49*e07d83d3SAndroid Build Coastguard Worker     logger.info("Server started, listening on " + port);
50*e07d83d3SAndroid Build Coastguard Worker```
51*e07d83d3SAndroid Build Coastguard Worker
52*e07d83d3SAndroid Build Coastguard WorkerIn the following examples, we assume you have made these changes to
53*e07d83d3SAndroid Build Coastguard Workerenable reflection in `HelloWorldServer.java`.
54*e07d83d3SAndroid Build Coastguard Worker
55*e07d83d3SAndroid Build Coastguard Worker## gRPC CLI
56*e07d83d3SAndroid Build Coastguard Worker
57*e07d83d3SAndroid Build Coastguard WorkerAfter enabling server reflection in a server application, you can use gRPC
58*e07d83d3SAndroid Build Coastguard WorkerCLI to get information about its available services. gRPC CLI is written
59*e07d83d3SAndroid Build Coastguard Workerin C++. Instructions on how to build gRPC CLI can be found at
60*e07d83d3SAndroid Build Coastguard Worker[command_line_tool.md](https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md).
61*e07d83d3SAndroid Build Coastguard Worker
62*e07d83d3SAndroid Build Coastguard Worker## Use gRPC CLI to check services
63*e07d83d3SAndroid Build Coastguard Worker
64*e07d83d3SAndroid Build Coastguard WorkerFirst, build and start the `hello-world-server`:
65*e07d83d3SAndroid Build Coastguard Worker
66*e07d83d3SAndroid Build Coastguard Worker```sh
67*e07d83d3SAndroid Build Coastguard Worker$ cd examples/
68*e07d83d3SAndroid Build Coastguard Worker$ ./gradlew installDist
69*e07d83d3SAndroid Build Coastguard Worker$ build/install/examples/bin/hello-world-server
70*e07d83d3SAndroid Build Coastguard Worker```
71*e07d83d3SAndroid Build Coastguard Worker
72*e07d83d3SAndroid Build Coastguard WorkerOpen a new terminal and make sure you are in the directory where the `grpc_cli`
73*e07d83d3SAndroid Build Coastguard Workerbinary is located:
74*e07d83d3SAndroid Build Coastguard Worker
75*e07d83d3SAndroid Build Coastguard Worker```sh
76*e07d83d3SAndroid Build Coastguard Worker$ cd <grpc-cpp-directory>/bins/opt
77*e07d83d3SAndroid Build Coastguard Worker```
78*e07d83d3SAndroid Build Coastguard Worker
79*e07d83d3SAndroid Build Coastguard Worker### List services
80*e07d83d3SAndroid Build Coastguard Worker
81*e07d83d3SAndroid Build Coastguard Worker`grpc_cli ls` command lists services and methods exposed at a given port:
82*e07d83d3SAndroid Build Coastguard Worker
83*e07d83d3SAndroid Build Coastguard Worker- List all the services exposed at a given port
84*e07d83d3SAndroid Build Coastguard Worker
85*e07d83d3SAndroid Build Coastguard Worker  ```sh
86*e07d83d3SAndroid Build Coastguard Worker  $ ./grpc_cli ls localhost:50051
87*e07d83d3SAndroid Build Coastguard Worker  ```
88*e07d83d3SAndroid Build Coastguard Worker
89*e07d83d3SAndroid Build Coastguard Worker  output:
90*e07d83d3SAndroid Build Coastguard Worker  ```sh
91*e07d83d3SAndroid Build Coastguard Worker  helloworld.Greeter
92*e07d83d3SAndroid Build Coastguard Worker  grpc.reflection.v1alpha.ServerReflection
93*e07d83d3SAndroid Build Coastguard Worker  ```
94*e07d83d3SAndroid Build Coastguard Worker
95*e07d83d3SAndroid Build Coastguard Worker- List one service with details
96*e07d83d3SAndroid Build Coastguard Worker
97*e07d83d3SAndroid Build Coastguard Worker  `grpc_cli ls` command inspects a service given its full name (in the format of
98*e07d83d3SAndroid Build Coastguard Worker  \<package\>.\<service\>). It can print information with a long listing format
99*e07d83d3SAndroid Build Coastguard Worker  when `-l` flag is set. This flag can be used to get more details about a
100*e07d83d3SAndroid Build Coastguard Worker  service.
101*e07d83d3SAndroid Build Coastguard Worker
102*e07d83d3SAndroid Build Coastguard Worker  ```sh
103*e07d83d3SAndroid Build Coastguard Worker  $ ./grpc_cli ls localhost:50051 helloworld.Greeter -l
104*e07d83d3SAndroid Build Coastguard Worker  ```
105*e07d83d3SAndroid Build Coastguard Worker
106*e07d83d3SAndroid Build Coastguard Worker  output:
107*e07d83d3SAndroid Build Coastguard Worker  ```sh
108*e07d83d3SAndroid Build Coastguard Worker  filename: helloworld.proto
109*e07d83d3SAndroid Build Coastguard Worker  package: helloworld;
110*e07d83d3SAndroid Build Coastguard Worker  service Greeter {
111*e07d83d3SAndroid Build Coastguard Worker    rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}
112*e07d83d3SAndroid Build Coastguard Worker  }
113*e07d83d3SAndroid Build Coastguard Worker
114*e07d83d3SAndroid Build Coastguard Worker  ```
115*e07d83d3SAndroid Build Coastguard Worker
116*e07d83d3SAndroid Build Coastguard Worker### List methods
117*e07d83d3SAndroid Build Coastguard Worker
118*e07d83d3SAndroid Build Coastguard Worker- List one method with details
119*e07d83d3SAndroid Build Coastguard Worker
120*e07d83d3SAndroid Build Coastguard Worker  `grpc_cli ls` command also inspects a method given its full name (in the
121*e07d83d3SAndroid Build Coastguard Worker  format of \<package\>.\<service\>.\<method\>).
122*e07d83d3SAndroid Build Coastguard Worker
123*e07d83d3SAndroid Build Coastguard Worker  ```sh
124*e07d83d3SAndroid Build Coastguard Worker  $ ./grpc_cli ls localhost:50051 helloworld.Greeter.SayHello -l
125*e07d83d3SAndroid Build Coastguard Worker  ```
126*e07d83d3SAndroid Build Coastguard Worker
127*e07d83d3SAndroid Build Coastguard Worker  output:
128*e07d83d3SAndroid Build Coastguard Worker  ```sh
129*e07d83d3SAndroid Build Coastguard Worker    rpc SayHello(helloworld.HelloRequest) returns (helloworld.HelloReply) {}
130*e07d83d3SAndroid Build Coastguard Worker  ```
131*e07d83d3SAndroid Build Coastguard Worker
132*e07d83d3SAndroid Build Coastguard Worker### Inspect message types
133*e07d83d3SAndroid Build Coastguard Worker
134*e07d83d3SAndroid Build Coastguard WorkerWe can use`grpc_cli type` command to inspect request/response types given the
135*e07d83d3SAndroid Build Coastguard Workerfull name of the type (in the format of \<package\>.\<type\>).
136*e07d83d3SAndroid Build Coastguard Worker
137*e07d83d3SAndroid Build Coastguard Worker- Get information about the request type
138*e07d83d3SAndroid Build Coastguard Worker
139*e07d83d3SAndroid Build Coastguard Worker  ```sh
140*e07d83d3SAndroid Build Coastguard Worker  $ ./grpc_cli type localhost:50051 helloworld.HelloRequest
141*e07d83d3SAndroid Build Coastguard Worker  ```
142*e07d83d3SAndroid Build Coastguard Worker
143*e07d83d3SAndroid Build Coastguard Worker  output:
144*e07d83d3SAndroid Build Coastguard Worker  ```sh
145*e07d83d3SAndroid Build Coastguard Worker  message HelloRequest {
146*e07d83d3SAndroid Build Coastguard Worker    optional string name = 1[json_name = "name"];
147*e07d83d3SAndroid Build Coastguard Worker  }
148*e07d83d3SAndroid Build Coastguard Worker  ```
149*e07d83d3SAndroid Build Coastguard Worker
150*e07d83d3SAndroid Build Coastguard Worker### Call a remote method
151*e07d83d3SAndroid Build Coastguard Worker
152*e07d83d3SAndroid Build Coastguard WorkerWe can send RPCs to a server and get responses using `grpc_cli call` command.
153*e07d83d3SAndroid Build Coastguard Worker
154*e07d83d3SAndroid Build Coastguard Worker- Call a unary method
155*e07d83d3SAndroid Build Coastguard Worker
156*e07d83d3SAndroid Build Coastguard Worker  ```sh
157*e07d83d3SAndroid Build Coastguard Worker  $ ./grpc_cli call localhost:50051 SayHello "name: 'gRPC CLI'"
158*e07d83d3SAndroid Build Coastguard Worker  ```
159*e07d83d3SAndroid Build Coastguard Worker
160*e07d83d3SAndroid Build Coastguard Worker  output:
161*e07d83d3SAndroid Build Coastguard Worker  ```sh
162*e07d83d3SAndroid Build Coastguard Worker  message: "Hello gRPC CLI"
163*e07d83d3SAndroid Build Coastguard Worker  ```
164