From d8e92f9d8de8d04bc0fd2c879d32f6171eb62c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20R=C3=A4del?= Date: Thu, 18 Aug 2016 21:36:05 +0200 Subject: [PATCH] Sample Hystrix implementation --- spring-cloud-hystrix/pom.xml | 49 +++++++++++++++ .../pom.xml | 62 +++++++++++++++++++ .../rest/consumer/GreetingService.java | 18 ++++++ .../consumer/RestConsumerApplication.java | 28 +++++++++ .../resources/templates/greeting-view.html | 9 +++ .../pom.xml | 40 ++++++++++++ .../rest/producer/GreetingController.java | 10 +++ .../producer/RestProducerApplication.java | 20 ++++++ .../src/main/resources/application.properties | 1 + 9 files changed, 237 insertions(+) create mode 100644 spring-cloud-hystrix/pom.xml create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties diff --git a/spring-cloud-hystrix/pom.xml b/spring-cloud-hystrix/pom.xml new file mode 100644 index 0000000000..f7394d729e --- /dev/null +++ b/spring-cloud-hystrix/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-hystrix + 1.0.0-SNAPSHOT + + spring-cloud-hystrix-rest-producer + spring-cloud-hystrix-rest-consumer + + pom + + Spring Cloud Hystrix + Spring Cloud Hystrix Demo + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + .. + + + + UTF-8 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + org.springframework.boot + spring-boot-maven-plugin + 1.4.0.RELEASE + + + + + diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml new file mode 100644 index 0000000000..b9da24b9a5 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + spring-cloud-hystrix-rest-consumer + 1.0.0-SNAPSHOT + jar + + Spring Cloud Hystrix REST Consumer + Spring Cloud Hystrix Sample Implementation + + + com.baeldung.spring.cloud + spring-cloud-hystrix + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.cloud + spring-cloud-starter-hystrix + 1.1.5.RELEASE + + + org.springframework.boot + spring-boot-starter-web + 1.4.0.RELEASE + + + org.springframework.boot + spring-boot-starter-thymeleaf + 1.4.0.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + Brixton.SR4 + pom + import + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java new file mode 100644 index 0000000000..d3d5e6e047 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingService.java @@ -0,0 +1,18 @@ +package com.baeldung.spring.cloud.hystrix.rest.consumer; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class GreetingService { + @HystrixCommand(fallbackMethod = "defaultGreeting") + public String getGreeting(String username) { + return new RestTemplate().getForObject("http://localhost:9090/greeting/{username}", String.class, username); + } + + private String defaultGreeting(String username) { + return "Hello User!"; + } +} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java new file mode 100644 index 0000000000..051fd9a9bb --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerApplication.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.cloud.hystrix.rest.consumer; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; + +@SpringBootApplication +@EnableCircuitBreaker +@Controller +public class RestConsumerApplication { + @Autowired + private GreetingService greetingService; + + public static void main(String[] args) { + SpringApplication.run(RestConsumerApplication.class, args); + } + + @RequestMapping("/get-greeting/{username}") + public String getGreeting(Model model, @PathVariable("username") String username) { + model.addAttribute("greeting", greetingService.getGreeting(username)); + return "greeting-view"; + } +} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html new file mode 100644 index 0000000000..6cb70edf2d --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/templates/greeting-view.html @@ -0,0 +1,9 @@ + + + + Greetings from Hystrix + + +

+ + \ No newline at end of file diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml new file mode 100644 index 0000000000..44e373c8ac --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + spring-cloud-hystrix-rest-producer + 1.0.0-SNAPSHOT + jar + + Spring Cloud Hystrix REST Producer + Spring Cloud Hystrix Sample REST Producer Implementation + + + com.baeldung.spring.cloud + spring-cloud-hystrix + 1.0.0-SNAPSHOT + .. + + + + + org.springframework.boot + spring-boot-starter-web + 1.4.0.RELEASE + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java new file mode 100644 index 0000000000..81541b4f8f --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/GreetingController.java @@ -0,0 +1,10 @@ +package com.baeldung.spring.cloud.hystrix.rest.producer; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +public interface GreetingController { + @RequestMapping("/greeting/{username}") + String greeting(@PathVariable("username") String username); +} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java new file mode 100644 index 0000000000..9496d4760d --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/producer/RestProducerApplication.java @@ -0,0 +1,20 @@ +package com.baeldung.spring.cloud.hystrix.rest.producer; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@RestController +public class RestProducerApplication implements GreetingController { + public static void main(String[] args) { + SpringApplication.run(RestProducerApplication.class, args); + } + + @Override + public String greeting(@PathVariable("username") String username) { + return String.format("Hello %s!\n", username); + } +} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties new file mode 100644 index 0000000000..5ff028510d --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=9090 \ No newline at end of file