From 72d7457d2f0bb4d6674fd803eaf9b761a7255c7e Mon Sep 17 00:00:00 2001 From: spencergibb Date: Tue, 26 Jan 2021 11:24:54 -0500 Subject: [PATCH] No reverse for auto contexts in ConfigData. This fixes a problem where the default context had precedence over the more specific ones. Fixes gh-280 --- .../ZookeeperConfigDataLocationResolver.java | 2 +- .../zookeeper/config/ZookeeperPropertySources.java | 8 +++++++- .../ZookeeperConfigDataIntegrationTests.java | 14 +++++++++++--- .../ZookeeperConfigDataLocationResolverTests.java | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolver.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolver.java index 43909566..809b9f58 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolver.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolver.java @@ -89,7 +89,7 @@ public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationRe ZookeeperPropertySources sources = new ZookeeperPropertySources(properties, log); List contexts = (locationUri == null || CollectionUtils.isEmpty(locationUri.getPathSegments())) - ? sources.getAutomaticContexts(profiles.getAccepted()) : getCustomContexts(locationUri); + ? sources.getAutomaticContexts(profiles.getAccepted(), false) : getCustomContexts(locationUri); // promote beans to context context.getBootstrapContext().addCloseListener(event -> { 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 d7988268..1cf8409a 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 @@ -33,6 +33,10 @@ public class ZookeeperPropertySources { } public List getAutomaticContexts(List profiles) { + return getAutomaticContexts(profiles, true); + } + + public List getAutomaticContexts(List profiles, boolean reverse) { String root = properties.getRoot(); List contexts = new ArrayList<>(); @@ -49,7 +53,9 @@ public class ZookeeperPropertySources { contexts.add(baseContext.toString()); addProfiles(contexts, baseContext.toString(), profiles); - Collections.reverse(contexts); + if (reverse) { + Collections.reverse(contexts); + } return contexts; } diff --git a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataIntegrationTests.java b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataIntegrationTests.java index 212f7a08..c6d3d9a2 100644 --- a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataIntegrationTests.java +++ b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataIntegrationTests.java @@ -55,6 +55,8 @@ public class ZookeeperConfigDataIntegrationTests { private static final Log log = LogFactory .getLog(ZookeeperConfigDataIntegrationTests.class); + public static final String APPLICATION_NAME = "testZkConfigDataIntegration"; + public static final String PREFIX = "test__configdata__"; public static final String ROOT = "/" + PREFIX + UUID.randomUUID(); @@ -65,6 +67,10 @@ public class ZookeeperConfigDataIntegrationTests { public static final String KEY_BASIC_PATH = CONTEXT + KEY_BASIC; + public static final String KEY_APP_PATH = ROOT + "/" + APPLICATION_NAME + "/" + KEY_BASIC; + + public static final String VAL_BASIC_DEFAULT = "testPropValDefault"; + public static final String VAL_BASIC = "testPropVal"; public static final String KEY_WITH_DOT = "testProp.dot"; @@ -108,7 +114,9 @@ public class ZookeeperConfigDataIntegrationTests { StringBuilder create = new StringBuilder(1024); create.append(this.curator.create().creatingParentsIfNeeded() - .forPath(KEY_BASIC_PATH, VAL_BASIC.getBytes())).append('\n'); + .forPath(KEY_BASIC_PATH, VAL_BASIC_DEFAULT.getBytes())).append('\n'); + create.append(this.curator.create().creatingParentsIfNeeded() + .forPath(KEY_APP_PATH, VAL_BASIC.getBytes())).append('\n'); create.append(this.curator.create().creatingParentsIfNeeded() .forPath(KEY_WITH_DOT_PATH, VAL_WITH_DOT.getBytes())).append('\n'); create.append(this.curator.create().creatingParentsIfNeeded() @@ -121,7 +129,7 @@ public class ZookeeperConfigDataIntegrationTests { this.context = new SpringApplicationBuilder(Config.class) .web(WebApplicationType.NONE) .run("--spring.config.import=zookeeper:" + connectString, - "--spring.application.name=testZkConfigDataIntegration", + "--spring.application.name=" + APPLICATION_NAME, "--logging.level.org.springframework.cloud.zookeeper=DEBUG", "--spring.cloud.zookeeper.config.root=" + ROOT); @@ -171,7 +179,7 @@ public class ZookeeperConfigDataIntegrationTests { String testProp = this.environment.getProperty(KEY_BASIC); assertThat(testProp).as("testProp was wrong").isEqualTo(VAL_BASIC); - this.curator.setData().forPath(KEY_BASIC_PATH, "testPropValUpdate".getBytes()); + this.curator.setData().forPath(KEY_APP_PATH, "testPropValUpdate".getBytes()); CountDownLatch latch = this.context.getBean(CountDownLatch.class); boolean receivedEvent = latch.await(15, TimeUnit.SECONDS); diff --git a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolverTests.java b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolverTests.java index f2777aa4..b1723c99 100644 --- a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolverTests.java +++ b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperConfigDataLocationResolverTests.java @@ -64,8 +64,8 @@ public class ZookeeperConfigDataLocationResolverTests { String location = "zookeeper:myhost:1234"; List locations = testResolveProfileSpecific(location); assertThat(locations).hasSize(4); - assertThat(toContexts(locations)).containsExactly("config/testapp,dev", - "config/testapp", "config/application,dev", "config/application"); + assertThat(toContexts(locations)).containsExactly("config/application", + "config/application,dev", "config/testapp", "config/testapp,dev"); } private List toContexts(List locations) {