Merge pull request #32529 from marcingrzejszczak

* gh-32529:
  Polish "Prefer WebClient to RestTemplate for Zipkin's Sender"
  Prefer WebClient to RestTemplate for Zipkin's Sender

Closes gh-32529
This commit is contained in:
Andy Wilkinson 2022-09-28 19:07:40 +01:00
commit 13c638ba39
2 changed files with 59 additions and 16 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.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 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.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -46,8 +45,8 @@ import org.springframework.web.reactive.function.client.WebClient;
class ZipkinConfigurations { class ZipkinConfigurations {
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@Import({ UrlConnectionSenderConfiguration.class, RestTemplateSenderConfiguration.class, @Import({ UrlConnectionSenderConfiguration.class, WebClientSenderConfiguration.class,
WebClientSenderConfiguration.class }) RestTemplateSenderConfiguration.class })
static class SenderConfiguration { static class SenderConfiguration {
} }
@ -85,7 +84,7 @@ class ZipkinConfigurations {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) @ConditionalOnClass(WebClient.class)
@EnableConfigurationProperties(ZipkinProperties.class) @EnableConfigurationProperties(ZipkinProperties.class)
static class WebClientSenderConfiguration { static class WebClientSenderConfiguration {

View File

@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -46,6 +47,9 @@ class ZipkinConfigurationsSenderConfigurationTests {
private final ReactiveWebApplicationContextRunner reactiveContextRunner = new ReactiveWebApplicationContextRunner() private final ReactiveWebApplicationContextRunner reactiveContextRunner = new ReactiveWebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SenderConfiguration.class)); .withConfiguration(AutoConfigurations.of(SenderConfiguration.class));
private final WebApplicationContextRunner servletContextRunner = new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(SenderConfiguration.class));
@Test @Test
void shouldSupplyBeans() { void shouldSupplyBeans() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
@ -56,8 +60,8 @@ class ZipkinConfigurationsSenderConfigurationTests {
} }
@Test @Test
void shouldUseWebClientSenderIfWebApplicationIsReactive() { void shouldPreferWebClientSenderIfWebApplicationIsReactiveAndUrlSenderIsNotAvailable() {
this.reactiveContextRunner.withUserConfiguration(WebClientConfiguration.class) this.reactiveContextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> { .withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class); assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class); assertThat(context).hasSingleBean(Sender.class);
@ -65,6 +69,56 @@ class ZipkinConfigurationsSenderConfigurationTests {
}); });
} }
@Test
void shouldPreferWebClientSenderIfWebApplicationIsServletAndUrlSenderIsNotAvailable() {
this.servletContextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinWebClientSender.class);
});
}
@Test
void shouldPreferWebClientInNonWebApplicationAndUrlConnectionSenderIsNotAvailable() {
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinWebClientSender.class);
});
}
@Test
void willUseRestTemplateInNonWebApplicationIfUrlConnectionSenderIsNotAvailable() {
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
});
}
@Test
void willUseRestTemplateInServletWebApplicationIfUrlConnectionSenderIsNotAvailable() {
this.servletContextRunner.withUserConfiguration(RestTemplateConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
});
}
@Test
void willUseRestTemplateInReactiveWebApplicationIfUrlConnectionSenderIsNotAvailable() {
this.reactiveContextRunner.withUserConfiguration(RestTemplateConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
});
}
@Test @Test
void shouldNotUseWebClientSenderIfNoBuilderIsAvailable() { void shouldNotUseWebClientSenderIfNoBuilderIsAvailable() {
this.reactiveContextRunner.run((context) -> { this.reactiveContextRunner.run((context) -> {
@ -74,16 +128,6 @@ class ZipkinConfigurationsSenderConfigurationTests {
}); });
} }
@Test
void shouldUseRestTemplateSenderIfUrlConnectionSenderIsNotAvailableAndWebAppIsNotReactive() {
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class)
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
assertThat(context).hasSingleBean(Sender.class);
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
});
}
@Test @Test
void shouldBackOffOnCustomBeans() { void shouldBackOffOnCustomBeans() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {