diff --git a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientBlockingAutoConfiguration.java b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientBlockingAutoConfiguration.java index 79f607a180..1b2752226b 100644 --- a/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientBlockingAutoConfiguration.java +++ b/spring-cloud-kubernetes-discovery/src/main/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientBlockingAutoConfiguration.java @@ -21,10 +21,10 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.cloud.CloudPlatform; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled; import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled; import org.springframework.cloud.client.ConditionalOnDiscoveryHealthIndicatorEnabled; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; @@ -42,6 +42,7 @@ @Configuration(proxyBeanMethods = false) @ConditionalOnDiscoveryEnabled @ConditionalOnKubernetesDiscoveryEnabled +@ConditionalOnBlockingDiscoveryEnabled @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) @EnableConfigurationProperties({ DiscoveryClientHealthIndicatorProperties.class, KubernetesDiscoveryClientProperties.class }) @@ -49,14 +50,12 @@ class KubernetesDiscoveryClientBlockingAutoConfiguration { @Bean @ConditionalOnMissingBean - @ConditionalOnMissingClass("org.springframework.web.reactive.function.client.WebClient") RestTemplate restTemplate() { return new RestTemplateBuilder().build(); } @Bean @ConditionalOnMissingBean - @ConditionalOnMissingClass("org.springframework.web.reactive.function.client.WebClient") KubernetesDiscoveryClient kubernetesDiscoveryClient(RestTemplate restTemplate, KubernetesDiscoveryClientProperties properties) { return new KubernetesDiscoveryClient(restTemplate, properties); diff --git a/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryAutoConfigurationTests.java b/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryAutoConfigurationTests.java index c62e61ef58..e7e6c50202 100644 --- a/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryAutoConfigurationTests.java +++ b/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryAutoConfigurationTests.java @@ -122,12 +122,12 @@ void reactiveDisabledBlockingEnabledWebClientPresent() { @Test void reactiveDisabledBlockingEnabledWebClientMissing() { setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES", - "spring.cloud.discovery.reactive.enabled=false", + "spring.cloud.discovery.reactive.enabled=false", "spring.cloud.discovery.blocking.enabled=false", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver"); applicationContextRunner.run(context -> { assertThat(context).doesNotHaveBean(RestTemplate.class); assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); - assertThat(context).getBean("indicatorInitializer").isNotNull(); + assertThat(context).getBean("indicatorInitializer").isNull(); assertThat(context).doesNotHaveBean(WebClient.Builder.class); assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); @@ -147,8 +147,8 @@ void reactiveDisabledBlockingEnabledWebClientMissingHealthIndicatorMissing() { "spring.cloud.discovery.reactive.enabled=false", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(RestTemplate.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(RestTemplate.class); + assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); assertThat(context).doesNotHaveBean("indicatorInitializer"); assertThat(context).doesNotHaveBean(WebClient.Builder.class); @@ -169,8 +169,8 @@ void blockingHealthIndicatorDisabled() { "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver", "spring.cloud.discovery.client.health-indicator.enabled=false"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(RestTemplate.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(RestTemplate.class); + assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); assertThat(context).doesNotHaveBean("indicatorInitializer"); assertThat(context).hasSingleBean(WebClient.Builder.class); @@ -191,8 +191,8 @@ void reactiveEnabledBlockingEnabledWebClientPresent() { "spring.cloud.discovery.reactive.enabled=true", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver"); applicationContextRunner.run(context -> { - assertThat(context).doesNotHaveBean(RestTemplate.class); - assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).hasSingleBean(RestTemplate.class); + assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); assertThat(context).getBean("indicatorInitializer").isNotNull(); assertThat(context).hasSingleBean(WebClient.Builder.class); @@ -223,6 +223,48 @@ void reactiveEnabledBlockingEnabledWebClientMissing() { }); } + /** + *
+ * '@ConditionalOnBlockingDiscoveryEnabled' is not matched. + *+ */ + @Test + void testBlockingDisabled() { + setupWithFilteredClassLoader(WebClient.class, "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.discovery.reactive.enabled=true", "spring.cloud.discovery.blocking.enabled=false", + "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver"); + applicationContextRunner.run(context -> { + assertThat(context).doesNotHaveBean(RestTemplate.class); + assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); + assertThat(context).getBean("indicatorInitializer").isNull(); + + assertThat(context).doesNotHaveBean(WebClient.Builder.class); + assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).getBean("kubernetesReactiveDiscoveryClientHealthIndicator").isNull(); + }); + } + + /** + *
+ * - WebClient is on the classpath (this is asserted via the presence of beans that come + * from the reactive auto-configuration) + * - This has no impact of the creation of the blocking discovery client + *+ */ + @Test + void testFor1426Issue() { + setupWithFilteredClassLoader(null, "spring.main.cloud-platform=KUBERNETES", + "spring.cloud.discovery.reactive.enabled=true", "spring.cloud.discovery.blocking.enabled=true", + "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver"); + applicationContextRunner.run(context -> { + // blocking client is present + assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + + // reactive client is present + assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + }); + } + private ApplicationContextRunner applicationContextRunner; private void setupWithFilteredClassLoader(Class> cls, String... properties) { diff --git a/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java b/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java index 0f9c2698dc..54fb77860e 100644 --- a/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java +++ b/spring-cloud-kubernetes-discovery/src/test/java/org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfigurationTests.java @@ -22,8 +22,6 @@ import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration; -import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; import org.springframework.cloud.client.discovery.health.reactive.ReactiveDiscoveryClientHealthIndicator; import org.springframework.cloud.commons.util.UtilAutoConfiguration; @@ -45,8 +43,8 @@ void shouldWorkWithDefaults() { .withPropertyValues("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver") .withClassLoader(new FilteredClassLoader("org.springframework.web.reactive")).run(context -> { - assertThat(context).hasSingleBean(DiscoveryClient.class); - assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(KubernetesDiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); }); } @@ -57,8 +55,8 @@ void shouldNotHaveDiscoveryClientWhenDiscoveryDisabled() { .withPropertyValues("spring.cloud.discovery.enabled=false", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver") .run(context -> { - assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class); - assertThat(context).doesNotHaveBean(DiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); }); } @@ -69,8 +67,8 @@ void shouldNotHaveDiscoveryClientWhenKubernetesDiscoveryDisabled() { .withPropertyValues("spring.cloud.kubernetes.discovery.enabled=false", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver") .run(context -> { - assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class); - assertThat(context).doesNotHaveBean(DiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); }); } @@ -79,10 +77,11 @@ void shouldNotHaveDiscoveryClientWhenKubernetesDiscoveryDisabled() { void shouldHaveReactiveDiscoveryClient() { contextRunner .withPropertyValues("spring.main.cloud-platform=KUBERNETES", + "spring.cloud.discovery.blocking.enabled=false", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver") .run(context -> { - assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class); - assertThat(context).doesNotHaveBean(DiscoveryClient.class); + assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesDiscoveryClient.class); assertThat(context).hasSingleBean(ReactiveDiscoveryClientHealthIndicator.class); }); } @@ -90,7 +89,7 @@ void shouldHaveReactiveDiscoveryClient() { @Test void shouldNotHaveDiscoveryClientWhenReactiveDiscoveryDisabled() { contextRunner.withPropertyValues("spring.cloud.discovery.reactive.enabled=false").run(context -> { - assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); }); } @@ -98,7 +97,7 @@ void shouldNotHaveDiscoveryClientWhenReactiveDiscoveryDisabled() { @Test void shouldNotHaveDiscoveryClientWhenKubernetesDisabled() { contextRunner.run(context -> { - assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class); + assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); }); } @@ -109,7 +108,7 @@ void worksWithoutActuator() { .withPropertyValues("spring.main.cloud-platform=KUBERNETES", "spring.cloud.kubernetes.discovery.discovery-server-url=http://k8sdiscoveryserver") .withClassLoader(new FilteredClassLoader("org.springframework.boot.actuate")).run(context -> { - assertThat(context).hasSingleBean(ReactiveDiscoveryClient.class); + assertThat(context).hasSingleBean(KubernetesReactiveDiscoveryClient.class); assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class); }); }