BAEL-1174: A Quick Guide to Spring Cloud Consul

This commit is contained in:
Jose Carvajal 2017-11-30 11:55:49 +01:00
parent f47a60c5de
commit b14dfbe4f0
14 changed files with 319 additions and 1 deletions

View File

@ -28,8 +28,14 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>

BIN
libraries-data/file.dat Normal file

Binary file not shown.

BIN
libraries/helloWorld.docx Normal file

Binary file not shown.

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

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.undertow</groupId>
<artifactId>undertow</artifactId>
<name>undertow</name>
<version>1.0-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.baeldung.undertow.SimpleServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<XMLTutorials>
<tutorial tutId="01" type="xml">
<title>XML with Dom4J</title>
<description>XML handling with Dom4J</description>
<date>14/06/2016</date>
<author>Dom4J tech writer</author>
</tutorial>
</XMLTutorials>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tutorials>
<tutorial tutId="01" type="XML">
<author>Jaxb author</author>
<date>04/02/2015</date>
<description>XML Binding with Jaxb</description>
<title>XML with Jaxb</title>
</tutorial>
</tutorials>