From 01949ffac722346fac92cd41b82df55270203b86 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 26 May 2015 12:15:35 -0600 Subject: [PATCH] polish & make sure config contexts are in order most to least specific --- .../ConsulConfigBootstrapConfiguration.java | 13 ++++-- .../consul/config/ConsulConfigProperties.java | 40 +++++++++++++++++++ .../config/ConsulPropertySourceLocator.java | 20 ++++++---- .../cloud/consul/ConsulProperties.java | 4 -- 4 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java index efdbc7e3..d4d6cd71 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigBootstrapConfiguration.java @@ -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()); } } diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java new file mode 100644 index 00000000..b50b8c26 --- /dev/null +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulConfigProperties.java @@ -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 = ","; +} diff --git a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java index b35a2215..f96b8d42 100644 --- a/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java +++ b/spring-cloud-consul-config/src/main/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocator.java @@ -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 profiles = Arrays.asList(env.getActiveProfiles()); - String prefix = properties.getPrefix(); + String prefix = this.properties.getPrefix(); List 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 contexts, String baseContext, List profiles) { for (String profile : profiles) { - contexts.add(baseContext + "::" + profile + "/"); + contexts.add(baseContext + this.properties.getProfileSeparator() + profile + "/"); } } } diff --git a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java index d4ecf8da..3015169b 100644 --- a/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java +++ b/spring-cloud-consul-core/src/main/java/org/springframework/cloud/consul/ConsulProperties.java @@ -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;