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
This commit is contained in:
Dave Syer
2014-11-10 08:46:02 +00:00
parent 04b6cd0f13
commit 9d55ef84d0
9 changed files with 80 additions and 11 deletions

View File

@@ -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:

View File

@@ -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);

View File

@@ -4,7 +4,8 @@ spring:
cloud:
config:
server:
basedir: target/config
git:
basedir: target/config
application:
name: configserver
jmx:

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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<String> 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<String> 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;
}
}

View File

@@ -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 {

View File

@@ -2,4 +2,5 @@ spring:
cloud:
config:
server:
basedir: target/config
git:
basedir: target/config