英文:
How to define custom health check endpoint for gRPC in Springboot
问题
我想定义一个自定义的 gRPC 健康检查在我的 gRPC 服务中。我为此编写了以下类:
@Slf4j
@GRpcService
@AllArgsConstructor
public class HealthCheckService extends HealthGrpc.HealthImplBase{
@Override
public void check(
final io.grpc.health.v1.HealthCheckRequest request,
final io.grpc.stub.StreamObserver<io.grpc.health.v1.HealthCheckResponse>
responseObserver) {
final HealthCheckResponse response;
if (someCondition()) {
response = HealthCheckResponse.newBuilder()
.setStatus(ServingStatus.NOT_SERVING)
.build();
log.info("********gRPC health check ==== NOT_SERVING ********");
} else {
response = HealthCheckResponse.newBuilder()
.setStatus(HealthCheckResponse.ServingStatus.SERVING)
.build();
log.info("********gRPC health check ==== SERVING ********");
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
然而,运行 Spring Boot 应用程序会导致以下错误:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver]: Factory method 'exceptionHandlerMethodResolver' threw exception with message: Duplicate key grpc.health.v1.Health (attempted merging values com.company.unit.customgrpcservice.grpc.HealthCheckService@4b37b01e and org.lognet.springboot.grpc.health.DefaultHealthStatusService$$SpringCGLIB$$0@46d52510)
我如何禁用默认健康检查并提供自定义健康检查?
英文:
I wanted to define a custom gRPC health check in my gRPC service. I wrote this class for that:
@Slf4j
@GRpcService
@AllArgsConstructor
public class HealthCheckService extends HealthGrpc.HealthImplBase{
@Override
public void check(
final io.grpc.health.v1.HealthCheckRequest request,
final io.grpc.stub.StreamObserver<io.grpc.health.v1.HealthCheckResponse>
responseObserver) {
final HealthCheckResponse response;
if (someCondition()) {
response = HealthCheckResponse.newBuilder()
.setStatus(ServingStatus.NOT_SERVING)
.build();
log.info("********gRPC health check ==== NOT_SERVING ********");
} else {
response = HealthCheckResponse.newBuilder()
.setStatus(HealthCheckResponse.ServingStatus.SERVING)
.build();
log.info("********gRPC health check ==== SERVING ********");
}
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
However, running the Springboot application gives this error:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.lognet.springboot.grpc.recovery.GRpcExceptionHandlerMethodResolver]: Factory method 'exceptionHandlerMethodResolver' threw exception with message: Duplicate key grpc.health.v1.Health (attempted merging values com.company.unit.customgrpcservice.grpc.HealthCheckService@4b37b01e and org.lognet.springboot.grpc.health.DefaultHealthStatusService$$SpringCGLIB$$0@46d52510)
How do I disable the default health check and provide the custom one?
答案1
得分: 1
如果您正在使用https://github.com/yidongnan/grpc-spring-boot-starter,
只需配置:
grpc.server.health-service-enabled为false
就像这样:
grpc.server.health-service-enabled=false
英文:
If you are using https://github.com/yidongnan/grpc-spring-boot-starter,
you just configure :
grpc.server.health-service-enabled to false
like this:
grpc.server.health-service-enabled=false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论