From cae3c1fe42df91883a8f599ee4afba290ae3feb6 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 10 Nov 2014 13:54:55 -0700 Subject: [PATCH] a HealthIndicator for ConfigServer. It tests the http connection to the config server and lists the names of the property sources if it is up. --- .../PropertySourceBootstrapConfiguration.java | 5 ++ .../client/ConfigServerHealthIndicator.java | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerHealthIndicator.java 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 876e5084..03272f7b 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 @@ -26,6 +26,7 @@ import org.springframework.beans.factory.annotation.Autowired; 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; @@ -113,6 +114,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); + } + } +}