Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use health check? #13962

Closed
xiaoyulei opened this issue Jan 10, 2018 · 8 comments
Closed

how to use health check? #13962

xiaoyulei opened this issue Jan 10, 2018 · 8 comments

Comments

@xiaoyulei
Copy link

xiaoyulei commented Jan 10, 2018

I see the health check document, but I am confused. Is there any example code to show how to enable health check?

I call EnableDefaultHealthCheckService(true) before server start, is it open health check? Do I need something else?

@xiaoyulei xiaoyulei reopened this Jan 10, 2018
@Falco20019
Copy link
Contributor

See health_service_end2end_test.cc for an example. If you have enabled it, you should be fine. health_service_end2end_test.cc / VerifyHealthCheckService shows an example verifier code you could try.

@xiaoyulei
Copy link
Author

@Falco20019 thanks, I saw this file yesterday, but I am still confused. After EnableDefaultHealthCheckService, I saw server had enable health-checking from log, but client do nothing. I do not know client how to do about health-checking.

@Falco20019
Copy link
Contributor

Falco20019 commented Jan 11, 2018

std::string server_address("0.0.0.0:50051");
const grpc::string kHealthyService("healthy_service");

// Server code:
EnableDefaultHealthCheckService(true);
ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
std::unique_ptr<Server> server(builder.BuildAndStart());

HealthCheckServiceInterface* service = server->GetHealthCheckService();
service->SetServingStatus(kHealthyService, true);

// Client code:
HealthCheckRequest request;
request.set_service(kHealthyService);

HealthCheckResponse response;
ClientContext context;
std::shared_ptr<Channel> channel = CreateChannel(server_address, InsecureChannelCredentials());
std::unique_ptr<Health::Stub> hc_stub = grpc::health::v1::Health::NewStub(channel);
Status s = hc_stub->Check(&context, request, &response);
EXPECT_EQ(Status::OK.error_code(), s.error_code());
if (s.ok()) {
	EXPECT_EQ(HealthCheckResponse::SERVING, response.status());
}

server->Shutdown();

@xiaoyulei
Copy link
Author

@Falco20019 Clients must write code to call Check manually? Not like server only EnableDefaultHealthCheckService? I thought it is only a option.

@Falco20019
Copy link
Contributor

This service is meant mostly for load balancers. You can just call a client method even if the client is not there yet or if there are network problems. GRPC will block the call and retry until the client can execute the call or the deadline is defined and reached. For that you don’t have to enable this option at all.

@yang-g
Copy link
Member

yang-g commented Jan 18, 2018

@YuleiXiao what do you want to achieve? Indeed the service makes more sense for load balancers and (centralized) health checkers.

@xiaoyulei
Copy link
Author

@yang-g I thought health check can be used to check if network connection is OK

@yang-g
Copy link
Member

yang-g commented Jan 25, 2018

@YuleiXiao
Yes. Probing the health check service can be a way to check the connection but it is a bit heavy weight, as it does not only check the connection is alive but also check whether the server is actively serving the rpcs and it also requires a grpc client.

Assuming you are using C++ API, there are other ways you can monitor the connectivity. You may want to use the C++ channel state API e.g. https://github.com/grpc/grpc/blob/master/include/grpc++/impl/codegen/channel_interface.h#L74

There is also built-in keep-alive style checks that can be tuned with channel arguments here:
https://github.com/grpc/grpc/blob/master/include/grpc/impl/codegen/grpc_types.h#L217

@lock lock bot locked as resolved and limited conversation to collaborators Sep 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants