This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable custom binder health check impelementation
Currently, KafkaBinderHealthIndicator is not customizable and included by default when Spring Boot actuator is on the classpath. Fix this by allowing the application to provide a custom implementation. A new marker interface called KafkaBinderHealth can be used by the applicaiton to provide a custom HealthIndicator implementation, in which case, the binder's default implementation will be excluded. Tests and docs changes. Resolves #1180
- Loading branch information
1 parent
31b91f4
commit d345ac8
Showing
5 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/KafkaBinderHealth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2022-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.stream.binder.kafka; | ||
|
||
import org.springframework.boot.actuate.health.HealthIndicator; | ||
|
||
/** | ||
* Marker interface used for custom KafkaBinderHealth indicator implementations. | ||
* | ||
* @author Soby Chacko | ||
* @since 3.2.2 | ||
*/ | ||
public interface KafkaBinderHealth extends HealthIndicator { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...ngframework/cloud/stream/binder/kafka/integration2/KafkaBinderCustomHealthCheckTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2022-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.stream.binder.kafka.integration2; | ||
|
||
import org.junit.ClassRule; | ||
import org.junit.Test; | ||
|
||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
import org.springframework.boot.WebApplicationType; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.builder.SpringApplicationBuilder; | ||
import org.springframework.cloud.stream.binder.kafka.KafkaBinderHealth; | ||
import org.springframework.cloud.stream.binder.kafka.KafkaBinderHealthIndicator; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.kafka.test.rule.EmbeddedKafkaRule; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
||
/** | ||
* @author Soby Chacko | ||
*/ | ||
public class KafkaBinderCustomHealthCheckTests { | ||
|
||
@ClassRule | ||
public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, 10); | ||
|
||
@Test | ||
public void testCustomHealthIndicatorIsActivated() { | ||
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder( | ||
CustomHealthCheckApplication.class).web(WebApplicationType.NONE).run( | ||
"--spring.cloud.stream.kafka.binder.brokers=" | ||
+ embeddedKafka.getEmbeddedKafka().getBrokersAsString()); | ||
final KafkaBinderHealth kafkaBinderHealth = applicationContext.getBean(KafkaBinderHealth.class); | ||
assertThat(kafkaBinderHealth).isInstanceOf(CustomHealthIndicator.class); | ||
assertThatThrownBy(() -> applicationContext.getBean(KafkaBinderHealthIndicator.class)).isInstanceOf(NoSuchBeanDefinitionException.class); | ||
applicationContext.close(); | ||
} | ||
|
||
@SpringBootApplication | ||
static class CustomHealthCheckApplication { | ||
|
||
@Bean | ||
public CustomHealthIndicator kafkaBinderHealthIndicator() { | ||
return new CustomHealthIndicator(); | ||
} | ||
} | ||
|
||
static class CustomHealthIndicator implements KafkaBinderHealth { | ||
|
||
@Override | ||
public Health health() { | ||
return null; | ||
} | ||
} | ||
} |