Add multi-datasource health indicator support
Update EndpointAutoConfiguration to support multiple datasources. Fixes gh-783
This commit is contained in:
parent
697afe3842
commit
43bd42f0f5
|
|
@ -38,6 +38,7 @@ import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
|
|||
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
|
||||
import org.springframework.boot.actuate.endpoint.VanillaPublicMetrics;
|
||||
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
import org.springframework.boot.actuate.health.SimpleHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.VanillaHealthIndicator;
|
||||
|
|
@ -75,7 +76,7 @@ public class EndpointAutoConfiguration {
|
|||
private HealthIndicator<? extends Object> healthIndicator;
|
||||
|
||||
@Autowired(required = false)
|
||||
private DataSource dataSource;
|
||||
private Map<String, DataSource> dataSources;
|
||||
|
||||
@Autowired
|
||||
private InfoPropertiesConfiguration properties;
|
||||
|
|
@ -97,20 +98,30 @@ public class EndpointAutoConfiguration {
|
|||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public HealthEndpoint<Object> healthEndpoint() {
|
||||
public HealthEndpoint<?> healthEndpoint() {
|
||||
if (this.healthIndicator == null) {
|
||||
if (this.dataSource == null) {
|
||||
this.healthIndicator = new VanillaHealthIndicator();
|
||||
}
|
||||
else {
|
||||
SimpleHealthIndicator healthIndicator = new SimpleHealthIndicator();
|
||||
healthIndicator.setDataSource(this.dataSource);
|
||||
this.healthIndicator = healthIndicator;
|
||||
}
|
||||
this.healthIndicator = createHealthIndicator();
|
||||
}
|
||||
return new HealthEndpoint<Object>(this.healthIndicator);
|
||||
}
|
||||
|
||||
private HealthIndicator<? extends Object> createHealthIndicator() {
|
||||
if (this.dataSources == null || this.dataSources.isEmpty()) {
|
||||
return new VanillaHealthIndicator();
|
||||
}
|
||||
|
||||
if (this.dataSources.size() == 1) {
|
||||
return new SimpleHealthIndicator(this.dataSources.values().iterator().next());
|
||||
}
|
||||
|
||||
CompositeHealthIndicator composite = new CompositeHealthIndicator();
|
||||
for (Map.Entry<String, DataSource> entry : this.dataSources.entrySet()) {
|
||||
composite.addHealthIndicator(entry.getKey(),
|
||||
new SimpleHealthIndicator(entry.getValue()));
|
||||
}
|
||||
return composite;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public BeansEndpoint beansEndpoint() {
|
||||
|
|
|
|||
|
|
@ -54,6 +54,21 @@ public class SimpleHealthIndicator implements HealthIndicator<Map<String, Object
|
|||
|
||||
private String query = null;
|
||||
|
||||
/**
|
||||
* Create a new {@link SimpleHealthIndicator} instance.
|
||||
*/
|
||||
public SimpleHealthIndicator() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link SimpleHealthIndicator} using the specified datasource.
|
||||
* @param dataSource the data source
|
||||
*/
|
||||
public SimpleHealthIndicator(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> health() {
|
||||
LinkedHashMap<String, Object> health = new LinkedHashMap<String, Object>();
|
||||
|
|
|
|||
Loading…
Reference in New Issue