From 7f17b77853ccce703577e8bafe5ef347ac072952 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Mon, 15 Mar 2021 16:42:41 -0400 Subject: [PATCH 1/2] Get all property sources for bootstrap, configserver, and configclient in health indicator --- .../client/ConfigServerConfigDataLoader.java | 8 +++- .../client/ConfigServerHealthIndicator.java | 45 ++++++++++++------- .../ConfigServerHealthIndicatorTests.java | 12 +++++ 3 files changed, 48 insertions(+), 17 deletions(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java index 57a36154..f9bcf875 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java @@ -57,6 +57,11 @@ import static org.springframework.cloud.config.client.ConfigClientProperties.TOK public class ConfigServerConfigDataLoader implements ConfigDataLoader, Ordered { + /** + * PropertySource name for the config client. + */ + public static final String CONFIG_CLIENT_PROPERTYSOURCE_NAME = "configClient"; + protected final Log logger; public ConfigServerConfigDataLoader(Log logger) { @@ -159,7 +164,8 @@ public class ConfigServerConfigDataLoader implements ConfigDataLoader cached; + private List> cached = new ArrayList<>(); public ConfigServerHealthIndicator(ConfigurableEnvironment environment, ConfigClientHealthProperties properties) { this.environment = environment; @@ -47,29 +51,38 @@ public class ConfigServerHealthIndicator extends AbstractHealthIndicator { @Override protected void doHealthCheck(Builder builder) { - PropertySource propertySource = getPropertySource(); - builder.up(); - if (propertySource instanceof CompositePropertySource) { + List> propertySources = getPropertySource(); + if (propertySources.isEmpty()) { + builder.unknown(); + builder.unknown().withDetail("error", "no property sources located"); + } + else { + builder.up(); List sources = new ArrayList<>(); - for (PropertySource ps : ((CompositePropertySource) propertySource).getPropertySources()) { - sources.add(ps.getName()); + for (PropertySource propertySource : propertySources) { + + if (propertySource instanceof CompositePropertySource) { + for (PropertySource ps : ((CompositePropertySource) propertySource).getPropertySources()) { + sources.add(ps.getName()); + } + } + else if (propertySource != null) { + sources.add(propertySource.getName()); + } } builder.withDetail("propertySources", sources); } - else if (propertySource != null) { - builder.withDetail("propertySources", propertySource.toString()); - } - else { - builder.unknown().withDetail("error", "no property sources located"); - } } - private PropertySource getPropertySource() { + private List> getPropertySource() { long accessTime = System.currentTimeMillis(); if (isCacheStale(accessTime)) { this.lastAccess = accessTime; - MutablePropertySources propertySources = this.environment.getPropertySources(); - this.cached = propertySources.get("configClient"); + this.cached = this.environment.getPropertySources().stream() + .filter(p -> p.getName().startsWith(CONFIG_CLIENT_PROPERTYSOURCE_NAME) + || p.getName().startsWith(BOOTSTRAP_PROPERTY_SOURCE_NAME + "-") + || p.getName().startsWith(PREFIX)) + .collect(Collectors.toList()); } return this.cached; } diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java index c527626f..e04959db 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerHealthIndicatorTests.java @@ -17,6 +17,8 @@ package org.springframework.cloud.config.client; import java.util.Collections; +import java.util.List; +import java.util.Map; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -62,13 +64,23 @@ public class ConfigServerHealthIndicatorTests { @Test public void testServerUp() { setupPropertySources(); + Map details = this.indicator.getHealth(true).getDetails(); + List propertySources = (List) details.get("propertySources"); + assertThat(propertySources.contains("bootstrapProperties-test")).isTrue(); + assertThat(propertySources.contains("configserver:test")).isTrue(); + assertThat(propertySources.contains("configClient")).isTrue(); + assertThat(propertySources.size()).isEqualTo(3); assertThat(this.indicator.health().getStatus()).isEqualTo(Status.UP); } protected void setupPropertySources() { PropertySource source = new MapPropertySource("configClient", Collections.emptyMap()); + PropertySource configServerSource = new MapPropertySource("configserver:test", Collections.emptyMap()); + PropertySource bootstrapSource = new MapPropertySource("bootstrapProperties-test", Collections.emptyMap()); MutablePropertySources sources = new MutablePropertySources(); sources.addFirst(source); + sources.addFirst(bootstrapSource); + sources.addFirst(configServerSource); doReturn(sources).when(this.environment).getPropertySources(); } From 964238a5f245fb8faf38e8f2dc26469d22054e9d Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Mon, 15 Mar 2021 19:35:26 -0400 Subject: [PATCH 2/2] Use property source name in ConfigServerConfigDataLocator --- .../cloud/config/client/ConfigServerConfigDataLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java index f9bcf875..0f7e9227 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java @@ -128,7 +128,7 @@ public class ConfigServerConfigDataLoader implements ConfigDataLoader