mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add configuration properties to disable out of the box health checks
This commit is contained in:
parent
4ffe816a21
commit
3f498a4803
@ -34,6 +34,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
@ -53,8 +54,8 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
@Configuration
|
||||
@AutoConfigureBefore({ EndpointAutoConfiguration.class })
|
||||
@AutoConfigureAfter({ DataSourceAutoConfiguration.class, MongoAutoConfiguration.class,
|
||||
MongoDataAutoConfiguration.class, RedisAutoConfiguration.class,
|
||||
RabbitAutoConfiguration.class })
|
||||
MongoDataAutoConfiguration.class, RedisAutoConfiguration.class,
|
||||
RabbitAutoConfiguration.class })
|
||||
public class HealthIndicatorAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ -65,6 +66,7 @@ public class HealthIndicatorAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(DataSource.class)
|
||||
@ConditionalOnExpression("${health.db.enabled:true}")
|
||||
public static class DataSourcesHealthIndicatorConfiguration {
|
||||
|
||||
@Autowired(required = false)
|
||||
@ -89,6 +91,7 @@ public class HealthIndicatorAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(MongoTemplate.class)
|
||||
@ConditionalOnExpression("${health.mongo.enabled:true}")
|
||||
public static class MongoHealthIndicatorConfiguration {
|
||||
|
||||
@Autowired
|
||||
@ -113,6 +116,7 @@ public class HealthIndicatorAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(RedisConnectionFactory.class)
|
||||
@ConditionalOnExpression("${health.redis.enabled:true}")
|
||||
public static class RedisHealthIndicatorConfiguration {
|
||||
|
||||
@Autowired
|
||||
@ -138,6 +142,7 @@ public class HealthIndicatorAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnBean(RabbitTemplate.class)
|
||||
@ConditionalOnExpression("${health.rabbit.enabled:true}")
|
||||
public static class RabbitHealthIndicatorConfiguration {
|
||||
|
||||
@Autowired
|
||||
|
@ -23,20 +23,23 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.MongoHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.RabbitHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.RedisHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.SimpleDataSourceHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.VanillaHealthIndicator;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.test.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link HealthIndicatorAutoConfiguration}.
|
||||
*
|
||||
*
|
||||
* @author Christian Dupuis
|
||||
*/
|
||||
public class HealthIndicatorAutoConfigurationTests {
|
||||
@ -84,6 +87,21 @@ public class HealthIndicatorAutoConfigurationTests {
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void notRedisHealthIndicator() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(RedisAutoConfiguration.class,
|
||||
HealthIndicatorAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "health.redis.enabled:false");
|
||||
this.context.refresh();
|
||||
Map<String, HealthIndicator> beans = this.context
|
||||
.getBeansOfType(HealthIndicator.class);
|
||||
assertEquals(1, beans.size());
|
||||
assertEquals(VanillaHealthIndicator.class, beans.values().iterator().next()
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void mongoHealthIndicator() {
|
||||
@ -98,6 +116,21 @@ public class HealthIndicatorAutoConfigurationTests {
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void notMongoHealthIndicator() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(MongoAutoConfiguration.class,
|
||||
MongoDataAutoConfiguration.class, HealthIndicatorAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "health.mongo.enabled:false");
|
||||
this.context.refresh();
|
||||
Map<String, HealthIndicator> beans = this.context
|
||||
.getBeansOfType(HealthIndicator.class);
|
||||
assertEquals(1, beans.size());
|
||||
assertEquals(VanillaHealthIndicator.class, beans.values().iterator().next()
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void combinedHealthIndicator() {
|
||||
@ -123,4 +156,48 @@ public class HealthIndicatorAutoConfigurationTests {
|
||||
assertEquals(SimpleDataSourceHealthIndicator.class, beans.values().iterator()
|
||||
.next().getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void notDataSourceHealthIndicator() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(EmbeddedDataSourceConfiguration.class,
|
||||
HealthIndicatorAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "health.db.enabled:false");
|
||||
this.context.refresh();
|
||||
Map<String, HealthIndicator> beans = this.context
|
||||
.getBeansOfType(HealthIndicator.class);
|
||||
assertEquals(1, beans.size());
|
||||
assertEquals(VanillaHealthIndicator.class, beans.values().iterator().next()
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void rabbitHealthIndicator() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(RabbitAutoConfiguration.class,
|
||||
HealthIndicatorAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
Map<String, HealthIndicator> beans = this.context
|
||||
.getBeansOfType(HealthIndicator.class);
|
||||
assertEquals(1, beans.size());
|
||||
assertEquals(RabbitHealthIndicator.class, beans.values().iterator().next()
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Test
|
||||
public void notRabbitHealthIndicator() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(RabbitAutoConfiguration.class,
|
||||
HealthIndicatorAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "health.rabbit.enabled:false");
|
||||
this.context.refresh();
|
||||
Map<String, HealthIndicator> beans = this.context
|
||||
.getBeansOfType(HealthIndicator.class);
|
||||
assertEquals(1, beans.size());
|
||||
assertEquals(VanillaHealthIndicator.class, beans.values().iterator().next()
|
||||
.getClass());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user