13
13
import org .springframework .stereotype .Component ;
14
14
import org .springframework .util .StringUtils ;
15
15
import org .springframework .web .client .RestTemplate ;
16
+ import org .springframework .web .reactive .function .client .ClientResponse ;
17
+ import org .springframework .web .reactive .function .client .WebClient ;
16
18
17
19
/**
18
20
* @author Marcin Grzejszczak
@@ -22,14 +24,14 @@ class Service2Client {
22
24
23
25
private static final Logger log = LoggerFactory .getLogger (MethodHandles .lookup ().lookupClass ());
24
26
25
- private final RestTemplate restTemplate ;
27
+ private final WebClient webClient ;
26
28
private final String serviceAddress ;
27
29
private final Tracer tracer ;
28
30
29
- Service2Client (RestTemplate restTemplate ,
31
+ Service2Client (WebClient webClient ,
30
32
@ Value ("${service2.address:localhost:8082}" ) String serviceAddress ,
31
33
Tracer tracer ) {
32
- this .restTemplate = restTemplate ;
34
+ this .webClient = webClient ;
33
35
this .serviceAddress = serviceAddress ;
34
36
this .tracer = tracer ;
35
37
}
@@ -49,7 +51,11 @@ public String start() throws InterruptedException {
49
51
span .annotate ("baggage_set" );
50
52
span .tag (baggageKey , baggageValue );
51
53
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 ();
53
59
Thread .sleep (100 );
54
60
log .info ("Got response from service2 [{}]" , response );
55
61
log .info ("Service1: Baggage for [key] is [" + ExtraFieldPropagation .get ("key" ) + "]" );
@@ -61,7 +67,14 @@ String timeout(@SpanTag("someTag") String tag) {
61
67
try {
62
68
Thread .sleep (300 );
63
69
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 ();
65
78
log .info ("Got response from service2 [{}]" , response );
66
79
return response ;
67
80
} catch (Exception e ) {
0 commit comments