Merge pull request #3195 from Sgitario/master
BAEL-1174: A Quick Guide to Spring Cloud Consul: Changes after senior code review
This commit is contained in:
commit
93fc61750e
|
@ -1,59 +1,16 @@
|
||||||
package com.baeldung.spring.cloud.consul.discovery;
|
package com.baeldung.spring.cloud.consul.discovery;
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
||||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableDiscoveryClient
|
@EnableDiscoveryClient
|
||||||
@RestController
|
|
||||||
public class DiscoveryClientApplication {
|
public class DiscoveryClientApplication {
|
||||||
|
|
||||||
@Bean
|
|
||||||
public RestTemplate restTemplate() {
|
|
||||||
return new RestTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private DiscoveryClient discoveryClient;
|
|
||||||
|
|
||||||
@GetMapping("/discoveryClient")
|
|
||||||
public String home() {
|
|
||||||
return restTemplate().getForEntity(serviceUrl().resolve("/ping"), String.class)
|
|
||||||
.getBody();
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/ping")
|
|
||||||
public String ping() {
|
|
||||||
return "pong";
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping("/my-health-check")
|
|
||||||
public ResponseEntity<String> myCustomCheck() {
|
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
public URI serviceUrl() {
|
|
||||||
return discoveryClient.getInstances("myApp")
|
|
||||||
.stream()
|
|
||||||
.findFirst()
|
|
||||||
.map(si -> si.getUri())
|
|
||||||
.orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true)
|
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true)
|
||||||
.run(args);
|
.run(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
package com.baeldung.spring.cloud.consul.discovery;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import javax.naming.ServiceUnavailableException;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class DiscoveryClientController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DiscoveryClient discoveryClient;
|
||||||
|
|
||||||
|
private final RestTemplate restTemplate = new RestTemplate();
|
||||||
|
|
||||||
|
@GetMapping("/discoveryClient")
|
||||||
|
public String discoveryPing() throws RestClientException, ServiceUnavailableException {
|
||||||
|
URI service = serviceUrl().map(s -> s.resolve("/ping"))
|
||||||
|
.orElseThrow(ServiceUnavailableException::new);
|
||||||
|
return restTemplate.getForEntity(service, String.class)
|
||||||
|
.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/ping")
|
||||||
|
public String ping() {
|
||||||
|
return "pong";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/my-health-check")
|
||||||
|
public ResponseEntity<String> myCustomCheck() {
|
||||||
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<URI> serviceUrl() {
|
||||||
|
return discoveryClient.getInstances("myApp")
|
||||||
|
.stream()
|
||||||
|
.findFirst()
|
||||||
|
.map(si -> si.getUri());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,24 +2,9 @@ package com.baeldung.spring.cloud.consul.health;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
|
||||||
public class ServiceDiscoveryApplication {
|
public class ServiceDiscoveryApplication {
|
||||||
@RequestMapping("/my-health-check")
|
|
||||||
public ResponseEntity<String> myCustomCheck() {
|
|
||||||
String message = "Testing my healh check function";
|
|
||||||
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping("/ping")
|
|
||||||
public String ping() {
|
|
||||||
return "pong";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true)
|
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true)
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package com.baeldung.spring.cloud.consul.health;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class ServiceDiscoveryController {
|
||||||
|
|
||||||
|
@GetMapping("/ping")
|
||||||
|
public String ping() {
|
||||||
|
return "pong";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/my-health-check")
|
||||||
|
public ResponseEntity<String> myCustomCheck() {
|
||||||
|
String message = "Testing my healh check function";
|
||||||
|
return new ResponseEntity<>(message, HttpStatus.FORBIDDEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,34 +1,16 @@
|
||||||
package com.baeldung.spring.cloud.consul.properties;
|
package com.baeldung.spring.cloud.consul.properties;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@RestController
|
@RestController
|
||||||
public class DistributedPropertiesApplication {
|
public class DistributedPropertiesApplication {
|
||||||
|
|
||||||
@Value("${my.prop}")
|
|
||||||
String value;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MyProperties properties;
|
|
||||||
|
|
||||||
@RequestMapping("/getConfigFromValue")
|
|
||||||
public String getConfigFromValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping("/getConfigFromProperty")
|
|
||||||
public String getConfigFromProperty() {
|
|
||||||
return properties.getProp();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true)
|
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true)
|
||||||
.run(args);
|
.run(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.cloud.consul.properties;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class DistributedPropertiesController {
|
||||||
|
|
||||||
|
@Value("${my.prop}")
|
||||||
|
String value;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MyProperties properties;
|
||||||
|
|
||||||
|
@GetMapping("/getConfigFromValue")
|
||||||
|
public String getConfigFromValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/getConfigFromProperty")
|
||||||
|
public String getConfigFromProperty() {
|
||||||
|
return properties.getProp();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import org.springframework.context.annotation.Configuration;
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConfigurationProperties("my")
|
@ConfigurationProperties("my")
|
||||||
public class MyProperties {
|
public class MyProperties {
|
||||||
|
|
||||||
private String prop;
|
private String prop;
|
||||||
|
|
||||||
public String getProp() {
|
public String getProp() {
|
||||||
|
@ -17,4 +18,5 @@ public class MyProperties {
|
||||||
public void setProp(String prop) {
|
public void setProp(String prop) {
|
||||||
this.prop = prop;
|
this.prop = prop;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
package com.baeldung.spring.cloud.consul.ribbon;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
|
||||||
|
|
||||||
@SpringBootApplication
|
|
||||||
@RestController
|
|
||||||
public class RibbonClientApplication {
|
|
||||||
|
|
||||||
@LoadBalanced
|
|
||||||
@Bean
|
|
||||||
RestTemplate getRestTemplate() {
|
|
||||||
return new RestTemplate();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
RestTemplate restTemplate;
|
|
||||||
|
|
||||||
@RequestMapping("/ribbonClient")
|
|
||||||
public String home() {
|
|
||||||
return restTemplate.getForObject("http://myApp/ping", String.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/ping")
|
|
||||||
public String ping() {
|
|
||||||
return "pong";
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequestMapping("/my-health-check")
|
|
||||||
public ResponseEntity<String> myCustomCheck() {
|
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
new SpringApplicationBuilder(RibbonClientApplication.class).web(true)
|
|
||||||
.run(args);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue