diff --git a/pom.xml b/pom.xml index c64acc145c..43fcedcf8e 100644 --- a/pom.xml +++ b/pom.xml @@ -87,7 +87,7 @@ spring-rest-angular spring-rest-docs spring-cloud-config - spring-cloud-eureka + spring-cloud-hystrix spring-security-basic-auth spring-security-custom-permission diff --git a/spring-cloud-hystrix/pom.xml b/spring-cloud-hystrix/pom.xml new file mode 100644 index 0000000000..2768a4f05b --- /dev/null +++ b/spring-cloud-hystrix/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + spring-cloud-hystrix + 1.0.0-SNAPSHOT + + spring-cloud-hystrix-rest-producer + spring-cloud-hystrix-rest-consumer + spring-cloud-hystrix-feign-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-feign-rest-consumer/pom.xml b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/pom.xml new file mode 100644 index 0000000000..d2716e897e --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + spring-cloud-hystrix-feign-rest-consumer + 1.0.0-SNAPSHOT + jar + + Spring Cloud Hystrix Feign REST Consumer + Spring Cloud Hystrix Feign Sample Implementation + + + com.baeldung.spring.cloud + spring-cloud-hystrix + 1.0.0-SNAPSHOT + .. + + + + + com.baeldung.spring.cloud + spring-cloud-hystrix-rest-producer + 1.0.0-SNAPSHOT + + + org.springframework.cloud + spring-cloud-starter-hystrix + 1.1.5.RELEASE + + + org.springframework.cloud + spring-cloud-starter-hystrix-dashboard + 1.1.5.RELEASE + + + org.springframework.cloud + spring-cloud-starter-feign + 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.boot + spring-boot-starter-actuator + 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-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java new file mode 100644 index 0000000000..b715e8c052 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java @@ -0,0 +1,21 @@ +package com.baeldung.spring.cloud.hystrix.rest.consumer; + +import com.baeldung.spring.cloud.hystrix.rest.producer.GreetingController; +import org.springframework.cloud.netflix.feign.FeignClient; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.PathVariable; + +@FeignClient( + name = "rest-producer", + url = "http://localhost:9090", + fallback = GreetingClient.GreetingClientFallback.class +) +public interface GreetingClient extends GreetingController { + @Component + public static class GreetingClientFallback implements GreetingClient { + @Override + public String greeting(@PathVariable("username") String username) { + return "Hello User!"; + } + } +} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java new file mode 100644 index 0000000000..b97d84eaf2 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java @@ -0,0 +1,32 @@ +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.cloud.netflix.feign.EnableFeignClients; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; +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 +@EnableHystrixDashboard +@EnableFeignClients +@Controller +public class RestConsumerFeignApplication { + @Autowired + private GreetingClient greetingClient; + + public static void main(String[] args) { + SpringApplication.run(RestConsumerFeignApplication.class, args); + } + + @RequestMapping("/get-greeting/{username}") + public String getGreeting(Model model, @PathVariable("username") String username) { + model.addAttribute("greeting", greetingClient.greeting(username)); + return "greeting-view"; + } +} diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/application.properties b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/application.properties new file mode 100644 index 0000000000..3cf12afeb9 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8082 diff --git a/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html new file mode 100644 index 0000000000..302390fde0 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html @@ -0,0 +1,9 @@ + + + + Greetings from Hystrix + + +

+ + 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..c9be67c302 --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/pom.xml @@ -0,0 +1,72 @@ + + + 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.cloud + spring-cloud-starter-hystrix-dashboard + 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.boot + spring-boot-starter-actuator + 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..9df745b1c6 --- /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,31 @@ +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.cloud.netflix.hystrix.EnableHystrix; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; +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 +@EnableHystrixDashboard +@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/application.properties b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/application.properties new file mode 100644 index 0000000000..4c00e40deb --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-consumer/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8080 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..302390fde0 --- /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 + + +

+ + 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..9ce9d88ffb --- /dev/null +++ b/spring-cloud-hystrix/spring-cloud-hystrix-rest-producer/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=rest-producer +server.port=9090