From bce9d5dc5a174947fee94ee425c649c2184ee40e Mon Sep 17 00:00:00 2001 From: Haytham Mohamed Date: Mon, 15 Mar 2021 18:16:50 -0500 Subject: [PATCH] reload properties of remotely activated profile fixes gh-1834 --- .../ConfigServicePropertySourceLocator.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 03b1c71f1..1e38e79d9 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -68,15 +68,21 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator private RestTemplate restTemplate; private ConfigClientProperties defaultProperties; + + private final String ACTIVE_PROFILES_PROPERTY_NAME = "spring.profiles.active"; public ConfigServicePropertySourceLocator(ConfigClientProperties defaultProperties) { this.defaultProperties = defaultProperties; } @Override - @Retryable(interceptor = "configServerRetryInterceptor") public org.springframework.core.env.PropertySource locate(org.springframework.core.env.Environment environment) { ConfigClientProperties properties = this.defaultProperties.override(environment); + return locate(properties); + } + + @Retryable(interceptor = "configServerRetryInterceptor") + public org.springframework.core.env.PropertySource locate(ConfigClientProperties properties) { CompositePropertySource composite = new OriginTrackedCompositePropertySource("configService"); ConfigClientRequestTemplateFactory requestTemplateFactory = new ConfigClientRequestTemplateFactory(logger, properties); @@ -101,6 +107,17 @@ public org.springframework.core.env.PropertySource locate(org.springframework @SuppressWarnings("unchecked") Map map = translateOrigins(source.getName(), (Map) source.getSource()); + + // if different profile is activated within the default + // profile + boolean relocate = checkIfProfileIsActivatedInDefault(result.getProfiles(), map); + if (relocate) { + OriginTrackedValue newProfiles = (OriginTrackedValue) map + .get(ACTIVE_PROFILES_PROPERTY_NAME); + properties.setProfile(newProfiles.getValue().toString()); + return this.locate(properties); // relocate again + } + composite.addPropertySource(new OriginTrackedMapPropertySource(source.getName(), map)); } } @@ -138,6 +155,15 @@ public org.springframework.core.env.PropertySource locate(org.springframework } + // to check if different profile is activated within the default profile + private boolean checkIfProfileIsActivatedInDefault(String[] profiles, Map map) { + List profilesList = Arrays.asList(profiles); + return (profilesList.size() == 1 && profilesList.get(0).equalsIgnoreCase("default") + && map.containsKey(ACTIVE_PROFILES_PROPERTY_NAME) + && !(((OriginTrackedValue) map.get(ACTIVE_PROFILES_PROPERTY_NAME))).getValue().toString() + .equalsIgnoreCase("default")); + } + @Override @Retryable(interceptor = "configServerRetryInterceptor") public Collection> locateCollection(