|
1 | 1 | package com.baeldung.spring.cloud.consul.discovery; |
2 | 2 |
|
3 | | -import java.net.URI; |
4 | | - |
5 | | -import org.springframework.beans.factory.annotation.Autowired; |
6 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
7 | 4 | import org.springframework.boot.builder.SpringApplicationBuilder; |
8 | | -import org.springframework.cloud.client.discovery.DiscoveryClient; |
9 | 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
10 | | -import org.springframework.context.annotation.Bean; |
11 | | -import org.springframework.http.HttpStatus; |
12 | | -import org.springframework.http.ResponseEntity; |
13 | | -import org.springframework.web.bind.annotation.GetMapping; |
14 | | -import org.springframework.web.bind.annotation.RequestMapping; |
15 | | -import org.springframework.web.bind.annotation.RestController; |
16 | | -import org.springframework.web.client.RestTemplate; |
17 | 6 |
|
18 | 7 | @SpringBootApplication |
19 | 8 | @EnableDiscoveryClient |
20 | | -@RestController |
21 | 9 | public class DiscoveryClientApplication { |
22 | 10 |
|
23 | | - @Bean |
24 | | - public RestTemplate restTemplate() { |
25 | | - return new RestTemplate(); |
26 | | - } |
27 | | - |
28 | | - @Autowired |
29 | | - private DiscoveryClient discoveryClient; |
30 | | - |
31 | | - @GetMapping("/discoveryClient") |
32 | | - public String home() { |
33 | | - return restTemplate().getForEntity(serviceUrl().resolve("/ping"), String.class) |
34 | | - .getBody(); |
35 | | - } |
36 | | - |
37 | | - @GetMapping("/ping") |
38 | | - public String ping() { |
39 | | - return "pong"; |
40 | | - } |
41 | | - |
42 | | - @RequestMapping("/my-health-check") |
43 | | - public ResponseEntity<String> myCustomCheck() { |
44 | | - return new ResponseEntity<>(HttpStatus.OK); |
45 | | - } |
46 | | - |
47 | | - public URI serviceUrl() { |
48 | | - return discoveryClient.getInstances("myApp") |
49 | | - .stream() |
50 | | - .findFirst() |
51 | | - .map(si -> si.getUri()) |
52 | | - .orElse(null); |
53 | | - } |
54 | | - |
55 | 11 | public static void main(String[] args) { |
56 | 12 | new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true) |
57 | 13 | .run(args); |
58 | 14 | } |
| 15 | + |
59 | 16 | } |
0 commit comments