Skip to content

Commit c8a4732

Browse files
Dave SyerShivang Shah
Dave Syer
authored and
Shivang Shah
committed
Remove @bean RestTemplate
(If users need one they can add it themselves) Fixes spring-cloudgh-239
1 parent 7b11f4c commit c8a4732

File tree

10 files changed

+122
-67
lines changed

10 files changed

+122
-67
lines changed

spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceWebClientAutoConfiguration.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
import org.springframework.web.client.RestTemplate;
3939

4040
/**
41-
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
42-
* enables span information propagation when using {@link RestTemplate}
41+
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration
42+
* Auto-configuration} enables span information propagation when using
43+
* {@link RestTemplate}
4344
*
4445
* @author Marcin Grzejszczak
4546
* @since 1.0.0
@@ -58,12 +59,6 @@ public TraceRestTemplateInterceptor traceRestTemplateInterceptor(Tracer tracer,
5859
return new TraceRestTemplateInterceptor(tracer, spanInjector);
5960
}
6061

61-
@Bean
62-
@ConditionalOnMissingBean
63-
public RestTemplate restTemplate() {
64-
return new RestTemplate();
65-
}
66-
6762
@Bean
6863
public SpanInjector<HttpRequest> httpRequestSpanInjector() {
6964
return new HttpRequestInjector();
@@ -82,7 +77,8 @@ protected static class TraceInterceptorConfiguration {
8277
public void init() {
8378
if (this.restTemplates != null) {
8479
for (RestTemplate restTemplate : this.restTemplates) {
85-
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(restTemplate.getInterceptors());
80+
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(
81+
restTemplate.getInterceptors());
8682
interceptors.add(this.traceRestTemplateInterceptor);
8783
restTemplate.setInterceptors(interceptors);
8884
}

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/RestTemplateTraceAspectIntegrationTests.java

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package org.springframework.cloud.sleuth.instrument.web;
22

3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.get;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
7+
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
8+
import static java.util.concurrent.TimeUnit.SECONDS;
9+
import static junitparams.JUnitParamsRunner.$;
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
13+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14+
315
import java.util.concurrent.Callable;
416
import java.util.concurrent.ExecutionException;
517

@@ -14,6 +26,7 @@
1426
import org.springframework.cloud.sleuth.instrument.DefaultTestAutoConfiguration;
1527
import org.springframework.cloud.sleuth.instrument.web.common.AbstractMvcWiremockIntegrationTest;
1628
import org.springframework.cloud.sleuth.instrument.web.common.HttpMockServer;
29+
import org.springframework.context.annotation.Bean;
1730
import org.springframework.context.annotation.Import;
1831
import org.springframework.http.MediaType;
1932
import org.springframework.scheduling.annotation.EnableAsync;
@@ -31,46 +44,42 @@
3144
import junitparams.JUnitParamsRunner;
3245
import junitparams.Parameters;
3346

34-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
35-
import static com.github.tomakehurst.wiremock.client.WireMock.get;
36-
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
37-
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
38-
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
39-
import static java.util.concurrent.TimeUnit.SECONDS;
40-
import static junitparams.JUnitParamsRunner.$;
41-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
42-
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
43-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
44-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
45-
46-
@SpringApplicationConfiguration(classes = {RestTemplateTraceAspectIntegrationTests.CorrelationIdAspectTestConfiguration.class})
47+
@SpringApplicationConfiguration(classes = {
48+
RestTemplateTraceAspectIntegrationTests.CorrelationIdAspectTestConfiguration.class })
4749
@RunWith(JUnitParamsRunner.class)
48-
public class RestTemplateTraceAspectIntegrationTests extends AbstractMvcWiremockIntegrationTest {
50+
public class RestTemplateTraceAspectIntegrationTests
51+
extends AbstractMvcWiremockIntegrationTest {
4952

50-
@ClassRule public static final SpringClassRule SCR = new SpringClassRule();
51-
@Rule public final SpringMethodRule springMethodRule = new SpringMethodRule();
53+
@ClassRule
54+
public static final SpringClassRule SCR = new SpringClassRule();
55+
@Rule
56+
public final SpringMethodRule springMethodRule = new SpringMethodRule();
5257

53-
@Before public void setupDefaultWireMockStubbing() {
58+
@Before
59+
public void setupDefaultWireMockStubbing() {
5460
stubInteraction(get(urlMatching(".*")), aResponse().withStatus(200));
5561
}
5662

5763
@Test
58-
public void should_set_span_data_on_headers_via_aspect_in_synchronous_call() throws Exception {
64+
public void should_set_span_data_on_headers_via_aspect_in_synchronous_call()
65+
throws Exception {
5966
whenARequestIsSentToASyncEndpoint();
6067

6168
thenTraceIdHasBeenSetOnARequestHeader();
6269
}
6370

6471
@Test
65-
public void should_set_span_data_on_headers_when_sending_a_request_via_async_rest_template() throws Exception {
72+
public void should_set_span_data_on_headers_when_sending_a_request_via_async_rest_template()
73+
throws Exception {
6674
whenARequestIsSentToAAsyncRestTemplateEndpoint();
6775

6876
thenTraceIdHasBeenSetOnARequestHeader();
6977
}
7078

7179
@Test
7280
@Parameters
73-
public void should_set_span_data_on_headers_via_aspect_in_asynchronous_call(String url) throws Exception {
81+
public void should_set_span_data_on_headers_via_aspect_in_asynchronous_call(
82+
String url) throws Exception {
7483
whenARequestIsSentToAnAsyncEndpoint(url);
7584

7685
thenTraceIdHasBeenSetOnARequestHeader();
@@ -81,39 +90,49 @@ public Object[] parametersForShould_set_span_data_on_headers_via_aspect_in_async
8190
}
8291

8392
private void whenARequestIsSentToAAsyncRestTemplateEndpoint() throws Exception {
84-
this.mockMvc.perform(MockMvcRequestBuilders.get("/asyncRestTemplate").accept(MediaType.TEXT_PLAIN)).andReturn();
93+
this.mockMvc.perform(MockMvcRequestBuilders.get("/asyncRestTemplate")
94+
.accept(MediaType.TEXT_PLAIN)).andReturn();
8595
}
8696

8797
private void whenARequestIsSentToASyncEndpoint() throws Exception {
88-
this.mockMvc.perform(MockMvcRequestBuilders.get("/syncPing").accept(MediaType.TEXT_PLAIN)).andReturn();
98+
this.mockMvc.perform(
99+
MockMvcRequestBuilders.get("/syncPing").accept(MediaType.TEXT_PLAIN))
100+
.andReturn();
89101
}
90102

91103
private void thenTraceIdHasBeenSetOnARequestHeader() {
92-
this.wireMock.verifyThat(getRequestedFor(urlMatching(".*")).withHeader(Span.TRACE_ID_NAME, matching("^(?!\\s*$).+")));
104+
this.wireMock.verifyThat(getRequestedFor(urlMatching(".*"))
105+
.withHeader(Span.TRACE_ID_NAME, matching("^(?!\\s*$).+")));
93106
}
94107

95108
private void whenARequestIsSentToAnAsyncEndpoint(String url) throws Exception {
96-
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get(url).accept(MediaType.TEXT_PLAIN))
97-
.andExpect(request().asyncStarted())
98-
.andReturn();
109+
MvcResult mvcResult = this.mockMvc
110+
.perform(MockMvcRequestBuilders.get(url).accept(MediaType.TEXT_PLAIN))
111+
.andExpect(request().asyncStarted()).andReturn();
99112
mvcResult.getAsyncResult(SECONDS.toMillis(2));
100-
this.mockMvc.perform(asyncDispatch(mvcResult)).
101-
andDo(print()).
102-
andExpect(status().isOk());
113+
this.mockMvc.perform(asyncDispatch(mvcResult)).andDo(print())
114+
.andExpect(status().isOk());
103115
}
104116

105117
@EnableAsync
106118
@DefaultTestAutoConfiguration
107119
@Import(AspectTestingController.class)
108120
public static class CorrelationIdAspectTestConfiguration {
121+
@Bean
122+
public RestTemplate restTemplate() {
123+
return new RestTemplate();
124+
}
109125
}
110126

111127
@RestController
112128
public static class AspectTestingController {
113129

114-
@Autowired HttpMockServer httpMockServer;
115-
@Autowired RestTemplate restTemplate;
116-
@Autowired AsyncRestTemplate asyncRestTemplate;
130+
@Autowired
131+
HttpMockServer httpMockServer;
132+
@Autowired
133+
RestTemplate restTemplate;
134+
@Autowired
135+
AsyncRestTemplate asyncRestTemplate;
117136

118137
@RequestMapping(value = "/asyncRestTemplate", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
119138
public String asyncRestTemplate()
@@ -147,13 +166,15 @@ public String call() throws Exception {
147166
};
148167

149168
private String callWiremockAndReturnOk() {
150-
this.restTemplate.getForObject("http://localhost:" + this.httpMockServer.port(), String.class);
169+
this.restTemplate.getForObject(
170+
"http://localhost:" + this.httpMockServer.port(), String.class);
151171
return "OK";
152172
}
153173

154174
private String callWiremockViaAsyncRestTemplateAndReturnOk()
155175
throws ExecutionException, InterruptedException {
156-
this.asyncRestTemplate.getForEntity("http://localhost:" + this.httpMockServer.port(), String.class).get();
176+
this.asyncRestTemplate.getForEntity(
177+
"http://localhost:" + this.httpMockServer.port(), String.class).get();
157178
return "OK";
158179
}
159180
}

spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/web/TraceFilterCustomExtractorTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public void onApplicationEvent(EmbeddedServletContainerInitializedEvent event) {
115115
this.port = event.getEmbeddedServletContainer().getPort();
116116
}
117117

118+
@Bean
119+
public RestTemplate restTemplate() {
120+
return new RestTemplate();
121+
}
122+
118123
@Bean
119124
CustomRestController customRestController() {
120125
return new CustomRestController();

spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-messaging/src/main/java/sample/SampleMessagingApplication.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
import org.springframework.beans.factory.annotation.Autowired;
2020
import org.springframework.boot.SpringApplication;
2121
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.context.annotation.Bean;
2223
import org.springframework.context.annotation.EnableAspectJAutoProxy;
2324
import org.springframework.integration.annotation.IntegrationComponentScan;
2425
import org.springframework.scheduling.annotation.EnableAsync;
2526
import org.springframework.web.bind.annotation.RequestMapping;
2627
import org.springframework.web.bind.annotation.RestController;
28+
import org.springframework.web.client.RestTemplate;
2729

2830
/**
2931
* @author Spencer Gibb
@@ -59,6 +61,11 @@ public String xform() {
5961
return this.transformer.send(msg);
6062
}
6163

64+
@Bean
65+
public RestTemplate restTemplate() {
66+
return new RestTemplate();
67+
}
68+
6269
public static void main(String[] args) {
6370
SpringApplication.run(SampleMessagingApplication.class, args);
6471
}

spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-ribbon/src/main/java/sample/SampleRibbonApplication.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.scheduling.annotation.EnableAsync;
28+
import org.springframework.web.client.RestTemplate;
2829

2930
import zipkin.Span;
3031

@@ -42,9 +43,14 @@ public static void main(String[] args) {
4243
SpringApplication.run(SampleRibbonApplication.class, args);
4344
}
4445

46+
@Bean
47+
public RestTemplate restTemplate() {
48+
return new RestTemplate();
49+
}
50+
4551
// Use this for debugging (or if there is no Zipkin server running on port 9411)
4652
@Bean
47-
@ConditionalOnProperty(value="sample.zipkin.enabled", havingValue="false")
53+
@ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
4854
public ZipkinSpanReporter spanCollector() {
4955
return new ZipkinSpanReporter() {
5056
@Override

spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-stream/src/main/java/sample/SampleController.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
* @author Spencer Gibb
3737
*/
3838
@RestController
39-
public class SampleController implements
40-
ApplicationListener<EmbeddedServletContainerInitializedEvent> {
39+
public class SampleController
40+
implements ApplicationListener<EmbeddedServletContainerInitializedEvent> {
4141
private static final Log log = LogFactory.getLog(SampleController.class);
4242

4343
@Autowired
@@ -48,16 +48,16 @@ public class SampleController implements
4848
private SpanAccessor accessor;
4949
@Autowired
5050
private SampleBackground controller;
51-
@Autowired
52-
private Random random;
51+
52+
private Random random = new Random();
5353
private int port;
5454

5555
@RequestMapping("/")
5656
public String hi() throws InterruptedException {
5757
Thread.sleep(this.random.nextInt(1000));
5858

59-
String s = this.restTemplate.getForObject("http://localhost:" + this.port
60-
+ "/hi2", String.class);
59+
String s = this.restTemplate
60+
.getForObject("http://localhost:" + this.port + "/hi2", String.class);
6161
return "hi/" + s;
6262
}
6363

@@ -68,7 +68,8 @@ public Callable<String> call() {
6868
public String call() throws Exception {
6969
int millis = SampleController.this.random.nextInt(1000);
7070
Thread.sleep(millis);
71-
SampleController.this.tracer.addTag("callable-sleep-millis", String.valueOf(millis));
71+
SampleController.this.tracer.addTag("callable-sleep-millis",
72+
String.valueOf(millis));
7273
Span currentSpan = SampleController.this.accessor.getCurrentSpan();
7374
return "async hi: " + currentSpan;
7475
}
@@ -98,8 +99,8 @@ public String traced() throws InterruptedException {
9899
Thread.sleep(millis);
99100
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
100101

101-
String s = this.restTemplate.getForObject("http://localhost:" + this.port
102-
+ "/call", String.class);
102+
String s = this.restTemplate
103+
.getForObject("http://localhost:" + this.port + "/call", String.class);
103104
this.tracer.close(span);
104105
return "traced/" + s;
105106
}
@@ -111,8 +112,8 @@ public String start() throws InterruptedException {
111112
Thread.sleep(millis);
112113
this.tracer.addTag("random-sleep-millis", String.valueOf(millis));
113114

114-
String s = this.restTemplate.getForObject("http://localhost:" + this.port
115-
+ "/call", String.class);
115+
String s = this.restTemplate
116+
.getForObject("http://localhost:" + this.port + "/call", String.class);
116117
return "start/" + s;
117118
}
118119

spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-stream/src/main/java/sample/SampleSleuthApplication.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.annotation.EnableAspectJAutoProxy;
2323
import org.springframework.scheduling.annotation.EnableAsync;
24+
import org.springframework.web.client.RestTemplate;
2425

2526
/**
2627
* @author Spencer Gibb
@@ -32,6 +33,11 @@ public class SampleSleuthApplication {
3233

3334
public static final String CLIENT_NAME = "testApp";
3435

36+
@Bean
37+
public RestTemplate restTemplate() {
38+
return new RestTemplate();
39+
}
40+
3541
@Bean
3642
public SampleController sampleController() {
3743
return new SampleController();

spring-cloud-sleuth-samples/spring-cloud-sleuth-sample-zipkin/src/main/java/sample/SampleZipkinApplication.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.EnableAspectJAutoProxy;
2727
import org.springframework.scheduling.annotation.EnableAsync;
28+
import org.springframework.web.client.RestTemplate;
2829

2930
/**
3031
* @author Spencer Gibb
@@ -40,9 +41,14 @@ public static void main(String[] args) {
4041
SpringApplication.run(SampleZipkinApplication.class, args);
4142
}
4243

44+
@Bean
45+
public RestTemplate restTemplate() {
46+
return new RestTemplate();
47+
}
48+
4349
// Use this for debugging (or if there is no Zipkin server running on port 9411)
4450
@Bean
45-
@ConditionalOnProperty(value="sample.zipkin.enabled", havingValue="false")
51+
@ConditionalOnProperty(value = "sample.zipkin.enabled", havingValue = "false")
4652
public ZipkinSpanReporter spanCollector() {
4753
return new ZipkinSpanReporter() {
4854
@Override

0 commit comments

Comments
 (0)