1*e07d83d3SAndroid Build Coastguard WorkergRPC Examples 2*e07d83d3SAndroid Build Coastguard Worker============================================== 3*e07d83d3SAndroid Build Coastguard Worker 4*e07d83d3SAndroid Build Coastguard WorkerThe examples require `grpc-java` to already be built. You are strongly encouraged 5*e07d83d3SAndroid Build Coastguard Workerto check out a git release tag, since there will already be a build of gRPC 6*e07d83d3SAndroid Build Coastguard Workeravailable. Otherwise you must follow [COMPILING](../COMPILING.md). 7*e07d83d3SAndroid Build Coastguard Worker 8*e07d83d3SAndroid Build Coastguard WorkerYou may want to read through the 9*e07d83d3SAndroid Build Coastguard Worker[Quick Start](https://grpc.io/docs/languages/java/quickstart) 10*e07d83d3SAndroid Build Coastguard Workerbefore trying out the examples. 11*e07d83d3SAndroid Build Coastguard Worker 12*e07d83d3SAndroid Build Coastguard Worker## Basic examples 13*e07d83d3SAndroid Build Coastguard Worker 14*e07d83d3SAndroid Build Coastguard Worker- [Hello world](src/main/java/io/grpc/examples/helloworld) 15*e07d83d3SAndroid Build Coastguard Worker 16*e07d83d3SAndroid Build Coastguard Worker- [Route guide](src/main/java/io/grpc/examples/routeguide) 17*e07d83d3SAndroid Build Coastguard Worker 18*e07d83d3SAndroid Build Coastguard Worker- [Metadata](src/main/java/io/grpc/examples/header) 19*e07d83d3SAndroid Build Coastguard Worker 20*e07d83d3SAndroid Build Coastguard Worker- [Error handling](src/main/java/io/grpc/examples/errorhandling) 21*e07d83d3SAndroid Build Coastguard Worker 22*e07d83d3SAndroid Build Coastguard Worker- [Compression](src/main/java/io/grpc/examples/experimental) 23*e07d83d3SAndroid Build Coastguard Worker 24*e07d83d3SAndroid Build Coastguard Worker- [Flow control](src/main/java/io/grpc/examples/manualflowcontrol) 25*e07d83d3SAndroid Build Coastguard Worker 26*e07d83d3SAndroid Build Coastguard Worker- [Wait For Ready](src/main/java/io/grpc/examples/waitforready) 27*e07d83d3SAndroid Build Coastguard Worker 28*e07d83d3SAndroid Build Coastguard Worker- [Json serialization](src/main/java/io/grpc/examples/advanced) 29*e07d83d3SAndroid Build Coastguard Worker 30*e07d83d3SAndroid Build Coastguard Worker- <details> 31*e07d83d3SAndroid Build Coastguard Worker <summary>Hedging</summary> 32*e07d83d3SAndroid Build Coastguard Worker 33*e07d83d3SAndroid Build Coastguard Worker The [hedging example](src/main/java/io/grpc/examples/hedging) demonstrates that enabling hedging 34*e07d83d3SAndroid Build Coastguard Worker can reduce tail latency. (Users should note that enabling hedging may introduce other overhead; 35*e07d83d3SAndroid Build Coastguard Worker and in some scenarios, such as when some server resource gets exhausted for a period of time and 36*e07d83d3SAndroid Build Coastguard Worker almost every RPC during that time has high latency or fails, hedging may make things worse. 37*e07d83d3SAndroid Build Coastguard Worker Setting a throttle in the service config is recommended to protect the server from too many 38*e07d83d3SAndroid Build Coastguard Worker inappropriate retry or hedging requests.) 39*e07d83d3SAndroid Build Coastguard Worker 40*e07d83d3SAndroid Build Coastguard Worker The server and the client in the example are basically the same as those in the 41*e07d83d3SAndroid Build Coastguard Worker [hello world](src/main/java/io/grpc/examples/helloworld) example, except that the server mimics a 42*e07d83d3SAndroid Build Coastguard Worker long tail of latency, and the client sends 2000 requests and can turn on and off hedging. 43*e07d83d3SAndroid Build Coastguard Worker 44*e07d83d3SAndroid Build Coastguard Worker To mimic the latency, the server randomly delays the RPC handling by 2 seconds at 10% chance, 5 45*e07d83d3SAndroid Build Coastguard Worker seconds at 5% chance, and 10 seconds at 1% chance. 46*e07d83d3SAndroid Build Coastguard Worker 47*e07d83d3SAndroid Build Coastguard Worker When running the client enabling the following hedging policy 48*e07d83d3SAndroid Build Coastguard Worker 49*e07d83d3SAndroid Build Coastguard Worker ```json 50*e07d83d3SAndroid Build Coastguard Worker "hedgingPolicy": { 51*e07d83d3SAndroid Build Coastguard Worker "maxAttempts": 3, 52*e07d83d3SAndroid Build Coastguard Worker "hedgingDelay": "1s" 53*e07d83d3SAndroid Build Coastguard Worker } 54*e07d83d3SAndroid Build Coastguard Worker ``` 55*e07d83d3SAndroid Build Coastguard Worker Then the latency summary in the client log is like the following 56*e07d83d3SAndroid Build Coastguard Worker 57*e07d83d3SAndroid Build Coastguard Worker ```text 58*e07d83d3SAndroid Build Coastguard Worker Total RPCs sent: 2,000. Total RPCs failed: 0 59*e07d83d3SAndroid Build Coastguard Worker [Hedging enabled] 60*e07d83d3SAndroid Build Coastguard Worker ======================== 61*e07d83d3SAndroid Build Coastguard Worker 50% latency: 0ms 62*e07d83d3SAndroid Build Coastguard Worker 90% latency: 6ms 63*e07d83d3SAndroid Build Coastguard Worker 95% latency: 1,003ms 64*e07d83d3SAndroid Build Coastguard Worker 99% latency: 2,002ms 65*e07d83d3SAndroid Build Coastguard Worker 99.9% latency: 2,011ms 66*e07d83d3SAndroid Build Coastguard Worker Max latency: 5,272ms 67*e07d83d3SAndroid Build Coastguard Worker ======================== 68*e07d83d3SAndroid Build Coastguard Worker ``` 69*e07d83d3SAndroid Build Coastguard Worker 70*e07d83d3SAndroid Build Coastguard Worker See [the section below](#to-build-the-examples) for how to build and run the example. The 71*e07d83d3SAndroid Build Coastguard Worker executables for the server and the client are `hedging-hello-world-server` and 72*e07d83d3SAndroid Build Coastguard Worker `hedging-hello-world-client`. 73*e07d83d3SAndroid Build Coastguard Worker 74*e07d83d3SAndroid Build Coastguard Worker To disable hedging, set environment variable `DISABLE_HEDGING_IN_HEDGING_EXAMPLE=true` before 75*e07d83d3SAndroid Build Coastguard Worker running the client. That produces a latency summary in the client log like the following 76*e07d83d3SAndroid Build Coastguard Worker 77*e07d83d3SAndroid Build Coastguard Worker ```text 78*e07d83d3SAndroid Build Coastguard Worker Total RPCs sent: 2,000. Total RPCs failed: 0 79*e07d83d3SAndroid Build Coastguard Worker [Hedging disabled] 80*e07d83d3SAndroid Build Coastguard Worker ======================== 81*e07d83d3SAndroid Build Coastguard Worker 50% latency: 0ms 82*e07d83d3SAndroid Build Coastguard Worker 90% latency: 2,002ms 83*e07d83d3SAndroid Build Coastguard Worker 95% latency: 5,002ms 84*e07d83d3SAndroid Build Coastguard Worker 99% latency: 10,004ms 85*e07d83d3SAndroid Build Coastguard Worker 99.9% latency: 10,007ms 86*e07d83d3SAndroid Build Coastguard Worker Max latency: 10,007ms 87*e07d83d3SAndroid Build Coastguard Worker ======================== 88*e07d83d3SAndroid Build Coastguard Worker ``` 89*e07d83d3SAndroid Build Coastguard Worker 90*e07d83d3SAndroid Build Coastguard Worker</details> 91*e07d83d3SAndroid Build Coastguard Worker 92*e07d83d3SAndroid Build Coastguard Worker- <details> 93*e07d83d3SAndroid Build Coastguard Worker <summary>Retrying</summary> 94*e07d83d3SAndroid Build Coastguard Worker 95*e07d83d3SAndroid Build Coastguard Worker The [retrying example](src/main/java/io/grpc/examples/retrying) provides a HelloWorld gRPC client & 96*e07d83d3SAndroid Build Coastguard Worker server which demos the effect of client retry policy configured on the [ManagedChannel]( 97*e07d83d3SAndroid Build Coastguard Worker ../api/src/main/java/io/grpc/ManagedChannel.java) via [gRPC ServiceConfig]( 98*e07d83d3SAndroid Build Coastguard Worker https://github.com/grpc/grpc/blob/master/doc/service_config.md). Retry policy implementation & 99*e07d83d3SAndroid Build Coastguard Worker configuration details are outlined in the [proposal](https://github.com/grpc/proposal/blob/master/A6-client-retries.md). 100*e07d83d3SAndroid Build Coastguard Worker 101*e07d83d3SAndroid Build Coastguard Worker This retrying example is very similar to the [hedging example](src/main/java/io/grpc/examples/hedging) in its setup. 102*e07d83d3SAndroid Build Coastguard Worker The [RetryingHelloWorldServer](src/main/java/io/grpc/examples/retrying/RetryingHelloWorldServer.java) responds with 103*e07d83d3SAndroid Build Coastguard Worker a status UNAVAILABLE error response to a specified percentage of requests to simulate server resource exhaustion and 104*e07d83d3SAndroid Build Coastguard Worker general flakiness. The [RetryingHelloWorldClient](src/main/java/io/grpc/examples/retrying/RetryingHelloWorldClient.java) makes 105*e07d83d3SAndroid Build Coastguard Worker a number of sequential requests to the server, several of which will be retried depending on the configured policy in 106*e07d83d3SAndroid Build Coastguard Worker [retrying_service_config.json](src/main/resources/io/grpc/examples/retrying/retrying_service_config.json). Although 107*e07d83d3SAndroid Build Coastguard Worker the requests are blocking unary calls for simplicity, these could easily be changed to future unary calls in order to 108*e07d83d3SAndroid Build Coastguard Worker test the result of request concurrency with retry policy enabled. 109*e07d83d3SAndroid Build Coastguard Worker 110*e07d83d3SAndroid Build Coastguard Worker One can experiment with the [RetryingHelloWorldServer](src/main/java/io/grpc/examples/retrying/RetryingHelloWorldServer.java) 111*e07d83d3SAndroid Build Coastguard Worker failure conditions to simulate server throttling, as well as alter policy values in the [retrying_service_config.json]( 112*e07d83d3SAndroid Build Coastguard Worker src/main/resources/io/grpc/examples/retrying/retrying_service_config.json) to see their effects. To disable retrying 113*e07d83d3SAndroid Build Coastguard Worker entirely, set environment variable `DISABLE_RETRYING_IN_RETRYING_EXAMPLE=true` before running the client. 114*e07d83d3SAndroid Build Coastguard Worker Disabling the retry policy should produce many more failed gRPC calls as seen in the output log. 115*e07d83d3SAndroid Build Coastguard Worker 116*e07d83d3SAndroid Build Coastguard Worker See [the section below](#to-build-the-examples) for how to build and run the example. The 117*e07d83d3SAndroid Build Coastguard Worker executables for the server and the client are `retrying-hello-world-server` and 118*e07d83d3SAndroid Build Coastguard Worker `retrying-hello-world-client`. 119*e07d83d3SAndroid Build Coastguard Worker 120*e07d83d3SAndroid Build Coastguard Worker</details> 121*e07d83d3SAndroid Build Coastguard Worker 122*e07d83d3SAndroid Build Coastguard Worker- <details> 123*e07d83d3SAndroid Build Coastguard Worker <summary>Health Service</summary> 124*e07d83d3SAndroid Build Coastguard Worker 125*e07d83d3SAndroid Build Coastguard Worker The [health service example](src/main/java/io/grpc/examples/healthservice) 126*e07d83d3SAndroid Build Coastguard Worker provides a HelloWorld gRPC server that doesn't like short names along with a 127*e07d83d3SAndroid Build Coastguard Worker health service. It also provides a client application which makes HelloWorld 128*e07d83d3SAndroid Build Coastguard Worker calls and checks the health status. 129*e07d83d3SAndroid Build Coastguard Worker 130*e07d83d3SAndroid Build Coastguard Worker The client application also shows how the round robin load balancer can 131*e07d83d3SAndroid Build Coastguard Worker utilize the health status to avoid making calls to a service that is 132*e07d83d3SAndroid Build Coastguard Worker not actively serving. 133*e07d83d3SAndroid Build Coastguard Worker</details> 134*e07d83d3SAndroid Build Coastguard Worker 135*e07d83d3SAndroid Build Coastguard Worker 136*e07d83d3SAndroid Build Coastguard Worker- [Keep Alive](src/main/java/io/grpc/examples/keepalive) 137*e07d83d3SAndroid Build Coastguard Worker 138*e07d83d3SAndroid Build Coastguard Worker### <a name="to-build-the-examples"></a> To build the examples 139*e07d83d3SAndroid Build Coastguard Worker 140*e07d83d3SAndroid Build Coastguard Worker1. **[Install gRPC Java library SNAPSHOT locally, including code generation plugin](../COMPILING.md) (Only need this step for non-released versions, e.g. master HEAD).** 141*e07d83d3SAndroid Build Coastguard Worker 142*e07d83d3SAndroid Build Coastguard Worker2. From grpc-java/examples directory: 143*e07d83d3SAndroid Build Coastguard Worker``` 144*e07d83d3SAndroid Build Coastguard Worker$ ./gradlew installDist 145*e07d83d3SAndroid Build Coastguard Worker``` 146*e07d83d3SAndroid Build Coastguard Worker 147*e07d83d3SAndroid Build Coastguard WorkerThis creates the scripts `hello-world-server`, `hello-world-client`, 148*e07d83d3SAndroid Build Coastguard Worker`route-guide-server`, `route-guide-client`, etc. in the 149*e07d83d3SAndroid Build Coastguard Worker`build/install/examples/bin/` directory that run the examples. Each 150*e07d83d3SAndroid Build Coastguard Workerexample requires the server to be running before starting the client. 151*e07d83d3SAndroid Build Coastguard Worker 152*e07d83d3SAndroid Build Coastguard WorkerFor example, to try the hello world example first run: 153*e07d83d3SAndroid Build Coastguard Worker 154*e07d83d3SAndroid Build Coastguard Worker``` 155*e07d83d3SAndroid Build Coastguard Worker$ ./build/install/examples/bin/hello-world-server 156*e07d83d3SAndroid Build Coastguard Worker``` 157*e07d83d3SAndroid Build Coastguard Worker 158*e07d83d3SAndroid Build Coastguard WorkerAnd in a different terminal window run: 159*e07d83d3SAndroid Build Coastguard Worker 160*e07d83d3SAndroid Build Coastguard Worker``` 161*e07d83d3SAndroid Build Coastguard Worker$ ./build/install/examples/bin/hello-world-client 162*e07d83d3SAndroid Build Coastguard Worker``` 163*e07d83d3SAndroid Build Coastguard Worker 164*e07d83d3SAndroid Build Coastguard WorkerThat's it! 165*e07d83d3SAndroid Build Coastguard Worker 166*e07d83d3SAndroid Build Coastguard WorkerFor more information, refer to gRPC Java's [README](../README.md) and 167*e07d83d3SAndroid Build Coastguard Worker[tutorial](https://grpc.io/docs/languages/java/basics). 168*e07d83d3SAndroid Build Coastguard Worker 169*e07d83d3SAndroid Build Coastguard Worker### Maven 170*e07d83d3SAndroid Build Coastguard Worker 171*e07d83d3SAndroid Build Coastguard WorkerIf you prefer to use Maven: 172*e07d83d3SAndroid Build Coastguard Worker1. **[Install gRPC Java library SNAPSHOT locally, including code generation plugin](../COMPILING.md) (Only need this step for non-released versions, e.g. master HEAD).** 173*e07d83d3SAndroid Build Coastguard Worker 174*e07d83d3SAndroid Build Coastguard Worker2. Run in this directory: 175*e07d83d3SAndroid Build Coastguard Worker``` 176*e07d83d3SAndroid Build Coastguard Worker$ mvn verify 177*e07d83d3SAndroid Build Coastguard Worker$ # Run the server 178*e07d83d3SAndroid Build Coastguard Worker$ mvn exec:java -Dexec.mainClass=io.grpc.examples.helloworld.HelloWorldServer 179*e07d83d3SAndroid Build Coastguard Worker$ # In another terminal run the client 180*e07d83d3SAndroid Build Coastguard Worker$ mvn exec:java -Dexec.mainClass=io.grpc.examples.helloworld.HelloWorldClient 181*e07d83d3SAndroid Build Coastguard Worker``` 182*e07d83d3SAndroid Build Coastguard Worker 183*e07d83d3SAndroid Build Coastguard Worker### Bazel 184*e07d83d3SAndroid Build Coastguard Worker 185*e07d83d3SAndroid Build Coastguard WorkerIf you prefer to use Bazel: 186*e07d83d3SAndroid Build Coastguard Worker``` 187*e07d83d3SAndroid Build Coastguard Worker$ bazel build :hello-world-server :hello-world-client 188*e07d83d3SAndroid Build Coastguard Worker$ # Run the server 189*e07d83d3SAndroid Build Coastguard Worker$ bazel-bin/hello-world-server 190*e07d83d3SAndroid Build Coastguard Worker$ # In another terminal run the client 191*e07d83d3SAndroid Build Coastguard Worker$ bazel-bin/hello-world-client 192*e07d83d3SAndroid Build Coastguard Worker``` 193*e07d83d3SAndroid Build Coastguard Worker 194*e07d83d3SAndroid Build Coastguard Worker## Other examples 195*e07d83d3SAndroid Build Coastguard Worker 196*e07d83d3SAndroid Build Coastguard Worker- [Android examples](android) 197*e07d83d3SAndroid Build Coastguard Worker 198*e07d83d3SAndroid Build Coastguard Worker- Secure channel examples 199*e07d83d3SAndroid Build Coastguard Worker 200*e07d83d3SAndroid Build Coastguard Worker + [TLS examples](example-tls) 201*e07d83d3SAndroid Build Coastguard Worker 202*e07d83d3SAndroid Build Coastguard Worker + [ALTS examples](example-alts) 203*e07d83d3SAndroid Build Coastguard Worker 204*e07d83d3SAndroid Build Coastguard Worker- [Google Authentication](example-gauth) 205*e07d83d3SAndroid Build Coastguard Worker 206*e07d83d3SAndroid Build Coastguard Worker- [JWT-based Authentication](example-jwt-auth) 207*e07d83d3SAndroid Build Coastguard Worker 208*e07d83d3SAndroid Build Coastguard Worker## Unit test examples 209*e07d83d3SAndroid Build Coastguard Worker 210*e07d83d3SAndroid Build Coastguard WorkerExamples for unit testing gRPC clients and servers are located in [examples/src/test](src/test). 211*e07d83d3SAndroid Build Coastguard Worker 212*e07d83d3SAndroid Build Coastguard WorkerIn general, we DO NOT allow overriding the client stub and we DO NOT support mocking final methods 213*e07d83d3SAndroid Build Coastguard Workerin gRPC-Java library. Users should be cautious that using tools like PowerMock or 214*e07d83d3SAndroid Build Coastguard Worker[mockito-inline](https://search.maven.org/search?q=g:org.mockito%20a:mockito-inline) can easily 215*e07d83d3SAndroid Build Coastguard Workerbreak this rule of thumb. We encourage users to leverage `InProcessTransport` as demonstrated in the 216*e07d83d3SAndroid Build Coastguard Workerexamples to write unit tests. `InProcessTransport` is light-weight and runs the server 217*e07d83d3SAndroid Build Coastguard Workerand client in the same process without any socket/TCP connection. 218*e07d83d3SAndroid Build Coastguard Worker 219*e07d83d3SAndroid Build Coastguard WorkerMocking the client stub provides a false sense of security when writing tests. Mocking stubs and responses 220*e07d83d3SAndroid Build Coastguard Workerallows for tests that don't map to reality, causing the tests to pass, but the system-under-test to fail. 221*e07d83d3SAndroid Build Coastguard WorkerThe gRPC client library is complicated, and accurately reproducing that complexity with mocks is very hard. 222*e07d83d3SAndroid Build Coastguard WorkerYou will be better off and write less code by using `InProcessTransport` instead. 223*e07d83d3SAndroid Build Coastguard Worker 224*e07d83d3SAndroid Build Coastguard WorkerExample bugs not caught by mocked stub tests include: 225*e07d83d3SAndroid Build Coastguard Worker 226*e07d83d3SAndroid Build Coastguard Worker* Calling the stub with a `null` message 227*e07d83d3SAndroid Build Coastguard Worker* Not calling `close()` 228*e07d83d3SAndroid Build Coastguard Worker* Sending invalid headers 229*e07d83d3SAndroid Build Coastguard Worker* Ignoring deadlines 230*e07d83d3SAndroid Build Coastguard Worker* Ignoring cancellation 231*e07d83d3SAndroid Build Coastguard Worker 232*e07d83d3SAndroid Build Coastguard WorkerFor testing a gRPC client, create the client with a real stub 233*e07d83d3SAndroid Build Coastguard Workerusing an 234*e07d83d3SAndroid Build Coastguard Worker[InProcessChannel](../core/src/main/java/io/grpc/inprocess/InProcessChannelBuilder.java), 235*e07d83d3SAndroid Build Coastguard Workerand test it against an 236*e07d83d3SAndroid Build Coastguard Worker[InProcessServer](../core/src/main/java/io/grpc/inprocess/InProcessServerBuilder.java) 237*e07d83d3SAndroid Build Coastguard Workerwith a mock/fake service implementation. 238*e07d83d3SAndroid Build Coastguard Worker 239*e07d83d3SAndroid Build Coastguard WorkerFor testing a gRPC server, create the server as an InProcessServer, 240*e07d83d3SAndroid Build Coastguard Workerand test it against a real client stub with an InProcessChannel. 241*e07d83d3SAndroid Build Coastguard Worker 242*e07d83d3SAndroid Build Coastguard WorkerThe gRPC-java library also provides a JUnit rule, 243*e07d83d3SAndroid Build Coastguard Worker[GrpcCleanupRule](../testing/src/main/java/io/grpc/testing/GrpcCleanupRule.java), to do the graceful 244*e07d83d3SAndroid Build Coastguard Workershutdown boilerplate for you. 245*e07d83d3SAndroid Build Coastguard Worker 246*e07d83d3SAndroid Build Coastguard Worker## Even more examples 247*e07d83d3SAndroid Build Coastguard Worker 248*e07d83d3SAndroid Build Coastguard WorkerA wide variety of third-party examples can be found [here](https://github.com/saturnism/grpc-java-by-example). 249