polish & make sure config contexts are in order most to least specific
This commit is contained in:
@@ -16,27 +16,34 @@
|
||||
|
||||
package org.springframework.cloud.consul.config;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@Import(ConsulAutoConfiguration.class)
|
||||
@EnableConfigurationProperties
|
||||
@ConditionalOnProperty(name = "spring.cloud.consul.enabled", matchIfMissing = true)
|
||||
public class ConsulConfigBootstrapConfiguration {
|
||||
|
||||
@Autowired
|
||||
private ConfigurableEnvironment environment;
|
||||
private ConsulClient consul;
|
||||
|
||||
@Bean
|
||||
public ConsulConfigProperties consulConfigProperties() {
|
||||
return new ConsulConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConsulPropertySourceLocator consulPropertySourceLocator() {
|
||||
return new ConsulPropertySourceLocator();
|
||||
return new ConsulPropertySourceLocator(consul, consulConfigProperties());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.consul.config;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.validator.constraints.NotEmpty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.consul.config")
|
||||
@Data
|
||||
public class ConsulConfigProperties {
|
||||
private boolean enabled = true;
|
||||
|
||||
@NotEmpty
|
||||
private String prefix = "config";
|
||||
|
||||
@NotEmpty
|
||||
private String defaultContext = "application";
|
||||
|
||||
@NotEmpty
|
||||
private String profileSeparator = ",";
|
||||
}
|
||||
@@ -18,11 +18,10 @@ package org.springframework.cloud.consul.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||
import org.springframework.cloud.consul.ConsulProperties;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -35,11 +34,14 @@ import com.ecwid.consul.v1.ConsulClient;
|
||||
*/
|
||||
public class ConsulPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
@Autowired
|
||||
private ConsulClient consul;
|
||||
|
||||
@Autowired
|
||||
private ConsulProperties properties;
|
||||
private ConsulConfigProperties properties;
|
||||
|
||||
public ConsulPropertySourceLocator(ConsulClient consul, ConsulConfigProperties properties) {
|
||||
this.consul = consul;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertySource<?> locate(Environment environment) {
|
||||
@@ -48,10 +50,10 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator {
|
||||
String appName = env.getProperty("spring.application.name");
|
||||
List<String> profiles = Arrays.asList(env.getActiveProfiles());
|
||||
|
||||
String prefix = properties.getPrefix();
|
||||
String prefix = this.properties.getPrefix();
|
||||
List<String> contexts = new ArrayList<>();
|
||||
|
||||
String defaultContext = prefix + "/application";
|
||||
String defaultContext = prefix + "/" + this.properties.getDefaultContext();
|
||||
contexts.add(defaultContext + "/");
|
||||
addProfiles(contexts, defaultContext, profiles);
|
||||
|
||||
@@ -61,6 +63,8 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
CompositePropertySource composite = new CompositePropertySource("consul");
|
||||
|
||||
Collections.reverse(contexts);
|
||||
|
||||
for (String propertySourceContext : contexts) {
|
||||
ConsulPropertySource propertySource = create(propertySourceContext);
|
||||
propertySource.init();
|
||||
@@ -79,7 +83,7 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator {
|
||||
private void addProfiles(List<String> contexts, String baseContext,
|
||||
List<String> profiles) {
|
||||
for (String profile : profiles) {
|
||||
contexts.add(baseContext + "::" + profile + "/");
|
||||
contexts.add(baseContext + this.properties.getProfileSeparator() + profile + "/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.consul;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
Reference in New Issue
Block a user