Skip to content

Commit

Permalink
Merge branch '3.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjbaxter committed Nov 20, 2023
2 parents df2d4c4 + bf5d5e9 commit 33c96b0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,21 +42,20 @@
@Configuration(proxyBeanMethods = false)
@ConditionalOnDiscoveryEnabled
@ConditionalOnKubernetesDiscoveryEnabled
@ConditionalOnBlockingDiscoveryEnabled
@ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES)
@EnableConfigurationProperties({ DiscoveryClientHealthIndicatorProperties.class,
KubernetesDiscoveryClientProperties.class })
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -223,6 +223,48 @@ void reactiveEnabledBlockingEnabledWebClientMissing() {
});
}

/**
* <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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
});
}
Expand All @@ -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);
});
}
Expand All @@ -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);
});
}
Expand All @@ -79,26 +77,27 @@ 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);
});
}

@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);
});
}

@Test
void shouldNotHaveDiscoveryClientWhenKubernetesDisabled() {
contextRunner.run(context -> {
assertThat(context).doesNotHaveBean(ReactiveDiscoveryClient.class);
assertThat(context).doesNotHaveBean(KubernetesReactiveDiscoveryClient.class);
assertThat(context).doesNotHaveBean(ReactiveDiscoveryClientHealthIndicator.class);
});
}
Expand All @@ -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);
});
}
Expand Down

0 comments on commit 33c96b0

Please sign in to comment.