Remove the need for @EnableDiscoveryClient (#245)
Enable AutoServiceRegistrationConfiguration by default.
This commit is contained in:
@@ -317,6 +317,9 @@ Commons provides the `@EnableDiscoveryClient` annotation. This looks for impleme
|
||||
|
||||
By default, implementations of `DiscoveryClient` will auto-register the local Spring Boot server with the remote discovery server. This can be disabled by setting `autoRegister=false` in `@EnableDiscoveryClient`.
|
||||
|
||||
NOTE: The use of `@EnableDiscoveryClient` is no longer required. It is enough to just have a `DiscoveryClient` implementation
|
||||
on the classpath to cause the Spring Boot application to register with the service discovery server.
|
||||
|
||||
==== Health Indicator
|
||||
|
||||
Commons creates a Spring Boot `HealthIndicator` that `DiscoveryClient` implementations can participate in by implementing `DiscoveryHealthIndicator`. To disable the composite `HealthIndicator` set `spring.cloud.discovery.client.composite-indicator.enabled=false`. A generic `HealthIndicator` based on `DiscoveryClient` is auto-configured (`DiscoveryClientHealthIndicator). To disable it, set `spring.cloud.discovery.client.health-indicator.enabled=false`. To disable the description field of the `DiscoveryClientHealthIndicator` set `spring.cloud.discovery.client.health-indicator.include-description=false`, otherwise it can bubble up as the `description` of the rolled up `HealthIndicator`.
|
||||
|
||||
@@ -21,10 +21,14 @@ import org.springframework.cloud.commons.util.SpringFactoryImportSelector;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -47,6 +51,17 @@ public class EnableDiscoveryClientImportSelector
|
||||
List<String> importsList = new ArrayList<>(Arrays.asList(imports));
|
||||
importsList.add("org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration");
|
||||
imports = importsList.toArray(new String[0]);
|
||||
} else {
|
||||
Environment env = getEnvironment();
|
||||
if(ConfigurableEnvironment.class.isInstance(env)) {
|
||||
ConfigurableEnvironment configEnv = (ConfigurableEnvironment)env;
|
||||
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("spring.cloud.service-registry.auto-registration.enabled", false);
|
||||
MapPropertySource propertySource = new MapPropertySource(
|
||||
"springCloudDiscoveryClient", map);
|
||||
configEnv.getPropertySources().addLast(propertySource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return imports;
|
||||
|
||||
@@ -4,13 +4,17 @@ import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
|
||||
@Import(AutoServiceRegistrationConfiguration.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||
public class AutoServiceRegistrationAutoConfiguration {
|
||||
|
||||
@Autowired(required = false)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.springframework.cloud.client.serviceregistry;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -8,5 +9,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(AutoServiceRegistrationProperties.class)
|
||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||
public class AutoServiceRegistrationConfiguration {
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguratio
|
||||
org.springframework.cloud.commons.util.UtilAutoConfiguration,\
|
||||
org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration,\
|
||||
org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration,\
|
||||
org.springframework.cloud.commons.httpclient.HttpClientConfiguration
|
||||
org.springframework.cloud.commons.httpclient.HttpClientConfiguration,\
|
||||
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration
|
||||
|
||||
|
||||
# Environment Post Processors
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.springframework.cloud.client.discovery;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(properties = {"spring.cloud.service-registry.auto-registration.enabled: false"})
|
||||
public class AutoRegisterPropertyFalseTests {
|
||||
@Autowired(required = false)
|
||||
AutoServiceRegistrationAutoConfiguration autoConfiguration;
|
||||
|
||||
@Autowired(required = false)
|
||||
AutoServiceRegistration autoServiceRegistration;
|
||||
|
||||
@Autowired(required = false)
|
||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties;
|
||||
|
||||
@Value("${spring.cloud.service-registry.auto-registration.enabled}")
|
||||
Boolean autoRegisterProperty;
|
||||
|
||||
@Test
|
||||
public void veryifyBeans() {
|
||||
assertNull(autoConfiguration);
|
||||
assertNull(autoServiceRegistration);
|
||||
assertNull(autoServiceRegistrationProperties);
|
||||
assertFalse(autoRegisterProperty);
|
||||
}
|
||||
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
public static class App {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.springframework.cloud.client.discovery;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class EnableDiscoveryClientAutoRegisterFalseTests {
|
||||
|
||||
@Autowired(required = false)
|
||||
AutoServiceRegistrationAutoConfiguration autoConfiguration;
|
||||
|
||||
@Autowired(required = false)
|
||||
AutoServiceRegistration autoServiceRegistration;
|
||||
|
||||
@Autowired(required = false)
|
||||
AutoServiceRegistrationProperties autoServiceRegistrationProperties;
|
||||
|
||||
@Value("${spring.cloud.service-registry.auto-registration.enabled}")
|
||||
Boolean autoRegisterProperty;
|
||||
|
||||
@Test
|
||||
public void veryifyBeans() {
|
||||
assertNull(autoConfiguration);
|
||||
assertNull(autoServiceRegistration);
|
||||
assertNull(autoServiceRegistrationProperties);
|
||||
assertFalse(autoRegisterProperty);
|
||||
}
|
||||
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Configuration
|
||||
@EnableDiscoveryClient(autoRegister = false)
|
||||
public static class App {
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
@@ -37,6 +39,7 @@ public class EnableDiscoveryClientImportSelectorTests {
|
||||
public void autoRegistrationIsEnabled() {
|
||||
configureAnnotation(true);
|
||||
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
|
||||
assertTrue(environment.getProperty("spring.cloud.service-registry.auto-registration.enabled", Boolean.class, true));
|
||||
assertThat(imports).hasSize(1);
|
||||
}
|
||||
|
||||
@@ -44,6 +47,7 @@ public class EnableDiscoveryClientImportSelectorTests {
|
||||
public void autoRegistrationIsDisabled() {
|
||||
configureAnnotation(false);
|
||||
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
|
||||
assertFalse(environment.getProperty("spring.cloud.service-registry.auto-registration.enabled", Boolean.class));
|
||||
assertThat(imports).isEmpty();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user