diff --git a/module/spring-boot-grpc-server/src/main/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponents.java b/module/spring-boot-grpc-server/src/main/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponents.java index bdee841ba665..4ded582191d3 100644 --- a/module/spring-boot-grpc-server/src/main/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponents.java +++ b/module/spring-boot-grpc-server/src/main/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponents.java @@ -51,7 +51,7 @@ */ class AutoConfiguredHealthCheckedGrpcComponents implements HealthCheckedGrpcComponents { - private final HealthCheckedGrpcComponent server; + private final @Nullable HealthCheckedGrpcComponent server; private final Map services; @@ -68,8 +68,10 @@ class AutoConfiguredHealthCheckedGrpcComponents implements HealthCheckedGrpcComp () -> StatusAggregator.of(properties.getStatus().getOrder())); StatusMapper statusMapper = getNonQualifiedBean(beanFactory, StatusMapper.class, () -> StatusMapper.of(properties.getStatus().getMapping())); - this.server = new AutoConfiguredHealthCheckedGrpcComponent(HealthContributorMembership.always(), - statusAggregator, statusMapper); + this.server = (properties.isIncludeOverallHealth()) + ? new AutoConfiguredHealthCheckedGrpcComponent(HealthContributorMembership.always(), statusAggregator, + statusMapper) + : null; this.services = createServices(properties.getService(), beanFactory, statusAggregator, statusMapper); } diff --git a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponentsTests.java b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponentsTests.java index 8a47232a3ca2..c176d5802711 100644 --- a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponentsTests.java +++ b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/AutoConfiguredHealthCheckedGrpcComponentsTests.java @@ -59,6 +59,15 @@ void getServerMatchesAllMembers() { }); } + @Test + void getServerWhenIncludeOverallHealthIsFalseReturnsNull() { + this.contextRunner.withPropertyValues("spring.grpc.server.health.include-overall-health=false") + .run((context) -> { + HealthCheckedGrpcComponents components = context.getBean(HealthCheckedGrpcComponents.class); + assertThat(components.getServer()).isNull(); + }); + } + @Test void getServiceNamesReturnsServiceNames() { this.contextRunner diff --git a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java index 527c284f61ca..86913e45a6ad 100644 --- a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java +++ b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java @@ -302,6 +302,16 @@ void createsGrpcServerHealth() { }); } + @Test + void whenIncludeOverallHealthIsFalseDoesNotReportOverallHealth() { + this.contextRunner.withPropertyValues("spring.grpc.server.health.include-overall-health=false").run((context) -> { + GrpcServerHealth serverHealth = context.getBean(GrpcServerHealth.class); + Map result = new LinkedHashMap<>(); + serverHealth.update(result::put); + assertThat(result).isEmpty(); + }); + } + @Test void whenHasGrpcServerHealthBeanDoesNotCreateAdditional() { this.contextRunner.withUserConfiguration(GrpcServerHealthConfiguration.class).run((context) -> {