Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 967ad5a

Browse files
authored
spring servlet example: make port configurable. (#1940)
1 parent 7eb2499 commit 967ad5a

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

examples/spring/servlet/src/main/java/io/opencensus/examples/spring/servlet/HelloController.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.logging.Level;
2121
import java.util.logging.Logger;
2222
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.core.env.Environment;
2324
import org.springframework.http.ResponseEntity;
2425
import org.springframework.http.client.ClientHttpRequestFactory;
2526
import 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

Comments
 (0)