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
This commit is contained in:
spencergibb
2021-01-26 11:24:54 -05:00
parent 0d5109acd0
commit 72d7457d2f
4 changed files with 21 additions and 7 deletions

View File

@@ -89,7 +89,7 @@ public class ZookeeperConfigDataLocationResolver implements ConfigDataLocationRe
ZookeeperPropertySources sources = new ZookeeperPropertySources(properties, log);
List<String> 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 -> {

View File

@@ -33,6 +33,10 @@ public class ZookeeperPropertySources {
}
public List<String> getAutomaticContexts(List<String> profiles) {
return getAutomaticContexts(profiles, true);
}
public List<String> getAutomaticContexts(List<String> profiles, boolean reverse) {
String root = properties.getRoot();
List<String> 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;
}

View File

@@ -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);

View File

@@ -64,8 +64,8 @@ public class ZookeeperConfigDataLocationResolverTests {
String location = "zookeeper:myhost:1234";
List<ZookeeperConfigDataResource> 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<String> toContexts(List<ZookeeperConfigDataResource> locations) {