Skip to content

Commit a278b12

Browse files
Merge branch 'mrsimonemms-sje/grpc-health-check-docs'
2 parents 5206191 + 126825d commit a278b12

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

content/microservices/grpc.md

+36
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,42 @@ lotsOfGreetings(requestStream: any, callback: (err: unknown, value: HelloRespons
507507

508508
Here we used the `callback` function to send the response once processing of the `requestStream` has been completed.
509509

510+
#### Health checks
511+
512+
When running a gRPC application in an orchestrator such a Kubernetes, you may need to know if it is running and in a healthy state. The [gRPC Health Check specification](https://grpc.io/docs/guides/health-checking/) is a standard that allow gRPC clients to expose their health status to allow the orchestrator to act accordingly.
513+
514+
To add gRPC health check support, first install the [grpc-node](https://github.com/grpc/grpc-node/tree/master/packages/grpc-health-check) package:
515+
516+
```bash
517+
$ npm i --save grpc-health-check
518+
```
519+
520+
Then it can be hooked into the gRPC service using the `onLoadPackageDefinition` hook in your gRPC server options, as follows. Note that the `protoPath` needs to have both the health check and the hero package.
521+
522+
```typescript
523+
@@filename(main)
524+
import { HealthImplementation, protoPath as healthCheckProtoPath } from 'grpc-health-check';
525+
526+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
527+
options: {
528+
protoPath: [
529+
healthCheckProtoPath,
530+
protoPath: join(__dirname, 'hero/hero.proto'),
531+
],
532+
onLoadPackageDefinition: (pkg, server) => {
533+
const healthImpl = new HealthImplementation({
534+
'': 'UNKNOWN',
535+
});
536+
537+
healthImpl.addToServer(server);
538+
healthImpl.setStatus('', 'SERVING');
539+
},
540+
},
541+
});
542+
```
543+
544+
> info **Hint** The [gRPC health probe](https://github.com/grpc-ecosystem/grpc-health-probe) is a useful CLI to test gRPC health checks in a containerized environment.
545+
510546
#### gRPC Metadata
511547

512548
Metadata is information about a particular RPC call in the form of a list of key-value pairs, where the keys are strings and the values are typically strings but can be binary data. Metadata is opaque to gRPC itself - it lets the client provide information associated with the call to the server and vice versa. Metadata may include authentication tokens, request identifiers and tags for monitoring purposes, and data information such as the number of records in a data set.

0 commit comments

Comments
 (0)