fix-1426 (#1511)
This commit is contained in:
@@ -21,10 +21,10 @@ import org.springframework.boot.actuate.health.HealthIndicator;
|
||||
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 @@ import org.springframework.web.client.RestTemplate;
|
||||
@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);
|
||||
|
||||
@@ -122,12 +122,12 @@ class KubernetesDiscoveryAutoConfigurationTests {
|
||||
@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 @@ class KubernetesDiscoveryAutoConfigurationTests {
|
||||
"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 @@ class KubernetesDiscoveryAutoConfigurationTests {
|
||||
"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 @@ class KubernetesDiscoveryAutoConfigurationTests {
|
||||
"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 @@ class KubernetesDiscoveryAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* '@ConditionalOnBlockingDiscoveryEnabled' is not matched.
|
||||
* </pre>
|
||||
*/
|
||||
@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();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* - 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
|
||||
* </pre>
|
||||
*/
|
||||
@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) {
|
||||
|
||||
@@ -22,8 +22,6 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
.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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
.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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
.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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
@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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
@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 @@ class KubernetesDiscoveryClientAutoConfigurationTests {
|
||||
.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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user