diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index 8f5c52df..3af0f2b4 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -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); + } } } diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java new file mode 100644 index 00000000..8d12fe78 --- /dev/null +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java @@ -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> propertySources = (Set>) field.get(composite); + List 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); + } + } +}