如何在 Springboot 中为 gRPC 定义自定义健康检查端点

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

我想在我的 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();
  }
}

但是运行Springboot应用报错:

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)

如何禁用默认健康检查并提供自定义健康检查?

java spring-boot grpc spring-boot-actuator grpc-java
1个回答
0
投票

如果你正在使用https://github.com/yidongnan/grpc-spring-boot-starter,

你只需配置:

grpc.server.health-service-enabled 为 false

像这样: grpc.server.health-service-enabled=false

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