Skip to content

Commit fdde93e

Browse files
author
黎木健
committed
support more config namespaces
1 parent b4e3c39 commit fdde93e

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,19 @@
4040
* Client</a>. Configuration is loaded into the Spring Environment during the special
4141
* "bootstrap" phase. Configuration is stored in the {@code /config} namespace by default.
4242
* Multiple {@code PropertySource} instances are created based on the application's name
43-
* and the active profiles that mimicks the Spring Cloud Config order of resolving
44-
* properties. For example, an application with the name "testApp" and with the "dev"
45-
* profile will have the following property sources created:
43+
* and the active profiles that mimics the Spring Cloud Config order of resolving
44+
* properties.What's more,it supports multiple business namespaces through {@code defaultContext}.
45+
* For example, an application with the name "testApp",and with the "dev"
46+
* profile,with "namespace1,namespace2" defaultContext will have the following property sources
47+
* created:
4648
*
4749
* <pre>{@code
4850
* config/testApp,dev
4951
* config/testApp
50-
* config/application,dev
51-
* config/application
52+
* config/namespace1,dev
53+
* config/namespace1
54+
* config/namespace2,dev
55+
* config/namespace2
5256
* }</pre>
5357
*
5458
* The most specific property source is at the top, with the least specific at the bottom.
@@ -58,6 +62,7 @@
5862
* named "testApp".
5963
*
6064
* @author Spencer Gibb
65+
* @author lemonJ
6166
* @since 1.0.0
6267
*/
6368
public class ZookeeperPropertySourceLocator implements PropertySourceLocator {
@@ -107,16 +112,4 @@ public PropertySource<?> locate(Environment environment) {
107112
@PreDestroy
108113
public void destroy() {
109114
}
110-
111-
private PropertySource<CuratorFramework> create(String context) {
112-
return new ZookeeperPropertySource(context, this.curator);
113-
}
114-
115-
private void addProfiles(List<String> contexts, String baseContext,
116-
List<String> profiles) {
117-
for (String profile : profiles) {
118-
contexts.add(baseContext + this.properties.getProfileSeparator() + profile);
119-
}
120-
}
121-
122115
}

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@
1616

1717
package org.springframework.cloud.zookeeper.config;
1818

19-
import java.util.ArrayList;
19+
import java.util.Arrays;
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.stream.Collectors;
23+
import java.util.stream.Stream;
2324

2425
import org.apache.commons.logging.Log;
2526
import org.apache.curator.framework.CuratorFramework;
2627

2728
import org.springframework.core.style.ToStringCreator;
29+
import org.springframework.util.StringUtils;
2830

2931
public class ZookeeperPropertySources {
3032
private final ZookeeperConfigProperties properties;
@@ -44,22 +46,23 @@ public List<String> getAutomaticContexts(List<String> profiles, boolean reverse)
4446
}
4547

4648
public List<Context> generateAutomaticContexts(List<String> profiles, boolean reverse) {
47-
List<Context> contexts = new ArrayList<>();
4849
String root = properties.getRoot();
4950

50-
String defaultContext = root + "/" + properties.getDefaultContext();
51-
contexts.add(new Context(defaultContext));
52-
addProfiles(contexts, defaultContext, profiles);
53-
54-
StringBuilder baseContext = new StringBuilder(root);
55-
if (!properties.getName().startsWith("/")) {
56-
baseContext.append("/");
57-
}
58-
// getName() defaults to ${spring.application.name} or application
59-
baseContext.append(properties.getName());
60-
contexts.add(new Context(baseContext.toString()));
61-
addProfiles(contexts, baseContext.toString(), profiles);
62-
51+
//properties.getDefaultContext() may contain some configs and the former config owns higher priority.
52+
List<String> defaultContextList = Arrays.stream(properties.getDefaultContext().split(properties.getProfileSeparator())).collect(Collectors.toList());
53+
Collections.reverse(defaultContextList);
54+
String name = properties.getName().startsWith("/") ? properties.getName().substring(1) : properties.getName();
55+
defaultContextList.add(name);
56+
57+
List<Context> contexts = defaultContextList.stream()
58+
.filter(StringUtils::hasLength)
59+
.flatMap(contextString -> {
60+
Context context = new Context(root + "/" + contextString);
61+
Stream<Context> profileContextStream = profiles.stream()
62+
.map(profile -> new Context(context.getPath() + this.properties.getProfileSeparator() + profile, profile));
63+
return Stream.concat(Stream.of(context), profileContextStream);
64+
}
65+
).distinct().collect(Collectors.toList());
6366
if (reverse) {
6467
Collections.reverse(contexts);
6568
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ public void propertyLoadedAndUpdated() throws Exception {
198198
public void compositePropertySourceHoldsPropertySourcesInCorrectOrder() {
199199

200200
// given
201-
final String defaultContext = "someDefaultContext";
201+
final String defaultContext = "someDefaultContext1,someDefaultContext2";
202+
String[] defaultContextArray = defaultContext.split(",");
202203
final String someName = "someName";
203204
final String someProfile = "someProfile";
204205

@@ -229,8 +230,11 @@ public void compositePropertySourceHoldsPropertySourcesInCorrectOrder() {
229230
.endsWith(someName + properties.getProfileSeparator() + someProfile);
230231
assertThat(propertySources.get(1).getName()).endsWith(someName);
231232
assertThat(propertySources.get(2).getName()).endsWith(
232-
defaultContext + properties.getProfileSeparator() + someProfile);
233-
assertThat(propertySources.get(3).getName()).endsWith(defaultContext);
233+
defaultContextArray[0] + properties.getProfileSeparator() + someProfile);
234+
assertThat(propertySources.get(3).getName()).endsWith(defaultContextArray[0]);
235+
assertThat(propertySources.get(4).getName()).endsWith(
236+
defaultContextArray[1] + properties.getProfileSeparator() + someProfile);
237+
assertThat(propertySources.get(5).getName()).endsWith(defaultContextArray[1]);
234238
}
235239

236240
@Configuration

0 commit comments

Comments
 (0)