Merge pull request #35 from spring-cloud/config-server-health-indicator
a HealthIndicator for ConfigServer. It tests the http connection to the...
This commit is contained in:
@@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.bootstrap.BootstrapApplicationListener;
|
||||
import org.springframework.cloud.config.client.ConfigClientProperties;
|
||||
import org.springframework.cloud.config.client.ConfigServerHealthIndicator;
|
||||
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
|
||||
import org.springframework.cloud.config.client.PropertySourceLocator;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
@@ -115,6 +116,10 @@ public class PropertySourceBootstrapConfiguration implements
|
||||
return locator;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConfigServerHealthIndicator configServerHealthIndicator(ConfigServicePropertySourceLocator locator) {
|
||||
return new ConfigServerHealthIndicator(environment, locator);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.springframework.cloud.config.client;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ConfigServerHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private Environment env;
|
||||
private ConfigServicePropertySourceLocator locator;
|
||||
|
||||
public ConfigServerHealthIndicator(Environment env, ConfigServicePropertySourceLocator locator) {
|
||||
this.env = env;
|
||||
this.locator = locator;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Builder builder) throws Exception {
|
||||
try {
|
||||
PropertySource<?> propertySource = locator.locate(env);
|
||||
builder.up();
|
||||
if (propertySource instanceof CompositePropertySource) {
|
||||
CompositePropertySource composite = CompositePropertySource.class.cast(propertySource);
|
||||
Field field = ReflectionUtils.findField(CompositePropertySource.class,
|
||||
"propertySources");
|
||||
field.setAccessible(true);
|
||||
Set<PropertySource<?>> propertySources = (Set<PropertySource<?>>) field.get(composite);
|
||||
List<String> sources = new ArrayList<>();
|
||||
for (PropertySource<?> ps : propertySources) {
|
||||
sources.add(ps.getName());
|
||||
}
|
||||
builder.withDetail("propertySources", sources);
|
||||
} else {
|
||||
builder.withDetail("propertySources", propertySource.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
builder.down(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user