I'm trying to learn gRPC and implemented the same code as in the tutorial. Wondering how to add gRPC health check to it.
Stumbled upon this, but clueless on how to write a gRPC health check.
I found this after many hours of search. To health check gRPC server, you have to add healthCheckService to your existing server. So the existing server will have multiple services running on it.
Example of how to add multiple services in same server is exaplained here.
Sample server:
# pip install grpcio-health-checking
from grpc_health.v1 import health
from grpc_health.v1 import health_pb2
from grpc_health.v1 import health_pb2_grpc
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
# your normal service, that the server is supposed to run
helloworld_pb2.add_GreeterServicer_to_server(_GreeterServicer(), server)
# health check service - add this service to server
health_pb2_grpc.add_HealthServicer_to_server(
health.HealthServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
The accompanying unit tests serve as a pretty good reference.
def start_server(self, non_blocking=False, thread_pool=None):
self._thread_pool = thread_pool
self._servicer = health.HealthServicer(
experimental_non_blocking=non_blocking,
experimental_thread_pool=thread_pool)
self._servicer.set('', health_pb2.HealthCheckResponse.SERVING)
self._servicer.set(_SERVING_SERVICE,
health_pb2.HealthCheckResponse.SERVING)
self._servicer.set(_UNKNOWN_SERVICE,
health_pb2.HealthCheckResponse.UNKNOWN)
self._servicer.set(_NOT_SERVING_SERVICE,
health_pb2.HealthCheckResponse.NOT_SERVING)
self._server = test_common.test_server()
port = self._server.add_insecure_port('[::]:0')
health_pb2_grpc.add_HealthServicer_to_server(
self._servicer, self._server)
self._server.start()
Keep a reference to the health servicer in the main servicer class for your application server. Then, call the set()
method on it at at the appropriate times, e.g. when startup is finished or when it goes into a state in which it is unable to server requests. Do note however that this makes use of some experimental features in order to ensure that the attached health servicer does not cause application-level requests to be starved.
SERVING
at start-up. If there's a period after the server starts during which the application needs to do preprocessing. Or if the application simply cannot operate when one of its downstream dependencies isn't functioning, being able to set that status yourself is vital. –
Macula I found this example really helpful: https://github.com/grpc/grpc/tree/master/examples/python/xds
Specifically, server.py
provides an implementation of health checking as well as reflection.
© 2022 - 2024 — McMap. All rights reserved.