Merge pull request #3164 from Sgitario/master

BAEL-1174 A Quick Guide to Spring Cloud Consul
This commit is contained in:
Loredana Crusoveanu 2017-12-01 12:20:30 +02:00 committed by GitHub
commit 4672451564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 257 additions and 3 deletions

View File

@ -1,3 +1,3 @@
UK
US
Germany
UK
US
Germany

BIN
libraries/helloWorld.docx Normal file

Binary file not shown.

View File

@ -19,6 +19,7 @@
<module>spring-cloud-stream</module>
<module>spring-cloud-connectors-heroku</module>
<module>spring-cloud-aws</module>
<module>spring-cloud-consul</module>
</modules>
<packaging>pom</packaging>

View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-cloud-consul</artifactId>
<packaging>jar</packaging>
<name>spring-cloud-consul</name>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -0,0 +1,9 @@
spring:
application:
name: myApp
cloud:
consul:
host: localhost
port: 8500
config:
enabled: true