From da1bdf90bfa524408821adb0d91447c0dd7663cd Mon Sep 17 00:00:00 2001 From: Christian Raedel Date: Mon, 22 Aug 2016 19:57:11 +0200 Subject: [PATCH] Added Feign Client --- spring-cloud-hystrix/pom.xml | 1 + .../pom.xml | 82 +++++++++++++++++++ .../hystrix/rest/consumer/GreetingClient.java | 21 +++++ .../RestConsumerFeignApplication.java | 32 ++++++++ .../resources/templates/greeting-view.html | 9 ++ 5 files changed, 145 insertions(+) create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/pom.xml create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/GreetingClient.java create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/java/com/baeldung/spring/cloud/hystrix/rest/consumer/RestConsumerFeignApplication.java create mode 100644 spring-cloud-hystrix/spring-cloud-hystrix-feign-rest-consumer/src/main/resources/templates/greeting-view.html diff --git a/spring-cloud-hystrix/pom.xml b/spring-cloud-hystrix/pom.xml index f7394d729e..2768a4f05b 100644 --- a/spring-cloud-hystrix/pom.xml +++ b/spring-cloud-hystrix/pom.xml @@ -9,6 +9,7 @@ spring-cloud-hystrix-rest-producer spring-cloud-hystrix-rest-consumer + spring-cloud-hystrix-feign-rest-consumer pom 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..e152b8233f --- /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/greeting/{username}", + 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/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..6cb70edf2d --- /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 + + +

+ + \ No newline at end of file