2020import java .util .logging .Level ;
2121import java .util .logging .Logger ;
2222import org .springframework .beans .factory .annotation .Autowired ;
23+ import org .springframework .core .env .Environment ;
2324import org .springframework .http .ResponseEntity ;
2425import org .springframework .http .client .ClientHttpRequestFactory ;
2526import org .springframework .http .client .HttpComponentsClientHttpRequestFactory ;
@@ -42,9 +43,9 @@ public class HelloController {
4243 @ RequestMapping ("/" )
4344 public String index () {
4445 String str = "Hello from servlet instrumented with opencensus-spring" ;
45- String resp = restTemplate .getForObject ("http://localhost:8080/ loopback" , String .class );
46+ String resp = restTemplate .getForObject (baseUrl () + " loopback" , String .class );
4647
47- String asyncUrl = "http://localhost:8080/ asyncloopback" ;
48+ String asyncUrl = baseUrl () + " asyncloopback" ;
4849 ListenableFuture <ResponseEntity <String >> future1 =
4950 asyncRestTemplate .getForEntity (asyncUrl , String .class );
5051 ListenableFuture <ResponseEntity <String >> future2 =
@@ -77,6 +78,13 @@ public String asyncLoopback() {
7778 return "Async Loopback. Hello from servlet!" ;
7879 }
7980
81+ private String baseUrl () {
82+ Integer port = environment .getProperty ("local.server.port" , Integer .class );
83+ return String .format ("http://localhost:%d/" , (port != null ? port : 0 ));
84+ }
85+
86+ @ Autowired Environment environment ;
87+
8088 @ Autowired AsyncRestTemplate asyncRestTemplate ;
8189
8290 RestTemplate restTemplate = new RestTemplate (getClientHttpRequestFactory ());
0 commit comments