From 53f73da6303c879af1647bdb872a5dc81db9bc5a Mon Sep 17 00:00:00 2001 From: woshikid Date: Thu, 13 Oct 2022 17:30:20 +0800 Subject: [PATCH] Adjust the priority of the configs --- .../main/asciidoc/spring-cloud-zookeeper.adoc | 2 + .../config/ZookeeperConfigProperties.java | 14 +++++++ .../config/ZookeeperPropertySources.java | 8 +++- .../ZookeeperPropertySourceLocatorTests.java | 40 +++++++++++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/docs/src/main/asciidoc/spring-cloud-zookeeper.adoc b/docs/src/main/asciidoc/spring-cloud-zookeeper.adoc index c6eed2760..56b38e1f4 100644 --- a/docs/src/main/asciidoc/spring-cloud-zookeeper.adoc +++ b/docs/src/main/asciidoc/spring-cloud-zookeeper.adoc @@ -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` diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java index ae55f1c1e..5476adea5 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigProperties.java @@ -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; } @@ -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) @@ -121,6 +134,7 @@ public String toString() { .append("defaultContext", defaultContext) .append("profileSeparator", profileSeparator) .append("failFast", failFast) + .append("profilePriority", profilePriority) .toString(); } diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySources.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySources.java index 0730ff1b5..3b9bdbf53 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySources.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySources.java @@ -49,7 +49,9 @@ public List generateAutomaticContexts(List 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("/")) { @@ -58,6 +60,10 @@ public List generateAutomaticContexts(List 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) { diff --git a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java index a4f06258e..ae5642341 100644 --- a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java +++ b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java @@ -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> 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 {