Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConsulPropertySourceLocator add shared contexts #725

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public class ConsulConfigProperties {
*/
private String name;

/**
* contexts which shared with each other, lowest priority.
*/
private String[] sharedContexts;

public ConsulConfigProperties() {
}

Expand Down Expand Up @@ -189,12 +194,21 @@ public void setName(String name) {
this.name = name;
}

public String[] getSharedContexts() {
return sharedContexts;
}

public void setSharedContexts(String[] sharedContexts) {
this.sharedContexts = sharedContexts;
}

@Override
public String toString() {
return new ToStringCreator(this).append("enabled", this.enabled).append("prefixes", this.prefixes)
.append("defaultContext", this.defaultContext).append("profileSeparator", this.profileSeparator)
.append("format", this.format).append("dataKey", this.dataKey).append("aclToken", this.aclToken)
.append("watch", this.watch).append("failFast", this.failFast).append("name", this.name).toString();
.append("watch", this.watch).append("failFast", this.failFast).append("name", this.name)
.append("sharedContexts", this.sharedContexts).toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator, Consu

private final LinkedHashMap<String, Long> contextIndex = new LinkedHashMap<>();

private final ConsulPropertySources sources;

public ConsulPropertySourceLocator(ConsulClient consul, ConsulConfigProperties properties) {
this.consul = consul;
this.properties = properties;
sources = new ConsulPropertySources(properties, log);
}

public ConsulPropertySourceLocator(ConsulClient consul, ConsulConfigProperties properties, ConsulPropertySources sources) {
this.consul = consul;
this.properties = properties;
this.sources = sources;
}

@Deprecated
Expand All @@ -77,8 +86,6 @@ public PropertySource<?> locate(Environment environment) {
if (environment instanceof ConfigurableEnvironment) {
ConfigurableEnvironment env = (ConfigurableEnvironment) environment;

ConsulPropertySources sources = new ConsulPropertySources(properties, log);

List<String> profiles = Arrays.asList(env.getActiveProfiles());
this.contexts.addAll(sources.getAutomaticContexts(profiles));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public List<String> getAutomaticContexts(List<String> profiles, boolean reverse)
public List<Context> generateAutomaticContexts(List<String> profiles, boolean reverse) {
List<Context> contexts = new ArrayList<>();
for (String prefix : this.properties.getPrefixes()) {
// contexts which shared with each other
final String[] sharedContexts = properties.getSharedContexts();
if (sharedContexts != null) {
for (String sharedContext : sharedContexts) {
contexts.add(new Context(getContext(prefix, sharedContext)));
}
}

String defaultContext = getContext(prefix, properties.getDefaultContext());
List<String> suffixes = getSuffixes();
for (String suffix : suffixes) {
Expand Down