Skip to content

Commit 0390d6f

Browse files
Added support for WebClient
1 parent af7265e commit 0390d6f

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ configure(subprojects) {
4545
repositories {
4646
mavenLocal()
4747
jcenter()
48+
mavenCentral()
4849
maven {
4950
url "http://repo.spring.io/snapshot"
5051
}

service1/src/main/java/io/spring/cloud/sleuth/docs/service1/Application.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
import org.springframework.context.annotation.Bean;
99
import org.springframework.context.annotation.Primary;
1010
import org.springframework.web.client.RestTemplate;
11+
import org.springframework.web.reactive.function.client.WebClient;
1112
import zipkin2.reporter.Sender;
1213

1314
@SpringBootApplication
1415
public class Application {
1516

16-
@Bean
17-
RestTemplate restTemplate() {
18-
return new RestTemplate();
19-
}
17+
@Bean WebClient webClient() { return WebClient.create(); }
2018

2119
public static void main(String... args) {
2220
new SpringApplication(Application.class).run(args);

service1/src/main/java/io/spring/cloud/sleuth/docs/service1/Service2Client.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.springframework.stereotype.Component;
1414
import org.springframework.util.StringUtils;
1515
import org.springframework.web.client.RestTemplate;
16+
import org.springframework.web.reactive.function.client.ClientResponse;
17+
import org.springframework.web.reactive.function.client.WebClient;
1618

1719
/**
1820
* @author Marcin Grzejszczak
@@ -22,14 +24,14 @@ class Service2Client {
2224

2325
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
2426

25-
private final RestTemplate restTemplate;
27+
private final WebClient webClient;
2628
private final String serviceAddress;
2729
private final Tracer tracer;
2830

29-
Service2Client(RestTemplate restTemplate,
31+
Service2Client(WebClient webClient,
3032
@Value("${service2.address:localhost:8082}") String serviceAddress,
3133
Tracer tracer) {
32-
this.restTemplate = restTemplate;
34+
this.webClient = webClient;
3335
this.serviceAddress = serviceAddress;
3436
this.tracer = tracer;
3537
}
@@ -49,7 +51,11 @@ public String start() throws InterruptedException {
4951
span.annotate("baggage_set");
5052
span.tag(baggageKey, baggageValue);
5153
log.info("Hello from service1. Calling service2");
52-
String response = restTemplate.getForObject("http://" + serviceAddress + "/foo", String.class);
54+
String response = webClient.get()
55+
.uri("http://" + serviceAddress + "/foo")
56+
.exchange()
57+
.block()
58+
.bodyToMono(String.class).block();
5359
Thread.sleep(100);
5460
log.info("Got response from service2 [{}]", response);
5561
log.info("Service1: Baggage for [key] is [" + ExtraFieldPropagation.get("key") + "]");
@@ -61,7 +67,14 @@ String timeout(@SpanTag("someTag") String tag) {
6167
try {
6268
Thread.sleep(300);
6369
log.info("Hello from service1. Calling service2 - should end up with read timeout");
64-
String response = restTemplate.getForObject("http://" + serviceAddress + "/readtimeout", String.class);
70+
String response = webClient.get()
71+
.uri("http://" + serviceAddress + "/readtimeout")
72+
.retrieve()
73+
.onStatus(httpStatus -> httpStatus.isError(), clientResponse -> {
74+
throw new IllegalStateException("Exception!");
75+
})
76+
.bodyToMono(String.class)
77+
.block();
6578
log.info("Got response from service2 [{}]", response);
6679
return response;
6780
} catch (Exception e) {

0 commit comments

Comments
 (0)