Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust the priority of the configs #303

Open
wants to merge 1 commit into
base: 3.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/main/asciidoc/spring-cloud-zookeeper.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ property sources with profiles.

CAUTION: If you have set `spring.cloud.bootstrap.enabled=true` or `spring.config.use-legacy-processing=true`, or included `spring-cloud-starter-bootstrap`, then the above values will need to be placed in `bootstrap.yml` instead of `application.yml`.

NOTE: Due to a long-standing issue, the priority of the configuration in `bootstrap` mode is different from that in the `spring.config.import` mode. Setting `spring.cloud.zookeeper.config.profilePriority=true` can fix this and align it with the behavior of Spring Cloud Config. The default value of this flag is `false`, and will be set to `true` in the future, and this flag will eventually be removed.

=== Access Control Lists (ACLs)

You can add authentication information for Zookeeper ACLs by calling the `addAuthInfo`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ public class ZookeeperConfigProperties {
*/
private boolean failFast = true;

/**
* Config with profiles always have higher priority in bootstrap mode, just like in import mode.
*/
private boolean profilePriority = false;

public boolean isEnabled() {
return this.enabled;
}
Expand Down Expand Up @@ -112,6 +117,14 @@ public void setFailFast(boolean failFast) {
this.failFast = failFast;
}

public boolean isProfilePriority() {
return this.profilePriority;
}

public void setProfilePriority(boolean profilePriority) {
this.profilePriority = profilePriority;
}

@Override
public String toString() {
return new ToStringCreator(this)
Expand All @@ -121,6 +134,7 @@ public String toString() {
.append("defaultContext", defaultContext)
.append("profileSeparator", profileSeparator)
.append("failFast", failFast)
.append("profilePriority", profilePriority)
.toString();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public List<Context> generateAutomaticContexts(List<String> profiles, boolean re

String defaultContext = root + "/" + properties.getDefaultContext();
contexts.add(new Context(defaultContext));
addProfiles(contexts, defaultContext, profiles);
if (!properties.isProfilePriority()) {
addProfiles(contexts, defaultContext, profiles);
}

StringBuilder baseContext = new StringBuilder(root);
if (!properties.getName().startsWith("/")) {
Expand All @@ -58,6 +60,10 @@ public List<Context> generateAutomaticContexts(List<String> profiles, boolean re
// getName() defaults to ${spring.application.name} or application
baseContext.append(properties.getName());
contexts.add(new Context(baseContext.toString()));

if (properties.isProfilePriority()) {
addProfiles(contexts, defaultContext, profiles);
}
addProfiles(contexts, baseContext.toString(), profiles);

if (reverse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,46 @@ public void compositePropertySourceHoldsPropertySourcesInCorrectOrder() {
assertThat(propertySources.get(3).getName()).endsWith(defaultContext);
}

@Test
public void compositePropertySourceHoldsPropertySourcesInCorrectOrderWithProfilePriority() {

// given
final String defaultContext = "someDefaultContext";
final String someName = "someName";
final String someProfile = "someProfile";

final CuratorFramework curator = mock(CuratorFramework.class);
when(curator.getChildren()).thenReturn(mock(GetChildrenBuilder.class));

final MockEnvironment mockEnvironment = new MockEnvironment();
mockEnvironment.setActiveProfiles(someProfile);

final ZookeeperConfigProperties properties = new ZookeeperConfigProperties();
properties.setName(someName);
properties.setDefaultContext(defaultContext);
properties.setProfilePriority(true);

final ZookeeperPropertySourceLocator locator = new ZookeeperPropertySourceLocator(
curator, properties);

// when
final PropertySource<?> propertySource = locator.locate(mockEnvironment);

// then
assertThat(propertySource).isInstanceOf(CompositePropertySource.class);

// and
final ArrayList<PropertySource<?>> propertySources = new ArrayList<>(
((CompositePropertySource) propertySource).getPropertySources());

assertThat(propertySources.get(0).getName())
.endsWith(someName + properties.getProfileSeparator() + someProfile);
assertThat(propertySources.get(1).getName()).endsWith(
defaultContext + properties.getProfileSeparator() + someProfile);
assertThat(propertySources.get(2).getName()).endsWith(someName);
assertThat(propertySources.get(3).getName()).endsWith(defaultContext);
}

@Configuration
@EnableAutoConfiguration
static class Config implements ApplicationListener<EnvironmentChangeEvent> {
Expand Down