如何开启grpc健康检查?

问题描述 投票:0回答:1

我想在 grpc 服务器/客户端上打开健康检查,但我没有找到任何示例代码。我在

EnableDefaultHealthCheckService
中找到了
health_check_service_interface.h
函数并在服务器启动之前调用它。但不知道这样够吗?我需要在客户端或服务器中做其他事情吗?

c++ grpc
1个回答
0
投票
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();

https://github.com/grpc/grpc/issues/13962

© www.soinside.com 2019 - 2024. All rights reserved.