Commit fe191906 authored by Andy Wilkinson's avatar Andy Wilkinson

Merge pull request #22222 from juliojgd

* gh-22222:
  Polish "Optionally ignore routing data sources when creating DB health indicators"
  Optionally ignore routing data sources when creating DB health indicators

Closes gh-22222
parents 523dd937 b6279180
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
......@@ -37,6 +37,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.metadata.CompositeDataSourcePoolMetadataProvider;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadata;
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
......@@ -54,6 +55,7 @@ import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Arthur Kalimullin
* @author Julio Gomez
* @since 2.0.0
*/
@Configuration(proxyBeanMethods = false)
......@@ -61,6 +63,7 @@ import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
@ConditionalOnBean(DataSource.class)
@ConditionalOnEnabledHealthIndicator("db")
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DataSourceHealthIndicatorProperties.class)
public class DataSourceHealthContributorAutoConfiguration extends
CompositeHealthContributorConfiguration<AbstractHealthIndicator, DataSource> implements InitializingBean {
......@@ -80,7 +83,14 @@ public class DataSourceHealthContributorAutoConfiguration extends
@Bean
@ConditionalOnMissingBean(name = { "dbHealthIndicator", "dbHealthContributor" })
public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources) {
public HealthContributor dbHealthContributor(Map<String, DataSource> dataSources,
DataSourceHealthIndicatorProperties dataSourceHealthIndicatorProperties) {
if (dataSourceHealthIndicatorProperties.isIgnoreRoutingDataSources()) {
Map<String, DataSource> filteredDatasources = dataSources.entrySet().stream()
.filter((e) -> !(e.getValue() instanceof AbstractRoutingDataSource))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
return createContributor(filteredDatasources);
}
return createContributor(dataSources);
}
......
/*
* Copyright 2012-2020 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.boot.actuate.autoconfigure.jdbc;
import org.springframework.boot.actuate.jdbc.DataSourceHealthIndicator;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* External configuration properties for {@link DataSourceHealthIndicator}.
*
* @author Julio Gomez
* @since 2.4.0
*/
@ConfigurationProperties(prefix = "management.health.db")
public class DataSourceHealthIndicatorProperties {
/**
* Whether to ignore AbstractRoutingDataSources when creating database health
* indicators.
*/
private boolean ignoreRoutingDataSources = false;
public boolean isIgnoreRoutingDataSources() {
return this.ignoreRoutingDataSources;
}
public void setIgnoreRoutingDataSources(boolean ignoreRoutingDataSources) {
this.ignoreRoutingDataSources = ignoreRoutingDataSources;
}
}
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
......@@ -44,6 +44,7 @@ import static org.mockito.Mockito.mock;
* Tests for {@link DataSourceHealthContributorAutoConfiguration}.
*
* @author Phillip Webb
* @author Julio Gomez
*/
class DataSourceHealthContributorAutoConfigurationTests {
......@@ -82,12 +83,30 @@ class DataSourceHealthContributorAutoConfigurationTests {
});
}
@Test
void runWithRoutingAndEmbeddedDataSourceShouldNotIncludeRoutingDataSourceWhenIgnored() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, RoutingDatasourceConfig.class)
.withPropertyValues("management.health.db.ignore-routing-datasources:true").run((context) -> {
assertThat(context).doesNotHaveBean(CompositeHealthContributor.class);
assertThat(context).hasSingleBean(DataSourceHealthIndicator.class);
assertThat(context).doesNotHaveBean(RoutingDataSourceHealthIndicator.class);
});
}
@Test
void runWithOnlyRoutingDataSourceShouldIncludeRoutingDataSource() {
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
.run((context) -> assertThat(context).hasSingleBean(RoutingDataSourceHealthIndicator.class));
}
@Test
void runWithOnlyRoutingDataSourceShouldCrashWhenIgnored() {
this.contextRunner.withUserConfiguration(RoutingDatasourceConfig.class)
.withPropertyValues("management.health.db.ignore-routing-datasources:true")
.run((context) -> assertThat(context).hasFailed().getFailure()
.hasRootCauseInstanceOf(IllegalArgumentException.class));
}
@Test
void runWithValidationQueryPropertyShouldUseCustomQuery() {
this.contextRunner
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment