From 9d55ef84d0ea89f4f8ecd20318555704df2a70b9 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 10 Nov 2014 08:46:02 +0000 Subject: [PATCH] Extract config server properties to Java bean ConfigServerProperties is now used to store global properties for the config server (like the path prefix and bootstrap flag) under spring.cloud.config.server.* so git specific ones have been pushed down to spring.cloud.config.server.git.* (similarly for git->native). At the same time introduce spring.cloud.config.server.defaultLabel for the deafult git label (defaults to "master"). Fixes gh-28 --- docs/src/main/asciidoc/quickstart.adoc | 2 +- .../test/java/sample/ApplicationTests.java | 2 +- .../src/test/resources/server.yml | 3 +- .../server/ConfigServerConfiguration.java | 6 ++- .../server/ConfigServerMvcConfiguration.java | 7 ++- .../config/server/ConfigServerProperties.java | 51 +++++++++++++++++++ .../config/server/EnvironmentController.java | 15 ++++-- .../cloud/config/server/ApplicationTests.java | 2 +- .../src/test/resources/configserver-test.yml | 3 +- 9 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerProperties.java diff --git a/docs/src/main/asciidoc/quickstart.adoc b/docs/src/main/asciidoc/quickstart.adoc index 5ce2ae19..70a6307c 100644 --- a/docs/src/main/asciidoc/quickstart.adoc +++ b/docs/src/main/asciidoc/quickstart.adoc @@ -18,7 +18,7 @@ $ curl localhost:8888/foo/development ---- The default strategy for locating property sources is to clone a git -repository (at "spring.cloud.config.server.uri") and use it to +repository (at "spring.cloud.config.server.git.uri") and use it to initialize a mini `SpringApplication`. The mini-application's `Environment` is used to enumerate property sources and publish them via a JSON endpoint. The service has resources in the form: diff --git a/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java b/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java index a18bba26..a219d1a9 100644 --- a/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java +++ b/spring-cloud-config-sample/src/test/java/sample/ApplicationTests.java @@ -35,7 +35,7 @@ public class ApplicationTests { ConfigurableApplicationContext context = SpringApplication.run( org.springframework.cloud.config.server.ConfigServerApplication.class, "--server.port=" + configPort, "--spring.config.name=server", - "--spring.cloud.config.server.uri=" + repo); + "--spring.cloud.config.server.git.uri=" + repo); configPort = ((EmbeddedWebApplicationContext) context) .getEmbeddedServletContainer().getPort(); System.setProperty("config.port", "" + configPort); diff --git a/spring-cloud-config-sample/src/test/resources/server.yml b/spring-cloud-config-sample/src/test/resources/server.yml index ba890c28..84ae4e71 100644 --- a/spring-cloud-config-sample/src/test/resources/server.yml +++ b/spring-cloud-config-sample/src/test/resources/server.yml @@ -4,7 +4,8 @@ spring: cloud: config: server: - basedir: target/config + git: + basedir: target/config application: name: configserver jmx: diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java index cf8d4a46..dd58a03f 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerConfiguration.java @@ -18,6 +18,7 @@ package org.springframework.cloud.config.server; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @@ -29,13 +30,14 @@ import org.springframework.core.env.ConfigurableEnvironment; */ @Configuration @ConditionalOnMissingBean(EnvironmentRepository.class) +@EnableConfigurationProperties(ConfigServerProperties.class) public class ConfigServerConfiguration { @Configuration @Profile("native") protected static class NativeRepositoryConfiguration { @Bean - @ConfigurationProperties("spring.cloud.config.server") + @ConfigurationProperties("spring.cloud.config.server.native") public SpringApplicationEnvironmentRepository repository() { return new SpringApplicationEnvironmentRepository(); } @@ -48,7 +50,7 @@ public class ConfigServerConfiguration { private ConfigurableEnvironment environment; @Bean - @ConfigurationProperties("spring.cloud.config.server") + @ConfigurationProperties("spring.cloud.config.server.git") public JGitEnvironmentRepository repository() { return new JGitEnvironmentRepository(environment); } diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerMvcConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerMvcConfiguration.java index 359f4fd8..01aa80f2 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerMvcConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerMvcConfiguration.java @@ -34,10 +34,15 @@ public class ConfigServerMvcConfiguration { @Autowired private EnvironmentRepository repository; + + @Autowired + private ConfigServerProperties server; @Bean public EnvironmentController environmentController() { - return new EnvironmentController(repository, encryptionController()); + EnvironmentController controller = new EnvironmentController(repository, encryptionController()); + controller.setDefaultLabel(server.getDefaultLabel()); + return controller; } @Bean diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerProperties.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerProperties.java new file mode 100644 index 00000000..d0fc16b8 --- /dev/null +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/ConfigServerProperties.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013-2014 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.config.server; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * @author Dave Syer + * + */ +@ConfigurationProperties("spring.cloud.config.server") +public class ConfigServerProperties { + + public static final String MASTER = "master"; + + private boolean bootstrap; + private String prefix; + private String defaultLabel = ConfigServerProperties.MASTER; + public String getDefaultLabel() { + return defaultLabel; + } + public void setDefaultLabel(String defaultLabel) { + this.defaultLabel = defaultLabel; + } + public boolean isBootstrap() { + return bootstrap; + } + public void setBootstrap(boolean bootstrap) { + this.bootstrap = bootstrap; + } + public String getPrefix() { + return prefix; + } + public void setPrefix(String prefix) { + this.prefix = prefix; + } + +} diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java index 365d298a..115b4c48 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/EnvironmentController.java @@ -34,6 +34,8 @@ public class EnvironmentController { private EncryptionController encryption; + private String defaultLabel = ConfigServerProperties.MASTER; + @Autowired public EnvironmentController(EnvironmentRepository repository, EncryptionController encryption) { @@ -44,7 +46,7 @@ public class EnvironmentController { @RequestMapping("/{name}/{profiles:.*[^-].*}") public Environment master(@PathVariable String name, @PathVariable String profiles) { - return labelled(name, profiles, "master"); + return labelled(name, profiles, defaultLabel); } @RequestMapping("/{name}/{profiles}/{label}") @@ -56,7 +58,7 @@ public class EnvironmentController { @RequestMapping("/{name}-{profiles}.properties") public ResponseEntity properties(@PathVariable String name, @PathVariable String profiles) throws IOException { - return labelledProperties(name, profiles, "master"); + return labelledProperties(name, profiles, defaultLabel); } @RequestMapping("/{label}/{name}-{profiles}.properties") @@ -92,7 +94,7 @@ public class EnvironmentController { @RequestMapping({ "/{name}-{profiles}.yml", "/{name}-{profiles}.yaml" }) public ResponseEntity yaml(@PathVariable String name, @PathVariable String profiles) throws Exception { - return labelledYaml(name, profiles, "master"); + return labelledYaml(name, profiles, defaultLabel); } @RequestMapping({ "/{label}/{name}-{profiles}.yml", "/{label}/{name}-{profiles}.yaml" }) @@ -180,4 +182,11 @@ public class EnvironmentController { return map; } + /** + * @param defaultLabel + */ + public void setDefaultLabel(String defaultLabel) { + this.defaultLabel = defaultLabel; + } + } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ApplicationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ApplicationTests.java index 9a2029bc..e187df81 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ApplicationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/ApplicationTests.java @@ -18,7 +18,7 @@ import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = ConfigServerApplication.class) -@IntegrationTest({"server.port:0", "spring.config.name:configserver", "spring.cloud.config.server.uri:file:./target/test-classes/config-repo"}) +@IntegrationTest({"server.port:0", "spring.config.name:configserver", "spring.cloud.config.server.git.uri:file:./target/test-classes/config-repo"}) @WebAppConfiguration @ActiveProfiles("test") public class ApplicationTests { diff --git a/spring-cloud-config-server/src/test/resources/configserver-test.yml b/spring-cloud-config-server/src/test/resources/configserver-test.yml index ee202fcd..5c0101a5 100644 --- a/spring-cloud-config-server/src/test/resources/configserver-test.yml +++ b/spring-cloud-config-server/src/test/resources/configserver-test.yml @@ -2,4 +2,5 @@ spring: cloud: config: server: - basedir: target/config + git: + basedir: target/config