Skip to content

Commit 13c638b

Browse files
committed
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
2 parents b325edb + 8c74b62 commit 13c638b

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurations.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3030
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
31-
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
3231
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3332
import org.springframework.boot.web.client.RestTemplateBuilder;
3433
import org.springframework.context.annotation.Bean;
@@ -46,8 +45,8 @@
4645
class ZipkinConfigurations {
4746

4847
@Configuration(proxyBeanMethods = false)
49-
@Import({ UrlConnectionSenderConfiguration.class, RestTemplateSenderConfiguration.class,
50-
WebClientSenderConfiguration.class })
48+
@Import({ UrlConnectionSenderConfiguration.class, WebClientSenderConfiguration.class,
49+
RestTemplateSenderConfiguration.class })
5150
static class SenderConfiguration {
5251

5352
}
@@ -85,7 +84,7 @@ ZipkinRestTemplateSender restTemplateSender(ZipkinProperties properties,
8584
}
8685

8786
@Configuration(proxyBeanMethods = false)
88-
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
87+
@ConditionalOnClass(WebClient.class)
8988
@EnableConfigurationProperties(ZipkinProperties.class)
9089
static class WebClientSenderConfiguration {
9190

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.test.context.FilteredClassLoader;
2626
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2727
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
28+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2829
import org.springframework.boot.web.client.RestTemplateBuilder;
2930
import org.springframework.context.annotation.Bean;
3031
import org.springframework.context.annotation.Configuration;
@@ -46,6 +47,9 @@ class ZipkinConfigurationsSenderConfigurationTests {
4647
private final ReactiveWebApplicationContextRunner reactiveContextRunner = new ReactiveWebApplicationContextRunner()
4748
.withConfiguration(AutoConfigurations.of(SenderConfiguration.class));
4849

50+
private final WebApplicationContextRunner servletContextRunner = new WebApplicationContextRunner()
51+
.withConfiguration(AutoConfigurations.of(SenderConfiguration.class));
52+
4953
@Test
5054
void shouldSupplyBeans() {
5155
this.contextRunner.run((context) -> {
@@ -56,8 +60,8 @@ void shouldSupplyBeans() {
5660
}
5761

5862
@Test
59-
void shouldUseWebClientSenderIfWebApplicationIsReactive() {
60-
this.reactiveContextRunner.withUserConfiguration(WebClientConfiguration.class)
63+
void shouldPreferWebClientSenderIfWebApplicationIsReactiveAndUrlSenderIsNotAvailable() {
64+
this.reactiveContextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
6165
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
6266
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
6367
assertThat(context).hasSingleBean(Sender.class);
@@ -66,16 +70,27 @@ void shouldUseWebClientSenderIfWebApplicationIsReactive() {
6670
}
6771

6872
@Test
69-
void shouldNotUseWebClientSenderIfNoBuilderIsAvailable() {
70-
this.reactiveContextRunner.run((context) -> {
71-
assertThat(context).doesNotHaveBean(ZipkinWebClientSender.class);
72-
assertThat(context).hasSingleBean(Sender.class);
73-
assertThat(context).hasSingleBean(URLConnectionSender.class);
74-
});
73+
void shouldPreferWebClientSenderIfWebApplicationIsServletAndUrlSenderIsNotAvailable() {
74+
this.servletContextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
75+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
76+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
77+
assertThat(context).hasSingleBean(Sender.class);
78+
assertThat(context).hasSingleBean(ZipkinWebClientSender.class);
79+
});
7580
}
7681

7782
@Test
78-
void shouldUseRestTemplateSenderIfUrlConnectionSenderIsNotAvailableAndWebAppIsNotReactive() {
83+
void shouldPreferWebClientInNonWebApplicationAndUrlConnectionSenderIsNotAvailable() {
84+
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
85+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
86+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
87+
assertThat(context).hasSingleBean(Sender.class);
88+
assertThat(context).hasSingleBean(ZipkinWebClientSender.class);
89+
});
90+
}
91+
92+
@Test
93+
void willUseRestTemplateInNonWebApplicationIfUrlConnectionSenderIsNotAvailable() {
7994
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class)
8095
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
8196
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
@@ -84,6 +99,35 @@ void shouldUseRestTemplateSenderIfUrlConnectionSenderIsNotAvailableAndWebAppIsNo
8499
});
85100
}
86101

102+
@Test
103+
void willUseRestTemplateInServletWebApplicationIfUrlConnectionSenderIsNotAvailable() {
104+
this.servletContextRunner.withUserConfiguration(RestTemplateConfiguration.class)
105+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
106+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
107+
assertThat(context).hasSingleBean(Sender.class);
108+
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
109+
});
110+
}
111+
112+
@Test
113+
void willUseRestTemplateInReactiveWebApplicationIfUrlConnectionSenderIsNotAvailable() {
114+
this.reactiveContextRunner.withUserConfiguration(RestTemplateConfiguration.class)
115+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
116+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
117+
assertThat(context).hasSingleBean(Sender.class);
118+
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
119+
});
120+
}
121+
122+
@Test
123+
void shouldNotUseWebClientSenderIfNoBuilderIsAvailable() {
124+
this.reactiveContextRunner.run((context) -> {
125+
assertThat(context).doesNotHaveBean(ZipkinWebClientSender.class);
126+
assertThat(context).hasSingleBean(Sender.class);
127+
assertThat(context).hasSingleBean(URLConnectionSender.class);
128+
});
129+
}
130+
87131
@Test
88132
void shouldBackOffOnCustomBeans() {
89133
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {

0 commit comments

Comments
 (0)