Make spring.cloud.discovery.enabled=false behave the same as spring.cloud.consul.discovery.enabled=false (#496)

This commit is contained in:
Olga Maciaszek-Sharma
2019-02-21 21:07:59 +01:00
committed by Ryan Baxter
parent 21d9639665
commit ed2ae397aa
3 changed files with 84 additions and 2 deletions

View File

@@ -72,7 +72,7 @@ CAUTION: If you use <<spring-cloud-consul-config,Spring Cloud Consul Config>>, t
The default service name, instance id and port, taken from the `Environment`, are `${spring.application.name}`, the Spring Context ID and `${server.port}` respectively.
To disable the Consul Discovery Client you can set `spring.cloud.consul.discovery.enabled` to `false`.
To disable the Consul Discovery Client you can set `spring.cloud.consul.discovery.enabled` to `false`. Consul Discovery Client will also be disabled when `spring.cloud.discovery.enabled` is set to `false`.
To disable the service registration you can set `spring.cloud.consul.discovery.register` to `false`.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2015 the original author or authors.
* Copyright 2013-2019 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.
@@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.cloud.consul.ConditionalOnConsulEnabled;
@@ -35,10 +36,12 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
* @author Spencer Gibb
* @author Olga Maciaszek-Sharma
*/
@Configuration
@ConditionalOnConsulEnabled
@ConditionalOnProperty(value = "spring.cloud.consul.discovery.enabled", matchIfMissing = true)
@ConditionalOnDiscoveryEnabled
@EnableConfigurationProperties
@AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class,
CommonsClientAutoConfiguration.class })

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2019-2019 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.discovery;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.cloud.commons.util.UtilAutoConfiguration;
import org.springframework.cloud.consul.ConsulAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.Assert.fail;
/**
* @author Olga Maciaszek-Sharma
*/
public class ConsulDiscoveryClientConfigurationTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void after() {
if (this.context != null && this.context.isActive()) {
this.context.close();
}
}
@Test
public void consulConfigNotLoadedWhenDiscoveryClientDisabled() {
TestPropertyValues.of("spring.cloud.discovery.enabled=false")
.applyTo(this.context);
setupContext();
assertBeanNotPresent(ConsulDiscoveryProperties.class);
assertBeanNotPresent(ConsulDiscoveryClient.class);
assertBeanNotPresent(HeartbeatProperties.class);
}
private void setupContext(Class<?>... config) {
ConfigurationPropertySources.attach(this.context.getEnvironment());
this.context.register(UtilAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
ConsulAutoConfiguration.class,
ConsulDiscoveryClientConfiguration.class);
for (Class<?> value : config) {
this.context.register(value);
}
this.context.refresh();
}
private void assertBeanNotPresent(Class beanClass) {
try {
context.getBean(beanClass);
fail("Bean of type " + beanClass + " should not have been created.");
}
catch (NoSuchBeanDefinitionException exception) {
// expected exception
}
}
}