Skip to content

Commit 059e66d

Browse files
Jose CarvajalJose Carvajal
authored andcommitted
BAEL-1174: A Quick Guide to Spring Cloud Consul: Changes after senior code review
1 parent 1f75728 commit 059e66d

8 files changed

Lines changed: 103 additions & 125 deletions

File tree

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,16 @@
11
package com.baeldung.spring.cloud.consul.discovery;
22

3-
import java.net.URI;
4-
5-
import org.springframework.beans.factory.annotation.Autowired;
63
import org.springframework.boot.autoconfigure.SpringBootApplication;
74
import org.springframework.boot.builder.SpringApplicationBuilder;
8-
import org.springframework.cloud.client.discovery.DiscoveryClient;
95
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;
176

187
@SpringBootApplication
198
@EnableDiscoveryClient
20-
@RestController
219
public class DiscoveryClientApplication {
2210

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-
5511
public static void main(String[] args) {
5612
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true)
5713
.run(args);
5814
}
15+
5916
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.spring.cloud.consul.discovery;
2+
3+
import java.net.URI;
4+
import java.util.Optional;
5+
6+
import javax.naming.ServiceUnavailableException;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.cloud.client.discovery.DiscoveryClient;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.client.RestClientException;
15+
import org.springframework.web.client.RestTemplate;
16+
17+
@RestController
18+
public class DiscoveryClientController {
19+
20+
@Autowired
21+
private DiscoveryClient discoveryClient;
22+
23+
private final RestTemplate restTemplate = new RestTemplate();
24+
25+
@GetMapping("/discoveryClient")
26+
public String discoveryPing() throws RestClientException, ServiceUnavailableException {
27+
URI service = serviceUrl().map(s -> s.resolve("/ping"))
28+
.orElseThrow(ServiceUnavailableException::new);
29+
return restTemplate.getForEntity(service, String.class)
30+
.getBody();
31+
}
32+
33+
@GetMapping("/ping")
34+
public String ping() {
35+
return "pong";
36+
}
37+
38+
@GetMapping("/my-health-check")
39+
public ResponseEntity<String> myCustomCheck() {
40+
return new ResponseEntity<>(HttpStatus.OK);
41+
}
42+
43+
public Optional<URI> serviceUrl() {
44+
return discoveryClient.getInstances("myApp")
45+
.stream()
46+
.findFirst()
47+
.map(si -> si.getUri());
48+
}
49+
50+
}

‎spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/health/ServiceDiscoveryApplication.java‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,9 @@
22

33
import org.springframework.boot.autoconfigure.SpringBootApplication;
44
import org.springframework.boot.builder.SpringApplicationBuilder;
5-
import org.springframework.http.HttpStatus;
6-
import org.springframework.http.ResponseEntity;
7-
import org.springframework.web.bind.annotation.RequestMapping;
8-
import org.springframework.web.bind.annotation.RestController;
95

106
@SpringBootApplication
11-
@RestController
127
public class ServiceDiscoveryApplication {
13-
@RequestMapping("/my-health-check")
14-
public ResponseEntity<String> myCustomCheck() {
15-
String message = "Testing my healh check function";
16-
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
17-
}
18-
19-
@RequestMapping("/ping")
20-
public String ping() {
21-
return "pong";
22-
}
238

249
public static void main(String[] args) {
2510
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.cloud.consul.health;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class ServiceDiscoveryController {
10+
11+
@GetMapping("/ping")
12+
public String ping() {
13+
return "pong";
14+
}
15+
16+
@GetMapping("/my-health-check")
17+
public ResponseEntity<String> myCustomCheck() {
18+
String message = "Testing my healh check function";
19+
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
11
package com.baeldung.spring.cloud.consul.properties;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
4-
import org.springframework.beans.factory.annotation.Value;
53
import org.springframework.boot.autoconfigure.SpringBootApplication;
64
import org.springframework.boot.builder.SpringApplicationBuilder;
7-
import org.springframework.web.bind.annotation.RequestMapping;
85
import org.springframework.web.bind.annotation.RestController;
96

107
@SpringBootApplication
118
@RestController
129
public class DistributedPropertiesApplication {
1310

14-
@Value("${my.prop}")
15-
String value;
16-
17-
@Autowired
18-
private MyProperties properties;
19-
20-
@RequestMapping("/getConfigFromValue")
21-
public String getConfigFromValue() {
22-
return value;
23-
}
24-
25-
@RequestMapping("/getConfigFromProperty")
26-
public String getConfigFromProperty() {
27-
return properties.getProp();
28-
}
29-
3011
public static void main(String[] args) {
3112
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true)
3213
.run(args);
3314
}
15+
3416
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.spring.cloud.consul.properties;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class DistributedPropertiesController {
10+
11+
@Value("${my.prop}")
12+
String value;
13+
14+
@Autowired
15+
private MyProperties properties;
16+
17+
@GetMapping("/getConfigFromValue")
18+
public String getConfigFromValue() {
19+
return value;
20+
}
21+
22+
@GetMapping("/getConfigFromProperty")
23+
public String getConfigFromProperty() {
24+
return properties.getProp();
25+
}
26+
27+
}

‎spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/properties/MyProperties.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@Configuration
99
@ConfigurationProperties("my")
1010
public class MyProperties {
11+
1112
private String prop;
1213

1314
public String getProp() {
@@ -17,4 +18,5 @@ public String getProp() {
1718
public void setProp(String prop) {
1819
this.prop = prop;
1920
}
21+
2022
}

‎spring-cloud/spring-cloud-consul/src/main/java/com/baeldung/spring/cloud/consul/ribbon/RibbonClientApplication.java‎

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)