Skip to content

Commit dd16ce0

Browse files
author
Ryan Baxter
authored
Remove the need for @EnableDiscoveryClient (spring-projects#245)
Enable AutoServiceRegistrationConfiguration by default.
1 parent 99488a1 commit dd16ce0

File tree

8 files changed

+131
-2
lines changed

8 files changed

+131
-2
lines changed

docs/src/main/asciidoc/spring-cloud-commons.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ Commons provides the `@EnableDiscoveryClient` annotation. This looks for impleme
317317

318318
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`.
319319

320+
NOTE: The use of `@EnableDiscoveryClient` is no longer required. It is enough to just have a `DiscoveryClient` implementation
321+
on the classpath to cause the Spring Boot application to register with the service discovery server.
322+
320323
==== Health Indicator
321324

322325
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`.

spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelector.java

+15
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
import org.springframework.core.Ordered;
2222
import org.springframework.core.annotation.AnnotationAttributes;
2323
import org.springframework.core.annotation.Order;
24+
import org.springframework.core.env.ConfigurableEnvironment;
25+
import org.springframework.core.env.Environment;
26+
import org.springframework.core.env.MapPropertySource;
2427
import org.springframework.core.type.AnnotationMetadata;
2528

2629
import java.util.ArrayList;
2730
import java.util.Arrays;
31+
import java.util.LinkedHashMap;
2832
import java.util.List;
2933

3034
/**
@@ -47,6 +51,17 @@ public String[] selectImports(AnnotationMetadata metadata) {
4751
List<String> importsList = new ArrayList<>(Arrays.asList(imports));
4852
importsList.add("org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration");
4953
imports = importsList.toArray(new String[0]);
54+
} else {
55+
Environment env = getEnvironment();
56+
if(ConfigurableEnvironment.class.isInstance(env)) {
57+
ConfigurableEnvironment configEnv = (ConfigurableEnvironment)env;
58+
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
59+
map.put("spring.cloud.service-registry.auto-registration.enabled", false);
60+
MapPropertySource propertySource = new MapPropertySource(
61+
"springCloudDiscoveryClient", map);
62+
configEnv.getPropertySources().addLast(propertySource);
63+
}
64+
5065
}
5166

5267
return imports;

spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AutoServiceRegistrationAutoConfiguration.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
79
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.Import;
811

912
/**
1013
* @author Spencer Gibb
1114
*/
1215
@Configuration
13-
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
16+
@Import(AutoServiceRegistrationConfiguration.class)
17+
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
1418
public class AutoServiceRegistrationAutoConfiguration {
1519

1620
@Autowired(required = false)

spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AutoServiceRegistrationConfiguration.java

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.springframework.cloud.client.serviceregistry;
22

3+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
34
import org.springframework.boot.context.properties.EnableConfigurationProperties;
45
import org.springframework.context.annotation.Configuration;
56

@@ -8,5 +9,6 @@
89
*/
910
@Configuration
1011
@EnableConfigurationProperties(AutoServiceRegistrationProperties.class)
12+
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
1113
public class AutoServiceRegistrationConfiguration {
1214
}

spring-cloud-commons/src/main/resources/META-INF/spring.factories

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguratio
99
org.springframework.cloud.commons.util.UtilAutoConfiguration,\
1010
org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClientAutoConfiguration,\
1111
org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration,\
12-
org.springframework.cloud.commons.httpclient.HttpClientConfiguration
12+
org.springframework.cloud.commons.httpclient.HttpClientConfiguration,\
13+
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration
1314

1415

1516
# Environment Post Processors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.springframework.cloud.client.discovery;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
10+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
11+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
import static org.junit.Assert.assertFalse;
16+
import static org.junit.Assert.assertNull;
17+
18+
/**
19+
* @author Ryan Baxter
20+
*/
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(properties = {"spring.cloud.service-registry.auto-registration.enabled: false"})
23+
public class AutoRegisterPropertyFalseTests {
24+
@Autowired(required = false)
25+
AutoServiceRegistrationAutoConfiguration autoConfiguration;
26+
27+
@Autowired(required = false)
28+
AutoServiceRegistration autoServiceRegistration;
29+
30+
@Autowired(required = false)
31+
AutoServiceRegistrationProperties autoServiceRegistrationProperties;
32+
33+
@Value("${spring.cloud.service-registry.auto-registration.enabled}")
34+
Boolean autoRegisterProperty;
35+
36+
@Test
37+
public void veryifyBeans() {
38+
assertNull(autoConfiguration);
39+
assertNull(autoServiceRegistration);
40+
assertNull(autoServiceRegistrationProperties);
41+
assertFalse(autoRegisterProperty);
42+
}
43+
44+
45+
@EnableAutoConfiguration
46+
@Configuration
47+
public static class App {
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.springframework.cloud.client.discovery;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;
10+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
11+
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
import static org.junit.Assert.assertFalse;
16+
import static org.junit.Assert.assertNull;
17+
18+
/**
19+
* @author Ryan Baxter
20+
*/
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest
23+
public class EnableDiscoveryClientAutoRegisterFalseTests {
24+
25+
@Autowired(required = false)
26+
AutoServiceRegistrationAutoConfiguration autoConfiguration;
27+
28+
@Autowired(required = false)
29+
AutoServiceRegistration autoServiceRegistration;
30+
31+
@Autowired(required = false)
32+
AutoServiceRegistrationProperties autoServiceRegistrationProperties;
33+
34+
@Value("${spring.cloud.service-registry.auto-registration.enabled}")
35+
Boolean autoRegisterProperty;
36+
37+
@Test
38+
public void veryifyBeans() {
39+
assertNull(autoConfiguration);
40+
assertNull(autoServiceRegistration);
41+
assertNull(autoServiceRegistrationProperties);
42+
assertFalse(autoRegisterProperty);
43+
}
44+
45+
46+
@EnableAutoConfiguration
47+
@Configuration
48+
@EnableDiscoveryClient(autoRegister = false)
49+
public static class App {
50+
}
51+
}

spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/EnableDiscoveryClientImportSelectorTests.java

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.mock.env.MockEnvironment;
1010

1111
import static org.assertj.core.api.Assertions.assertThat;
12+
import static org.junit.Assert.assertFalse;
13+
import static org.junit.Assert.assertTrue;
1214
import static org.mockito.BDDMockito.given;
1315

1416
/**
@@ -37,13 +39,15 @@ public void setup() {
3739
public void autoRegistrationIsEnabled() {
3840
configureAnnotation(true);
3941
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
42+
assertTrue(environment.getProperty("spring.cloud.service-registry.auto-registration.enabled", Boolean.class, true));
4043
assertThat(imports).hasSize(1);
4144
}
4245

4346
@Test
4447
public void autoRegistrationIsDisabled() {
4548
configureAnnotation(false);
4649
String[] imports = this.importSelector.selectImports(this.annotationMetadata);
50+
assertFalse(environment.getProperty("spring.cloud.service-registry.auto-registration.enabled", Boolean.class));
4751
assertThat(imports).isEmpty();
4852
}
4953

0 commit comments

Comments
 (0)