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

huangapple go评论137阅读模式
英文:

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&lt;io.grpc.health.v1.HealthCheckResponse&gt;
          responseObserver) {

    final HealthCheckResponse response;
    if (someCondition()) {
      response = HealthCheckResponse.newBuilder()
          .setStatus(ServingStatus.NOT_SERVING)
          .build();
      log.info(&quot;********gRPC health check ==== NOT_SERVING ********&quot;);
    } else {
      response = HealthCheckResponse.newBuilder()
          .setStatus(HealthCheckResponse.ServingStatus.SERVING)
          .build();
      log.info(&quot;********gRPC health check ==== SERVING ********&quot;);
    }
    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 &#39;exceptionHandlerMethodResolver&#39; 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

huangapple
  • 本文由 发表于 2023年3月9日 22:48:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定