Skip to content

Commit d969477

Browse files
author
Ryan Baxter
authored
Config server function should return empty list when discovery client disabled (#298)
1 parent 298be5a commit d969477

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ConditionalOnZookeeperDiscoveryEnabled.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
@Retention(RetentionPolicy.RUNTIME)
3434
@Target({ ElementType.TYPE, ElementType.METHOD })
3535
@ConditionalOnZookeeperEnabled
36-
@ConditionalOnProperty(value = "spring.cloud.zookeeper.discovery.enabled", matchIfMissing = true)
36+
@ConditionalOnProperty(value = ConditionalOnZookeeperDiscoveryEnabled.PROPERTY, matchIfMissing = true)
3737
public @interface ConditionalOnZookeeperDiscoveryEnabled {
38-
38+
/**
39+
* Property name.
40+
*/
41+
String PROPERTY = "spring.cloud.zookeeper.discovery.enabled";
3942
}

spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/configclient/ZookeeperConfigServerBootstrapper.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.cloud.zookeeper.discovery.configclient;
1818

19+
import java.util.Collections;
20+
1921
import org.apache.curator.framework.CuratorFramework;
2022
import org.apache.curator.x.discovery.ServiceDiscovery;
2123
import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
@@ -32,6 +34,7 @@
3234
import org.springframework.cloud.config.client.ConfigClientProperties;
3335
import org.springframework.cloud.config.client.ConfigServerInstanceProvider;
3436
import org.springframework.cloud.zookeeper.CuratorFactory;
37+
import org.springframework.cloud.zookeeper.discovery.ConditionalOnZookeeperDiscoveryEnabled;
3538
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryClient;
3639
import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties;
3740
import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance;
@@ -102,7 +105,7 @@ public void initialize(BootstrapRegistry registry) {
102105
// create instance provider
103106
registry.registerIfAbsent(ConfigServerInstanceProvider.Function.class, context -> {
104107
if (!isEnabled(context.get(Binder.class))) {
105-
return null;
108+
return (id) -> Collections.emptyList();
106109
}
107110
return context.get(ZookeeperDiscoveryClient.class)::getInstances;
108111
});
@@ -122,7 +125,9 @@ private BindHandler getBindHandler(org.springframework.boot.BootstrapContext con
122125
}
123126

124127
private boolean isEnabled(Binder binder) {
125-
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false);
128+
return binder.bind(ConfigClientProperties.CONFIG_DISCOVERY_ENABLED, Boolean.class).orElse(false) &&
129+
binder.bind(ConditionalOnZookeeperDiscoveryEnabled.PROPERTY, Boolean.class).orElse(true) &&
130+
binder.bind("spring.cloud.discovery.enabled", Boolean.class).orElse(true);
126131
}
127132

128133
}

spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/serviceregistry/ZookeeperServiceRegistryAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.beans.BeansException;
2424
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2627
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2728
import org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration;
@@ -52,6 +53,7 @@ public void setApplicationContext(ApplicationContext context) throws BeansExcept
5253
}
5354

5455
@Bean
56+
@ConditionalOnBean(ServiceDiscovery.class)
5557
@SuppressWarnings("unchecked")
5658
public ZookeeperServiceRegistry zookeeperServiceRegistry() {
5759
return new ZookeeperServiceRegistry(this.context.getBean(ServiceDiscovery.class));

spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/configclient/ZookeeperConfigServerBootstrapperTests.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public void after() {
5252
}
5353

5454
@Test
55-
public void notEnabledDoesNotAddInstanceProviderFn() {
55+
public void notEnabledReturnsEmptyList() {
5656
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
5757
.listeners(new ZookeeperTestingServer())
5858
.properties("--server.port=0", "spring.cloud.service-registry.auto-registration.enabled=false")
5959
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
6060
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
6161
.get(ConfigServerInstanceProvider.Function.class);
62-
assertThat(providerFn).as("ConfigServerInstanceProvider.Function was created when it shouldn't")
63-
.isNull();
62+
assertThat(providerFn.apply("id")).as("ConfigServerInstanceProvider.Function should return empty list")
63+
.isEmpty();
6464
})).run();
6565
CuratorFramework curatorFramework = context.getBean("curatorFramework", CuratorFramework.class);
6666
assertThat(curatorFramework).isNotNull();
@@ -69,6 +69,48 @@ public void notEnabledDoesNotAddInstanceProviderFn() {
6969
context.close();
7070
}
7171

72+
@Test
73+
public void zookeeperDiscoveryClientDisabledReturnsEmptyList() {
74+
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
75+
.listeners(new ZookeeperTestingServer())
76+
.properties("--server.port=0", "spring.cloud.config.discovery.enabled=true",
77+
"spring.cloud.zookeeper.discovery.enabled=false",
78+
"spring.cloud.zookeeper.discovery.metadata[mymetadataprop]=mymetadataval",
79+
"spring.cloud.service-registry.auto-registration.enabled=false")
80+
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
81+
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
82+
.get(ConfigServerInstanceProvider.Function.class);
83+
assertThat(providerFn.apply("id")).as("ConfigServerInstanceProvider.Function should return empty list")
84+
.isEmpty();
85+
})).run();
86+
CuratorFramework curatorFramework = context.getBean("curatorFramework", CuratorFramework.class);
87+
assertThat(curatorFramework).isNotNull();
88+
assertThatThrownBy(() ->
89+
context.getBean("configDataCuratorFramework", CuratorFramework.class)).isInstanceOf(NoSuchBeanDefinitionException.class);
90+
context.close();
91+
}
92+
93+
@Test
94+
public void discoveryClientDisabledReturnsEmptyList() {
95+
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfig.class)
96+
.listeners(new ZookeeperTestingServer())
97+
.properties("--server.port=0", "spring.cloud.config.discovery.enabled=true",
98+
"spring.cloud.discovery.enabled=false",
99+
"spring.cloud.zookeeper.discovery.metadata[mymetadataprop]=mymetadataval",
100+
"spring.cloud.service-registry.auto-registration.enabled=false")
101+
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
102+
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
103+
.get(ConfigServerInstanceProvider.Function.class);
104+
assertThat(providerFn.apply("id")).as("ConfigServerInstanceProvider.Function should return empty list")
105+
.isEmpty();
106+
})).run();
107+
CuratorFramework curatorFramework = context.getBean("curatorFramework", CuratorFramework.class);
108+
assertThat(curatorFramework).isNotNull();
109+
assertThatThrownBy(() ->
110+
context.getBean("configDataCuratorFramework", CuratorFramework.class)).isInstanceOf(NoSuchBeanDefinitionException.class);
111+
context.close();
112+
}
113+
72114
@Test
73115
public void enabledAddsInstanceProviderFn() {
74116
AtomicReference<ZookeeperDiscoveryClient> bootstrapDiscoveryClient = new AtomicReference<>();
@@ -82,7 +124,7 @@ public void enabledAddsInstanceProviderFn() {
82124
.addBootstrapRegistryInitializer(registry -> registry.addCloseListener(event -> {
83125
ConfigServerInstanceProvider.Function providerFn = event.getBootstrapContext()
84126
.get(ConfigServerInstanceProvider.Function.class);
85-
assertThat(providerFn).as("ConfigServerInstanceProvider.Function was not created when it should.")
127+
assertThat(providerFn.apply("id")).as("Should return empty list.")
86128
.isNotNull();
87129
bootstrapDiscoveryClient.set(event.getBootstrapContext().get(ZookeeperDiscoveryClient.class));
88130
})).run();

0 commit comments

Comments
 (0)