BAEL-1174: A Quick Guide to Spring Cloud Consul
This commit is contained in:
+53
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.spring.cloud.consul;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@RestController
|
||||
public class DiscoveryClientApplication {
|
||||
|
||||
@Bean
|
||||
public RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
@RequestMapping("/discoveryClient")
|
||||
public String home() {
|
||||
return this.restTemplate.getForEntity(serviceUrl().resolve("/ping"), String.class)
|
||||
.getBody();
|
||||
}
|
||||
|
||||
public URI serviceUrl() {
|
||||
List<ServiceInstance> list = discoveryClient.getInstances("myApp");
|
||||
if (list != null && list.size() > 0) {
|
||||
return list.get(0)
|
||||
.getUri();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(DiscoveryClientApplication.class).web(true)
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.spring.cloud.consul;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.spring.cloud.consul.properties.MyProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
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) {
|
||||
new SpringApplicationBuilder(DistributedPropertiesApplication.class).web(true)
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.spring.cloud.consul;
|
||||
|
||||
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.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class RibbonClientApplication {
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
public RestTemplate loadbalancedRestTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@RequestMapping("/ribbonClient")
|
||||
public String home() {
|
||||
return this.restTemplate.getForObject("http://myApp/ping", String.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(RibbonClientApplication.class).web(true)
|
||||
.run(args);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.spring.cloud.consul;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
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
|
||||
@RestController
|
||||
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) {
|
||||
new SpringApplicationBuilder(ServiceDiscoveryApplication.class).web(true)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.spring.cloud.consul.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@RefreshScope
|
||||
@Configuration
|
||||
@ConfigurationProperties("my")
|
||||
public class MyProperties {
|
||||
private String prop;
|
||||
|
||||
public String getProp() {
|
||||
return prop;
|
||||
}
|
||||
|
||||
public void setProp(String prop) {
|
||||
this.prop = prop;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
spring:
|
||||
application:
|
||||
name: myApp
|
||||
cloud:
|
||||
consul:
|
||||
host: localhost
|
||||
port: 8500
|
||||
discovery:
|
||||
healthCheckPath: /my-health-check
|
||||
healthCheckInterval: 20s
|
||||
enabled: true
|
||||
instanceId: ${spring.application.name}:${random.value}
|
||||
server:
|
||||
port: 0
|
||||
@@ -0,0 +1,9 @@
|
||||
spring:
|
||||
application:
|
||||
name: myApp
|
||||
cloud:
|
||||
consul:
|
||||
host: localhost
|
||||
port: 8500
|
||||
config:
|
||||
enabled: true
|
||||
Reference in New Issue
Block a user