我想在 grpc 服务器/客户端上打开健康检查,但我没有找到任何示例代码。我在
EnableDefaultHealthCheckService
中找到了 health_check_service_interface.h
函数并在服务器启动之前调用它。但不知道这样够吗?我需要在客户端或服务器中做其他事情吗?
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();