From 757fe1af311cfe88bcc220223a0411f70d523f96 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 15 Feb 2017 14:06:31 -0700 Subject: [PATCH] Add support for spring.cloud.consul.config.name fixes gh-279 --- .../consul/config/ConsulConfigProperties.java | 5 + .../config/ConsulPropertySourceLocator.java | 10 +- ...tySourceLocatorAppNameCustomizedTests.java | 99 +++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorAppNameCustomizedTests.java 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 index 997e2fc9..a79e9401 100644 --- 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 @@ -62,6 +62,11 @@ public class ConsulConfigProperties { */ private boolean failFast = true; + /** + * Alternative to spring.application.name to use in looking up values in consul KV. + */ + private String name; + @PostConstruct public void init() { if (this.format == Format.FILES) { 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 12703867..f9e8e29a 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 @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; @@ -65,7 +66,14 @@ public class ConsulPropertySourceLocator implements PropertySourceLocator { public PropertySource locate(Environment environment) { if (environment instanceof ConfigurableEnvironment) { ConfigurableEnvironment env = (ConfigurableEnvironment) environment; - String appName = env.getProperty("spring.application.name"); + RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env); + + String appName = properties.getName(); + + if (appName == null) { + appName = propertyResolver.getProperty("spring.application.name"); + } + List profiles = Arrays.asList(env.getActiveProfiles()); String prefix = this.properties.getPrefix(); diff --git a/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorAppNameCustomizedTests.java b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorAppNameCustomizedTests.java new file mode 100644 index 00000000..b94dc31e --- /dev/null +++ b/spring-cloud-consul-config/src/test/java/org/springframework/cloud/consul/config/ConsulPropertySourceLocatorAppNameCustomizedTests.java @@ -0,0 +1,99 @@ +/* + * Copyright 2013-2017 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 java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.consul.ConsulProperties; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.test.annotation.DirtiesContext; + +import com.ecwid.consul.v1.ConsulClient; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author Spencer Gibb + */ +@DirtiesContext +public class ConsulPropertySourceLocatorAppNameCustomizedTests { + private static final String PREFIX = "_propertySourceLocatorAppNameCustomizedTests_config__"; + private static final String ROOT = PREFIX + UUID.randomUUID(); + private static final String VALUE1 = "testPropVal"; + private static final String TEST_PROP = "testProp"; + private static final String CONFIG_NAME = "myconfigfolder"; + private static final String KEY1 = ROOT + "/"+ CONFIG_NAME +"/"+ TEST_PROP; + private static final String VALUE2 = "testPropVal2"; + private static final String TEST_PROP2 = "testProp2"; + private static final String KEY2 = ROOT + "/"+ CONFIG_NAME +"/"+ TEST_PROP2; + + private ConfigurableApplicationContext context; + + @Configuration + @EnableAutoConfiguration + static class Config { + + } + + private ConfigurableEnvironment environment; + private ConsulClient client; + private ConsulProperties properties; + + @Before + public void setup() { + this.properties = new ConsulProperties(); + this.client = new ConsulClient(properties.getHost(), properties.getPort()); + this.client.deleteKVValues(PREFIX); + this.client.setKVValue(KEY1, VALUE1); + this.client.setKVValue(KEY2, VALUE2); + + + this.context = new SpringApplicationBuilder(Config.class) + .web(false) + .run("--spring.application.name=testConsulPropertySourceLocatorAppNameCustomized", + "--spring.cloud.consul.config.name="+CONFIG_NAME, + "--spring.cloud.consul.config.prefix="+ROOT); + + this.client = context.getBean(ConsulClient.class); + this.properties = context.getBean(ConsulProperties.class); + this.environment = context.getEnvironment(); + } + + @After + public void teardown() { + this.client.deleteKVValues(PREFIX); + this.context.close(); + } + + @Test + public void propertyLoaded() throws Exception { + String testProp = this.environment.getProperty(TEST_PROP); + assertThat("testProp was wrong", testProp, is(equalTo(VALUE1))); + + String testProp2 = this.environment.getProperty(TEST_PROP2); + assertThat("testProp2 was wrong", testProp2, is(equalTo(VALUE2))); + } +}