Skip to content

Commit 53f73da

Browse files
committed
Adjust the priority of the configs
1 parent 9e9eb71 commit 53f73da

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

docs/src/main/asciidoc/spring-cloud-zookeeper.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,8 @@ property sources with profiles.
564564

565565
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`.
566566

567+
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.
568+
567569
=== Access Control Lists (ACLs)
568570

569571
You can add authentication information for Zookeeper ACLs by calling the `addAuthInfo`

spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public class ZookeeperConfigProperties {
6262
*/
6363
private boolean failFast = true;
6464

65+
/**
66+
* Config with profiles always have higher priority in bootstrap mode, just like in import mode.
67+
*/
68+
private boolean profilePriority = false;
69+
6570
public boolean isEnabled() {
6671
return this.enabled;
6772
}
@@ -112,6 +117,14 @@ public void setFailFast(boolean failFast) {
112117
this.failFast = failFast;
113118
}
114119

120+
public boolean isProfilePriority() {
121+
return this.profilePriority;
122+
}
123+
124+
public void setProfilePriority(boolean profilePriority) {
125+
this.profilePriority = profilePriority;
126+
}
127+
115128
@Override
116129
public String toString() {
117130
return new ToStringCreator(this)
@@ -121,6 +134,7 @@ public String toString() {
121134
.append("defaultContext", defaultContext)
122135
.append("profileSeparator", profileSeparator)
123136
.append("failFast", failFast)
137+
.append("profilePriority", profilePriority)
124138
.toString();
125139

126140
}

spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySources.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public List<Context> generateAutomaticContexts(List<String> profiles, boolean re
4949

5050
String defaultContext = root + "/" + properties.getDefaultContext();
5151
contexts.add(new Context(defaultContext));
52-
addProfiles(contexts, defaultContext, profiles);
52+
if (!properties.isProfilePriority()) {
53+
addProfiles(contexts, defaultContext, profiles);
54+
}
5355

5456
StringBuilder baseContext = new StringBuilder(root);
5557
if (!properties.getName().startsWith("/")) {
@@ -58,6 +60,10 @@ public List<Context> generateAutomaticContexts(List<String> profiles, boolean re
5860
// getName() defaults to ${spring.application.name} or application
5961
baseContext.append(properties.getName());
6062
contexts.add(new Context(baseContext.toString()));
63+
64+
if (properties.isProfilePriority()) {
65+
addProfiles(contexts, defaultContext, profiles);
66+
}
6167
addProfiles(contexts, baseContext.toString(), profiles);
6268

6369
if (reverse) {

spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,46 @@ public void compositePropertySourceHoldsPropertySourcesInCorrectOrder() {
233233
assertThat(propertySources.get(3).getName()).endsWith(defaultContext);
234234
}
235235

236+
@Test
237+
public void compositePropertySourceHoldsPropertySourcesInCorrectOrderWithProfilePriority() {
238+
239+
// given
240+
final String defaultContext = "someDefaultContext";
241+
final String someName = "someName";
242+
final String someProfile = "someProfile";
243+
244+
final CuratorFramework curator = mock(CuratorFramework.class);
245+
when(curator.getChildren()).thenReturn(mock(GetChildrenBuilder.class));
246+
247+
final MockEnvironment mockEnvironment = new MockEnvironment();
248+
mockEnvironment.setActiveProfiles(someProfile);
249+
250+
final ZookeeperConfigProperties properties = new ZookeeperConfigProperties();
251+
properties.setName(someName);
252+
properties.setDefaultContext(defaultContext);
253+
properties.setProfilePriority(true);
254+
255+
final ZookeeperPropertySourceLocator locator = new ZookeeperPropertySourceLocator(
256+
curator, properties);
257+
258+
// when
259+
final PropertySource<?> propertySource = locator.locate(mockEnvironment);
260+
261+
// then
262+
assertThat(propertySource).isInstanceOf(CompositePropertySource.class);
263+
264+
// and
265+
final ArrayList<PropertySource<?>> propertySources = new ArrayList<>(
266+
((CompositePropertySource) propertySource).getPropertySources());
267+
268+
assertThat(propertySources.get(0).getName())
269+
.endsWith(someName + properties.getProfileSeparator() + someProfile);
270+
assertThat(propertySources.get(1).getName()).endsWith(
271+
defaultContext + properties.getProfileSeparator() + someProfile);
272+
assertThat(propertySources.get(2).getName()).endsWith(someName);
273+
assertThat(propertySources.get(3).getName()).endsWith(defaultContext);
274+
}
275+
236276
@Configuration
237277
@EnableAutoConfiguration
238278
static class Config implements ApplicationListener<EnvironmentChangeEvent> {

0 commit comments

Comments
 (0)