Prefer WebClient to RestTemplate for Zipkin's Sender

Previously, a Webclient-based sender was only for reactive web
applications, falling back to a RestTemplate-based sender in all
other cases.

With this commit we now prefer to use WebClient if it is available,
irrespective of the web application type. The assumption is that
if the user has WebClient on the classpath, it's either a reactive
web application, or it's a servlet web application or non-web
application but WebClient is preferred.

See gh-32529
This commit is contained in:
Marcin Grzejszczak 2022-09-28 16:47:34 +02:00 committed by Andy Wilkinson
parent b325edbc55
commit cd3b3d468a
2 changed files with 6 additions and 5 deletions

View File

@ -28,7 +28,6 @@ import zipkin2.reporter.urlconnection.URLConnectionSender;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
@ -46,8 +45,8 @@ import org.springframework.web.reactive.function.client.WebClient;
class ZipkinConfigurations {
@Configuration(proxyBeanMethods = false)
@Import({ UrlConnectionSenderConfiguration.class, RestTemplateSenderConfiguration.class,
WebClientSenderConfiguration.class })
@Import({ UrlConnectionSenderConfiguration.class, WebClientSenderConfiguration.class,
RestTemplateSenderConfiguration.class })
static class SenderConfiguration {
}
@ -85,7 +84,7 @@ class ZipkinConfigurations {
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
@ConditionalOnClass(WebClient.class)
@EnableConfigurationProperties(ZipkinProperties.class)
static class WebClientSenderConfiguration {

View File

@ -77,7 +77,9 @@ class ZipkinConfigurationsSenderConfigurationTests {
@Test
void shouldUseRestTemplateSenderIfUrlConnectionSenderIsNotAvailableAndWebAppIsNotReactive() {
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
.withClassLoader(
new FilteredClassLoader("zipkin2.reporter.urlconnection", "org.springframework.web.reactive"))
.run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);