Skip to content

Commit

Permalink
reload properties of remotely activated profile fixes spring-cloudgh-…
Browse files Browse the repository at this point in the history
  • Loading branch information
Haytham Mohamed committed Mar 15, 2021
1 parent d06ab15 commit bce9d5d
Showing 1 changed file with 27 additions and 1 deletion.
Expand Up @@ -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);
Expand All @@ -101,6 +107,17 @@ public org.springframework.core.env.PropertySource<?> locate(org.springframework
@SuppressWarnings("unchecked")
Map<String, Object> map = translateOrigins(source.getName(),
(Map<String, Object>) 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));
}
}
Expand Down Expand Up @@ -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<String, Object> map) {
List<String> 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<org.springframework.core.env.PropertySource<?>> locateCollection(
Expand Down

0 comments on commit bce9d5d

Please sign in to comment.