Skip to content

Commit c63fe8c

Browse files
Merge branch 'sje/grpc-health-check-docs' of github.com:mrsimonemms/docs.nestjs.com into mrsimonemms-sje/grpc-health-check-docs
2 parents 5206191 + f6d3abf commit c63fe8c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

content/microservices/grpc.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,42 @@ Nest supports GRPC stream handlers in two possible ways:
377377

378378
<app-banner-enterprise></app-banner-enterprise>
379379

380+
#### Health checks
381+
382+
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.
383+
384+
To add gRPC health check support, first install the [grpc-node](https://github.com/grpc/grpc-node/tree/master/packages/grpc-health-check) package:
385+
386+
```bash
387+
$ npm i --save grpc-health-check
388+
```
389+
390+
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.
391+
392+
```typescript
393+
@@filename(main)
394+
import { HealthImplementation, protoPath as healthCheckProtoPath } from 'grpc-health-check';
395+
396+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
397+
options: {
398+
protoPath: [
399+
healthCheckProtoPath,
400+
protoPath: join(__dirname, 'hero/hero.proto'),
401+
],
402+
onLoadPackageDefinition: (pkg, server) => {
403+
const healthImpl = new HealthImplementation({
404+
'': 'UNKNOWN',
405+
});
406+
407+
healthImpl.addToServer(server);
408+
healthImpl.setStatus('', 'SERVING');
409+
},
410+
},
411+
});
412+
```
413+
414+
> 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.
415+
380416
#### Streaming sample
381417

382418
Let's define a new sample gRPC service called `HelloService`. The `hello.proto` file is structured using <a href="https://developers.google.com/protocol-buffers">protocol buffers</a>. Here's what it looks like:

0 commit comments

Comments
 (0)