Disable config server client by default in config server

This change adds a listener that inserts a PropertySource really early
so that spring.cloud.config.enabled=false is the default in a config
server. It can be overridden if the user primes the Environment with
spring.cloud.config.enabled=true.

See gh-104
This commit is contained in:
Dave Syer
2015-03-17 15:14:32 +00:00
parent b49ceed3c5
commit 83ef3e4b74
8 changed files with 252 additions and 25 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright 2014-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.config.server;
import java.util.Collections;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
/**
* Normally you don't want the config server to be a config client itself, so this
* listener disables the config client unless <code>spring.cloud.config.enabled</code> is
* explicitly "true". It has to be "true" at the time this listener is fired, which means
* <b>before</b> the <code>bootstrap.yml</code> is parsed, which in turn means to you need
* to launch the application with an existing primed {@link Environment} (e.g. via System
* properties or a {@link SpringApplicationBuilder}). This is the same rule of precedence
* as for anything else affecting the bootstrap process itself, e.g. setting
* <code>spring.cloud.bootstrap.name</code> to something other than "bootstrap".
*
* @author Dave Syer
*
*/
public class ConfigServerBootstrapApplicationListener implements
ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 4;
private int order = DEFAULT_ORDER;
private PropertySource<?> propertySource = new MapPropertySource(
"configServerClient", Collections.<String, Object> singletonMap(
"spring.cloud.config.enabled", "false"));
public void setOrder(int order) {
this.order = order;
}
@Override
public int getOrder() {
return this.order;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
if (!environment.resolvePlaceholders("${spring.cloud.config.enabled:false}")
.equalsIgnoreCase("true")) {
if (!environment.getPropertySources().contains(propertySource.getName())) {
environment.getPropertySources().addLast(propertySource);
}
}
}
}

View File

@@ -32,21 +32,25 @@ import org.springframework.context.annotation.Import;
* @author Dave Syer
*
*/
@ConditionalOnProperty("spring.cloud.config.server.bootstrap")
@Configuration
@Import(ConfigServerConfiguration.class)
public class ConfigServerBootstrapConfiguration {
@Autowired
private EnvironmentRepository repository;
@ConditionalOnProperty("spring.cloud.config.server.bootstrap")
@Import(ConfigServerConfiguration.class)
protected static class LocalPropertySourceLocatorConfiguration {
@Autowired
private ConfigClientProperties client;
@Autowired
private EnvironmentRepository repository;
@Autowired
private ConfigClientProperties client;
@Bean
public EnvironmentRepositoryPropertySourceLocator environmentRepositoryPropertySourceLocator() {
return new EnvironmentRepositoryPropertySourceLocator(repository,
client.getName(), client.getProfile(), client.getLabel());
}
@Bean
public EnvironmentRepositoryPropertySourceLocator environmentRepositoryPropertySourceLocator() {
return new EnvironmentRepositoryPropertySourceLocator(repository,
client.getName(), client.getProfile(), client.getLabel());
}
}

View File

@@ -1,3 +1,7 @@
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.config.server.ConfigServerBootstrapConfiguration
org.springframework.cloud.config.server.ConfigServerBootstrapConfiguration
# Application listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.config.server.ConfigServerBootstrapApplicationListener

View File

@@ -21,8 +21,7 @@ import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ConfigServerApplication.class)
@IntegrationTest({ "debug", "server.port:0",
"spring.cloud.bootstrap.name:enable-bootstrap" })
@IntegrationTest({ "server.port:0", "spring.cloud.bootstrap.name:enable-bootstrap" })
@WebAppConfiguration
@ActiveProfiles("test")
public class BootstrapConfigServerIntegrationTests {
@@ -43,8 +42,8 @@ public class BootstrapConfigServerIntegrationTests {
Environment environment = new TestRestTemplate().getForObject("http://localhost:"
+ port + "/foo/development/", Environment.class);
assertFalse(environment.getPropertySources().isEmpty());
assertEquals("bar", environment.getPropertySources().get(0)
.getSource().get("info.foo"));
assertEquals("bar",
environment.getPropertySources().get(0).getSource().get("info.foo"));
}
@Test

View File

@@ -0,0 +1,71 @@
package org.springframework.cloud.config.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.ConfigClientOffIntegrationTests.TestConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@IntegrationTest("server.port:0")
@WebAppConfiguration
@ActiveProfiles("test")
public class ConfigClientOffIntegrationTests {
@Value("${local.server.port}")
private int port;
@Autowired
private ApplicationContext context;
@BeforeClass
public static void init() throws IOException {
ConfigServerTestUtils.prepareLocalRepo();
}
@Test
public void contextLoads() {
Environment environment = new TestRestTemplate().getForObject("http://localhost:"
+ port + "/foo/development/", Environment.class);
assertTrue(environment.getPropertySources().isEmpty());
}
@Test
public void configClientDisabled() throws Exception {
assertEquals(0, BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
ConfigServicePropertySourceLocator.class).length);
}
@Configuration
@Import(ConfigServerApplication.class)
protected static class TestConfiguration {
@Bean
public EnvironmentRepository environmentRepository() {
return Mockito.mock(EnvironmentRepository.class);
}
}
}

View File

@@ -0,0 +1,71 @@
package org.springframework.cloud.config.server;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.server.ConfigClientOnIntegrationTests.TestConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@IntegrationTest({ "server.port:0", "spring.cloud.config.enabled:true" })
@WebAppConfiguration
@ActiveProfiles("test")
public class ConfigClientOnIntegrationTests {
@Value("${local.server.port}")
private int port;
@Autowired
private ApplicationContext context;
@BeforeClass
public static void init() throws IOException {
ConfigServerTestUtils.prepareLocalRepo();
}
@Test
public void contextLoads() {
Environment environment = new TestRestTemplate().getForObject("http://localhost:"
+ port + "/foo/development/", Environment.class);
assertTrue(environment.getPropertySources().isEmpty());
}
@Test
public void configClientEnabled() throws Exception {
assertEquals(1, BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,
ConfigServicePropertySourceLocator.class).length);
}
@Configuration
@Import(ConfigServerApplication.class)
protected static class TestConfiguration {
@Bean
public EnvironmentRepository environmentRepository() {
return Mockito.mock(EnvironmentRepository.class);
}
}
}