Skip to content

Commit da1bdf9

Browse files
Added Feign Client
1 parent e618804 commit da1bdf9

5 files changed

Lines changed: 145 additions & 0 deletions

File tree

‎spring-cloud-hystrix/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<modules>
1010
<module>spring-cloud-hystrix-rest-producer</module>
1111
<module>spring-cloud-hystrix-rest-consumer</module>
12+
<module>spring-cloud-hystrix-feign-rest-consumer</module>
1213
</modules>
1314
<packaging>pom</packaging>
1415

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>spring-cloud-hystrix-feign-rest-consumer</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Spring Cloud Hystrix Feign REST Consumer</name>
11+
<description>Spring Cloud Hystrix Feign Sample Implementation</description>
12+
13+
<parent>
14+
<groupId>com.baeldung.spring.cloud</groupId>
15+
<artifactId>spring-cloud-hystrix</artifactId>
16+
<version>1.0.0-SNAPSHOT</version>
17+
<relativePath>..</relativePath>
18+
</parent>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.baeldung.spring.cloud</groupId>
23+
<artifactId>spring-cloud-hystrix-rest-producer</artifactId>
24+
<version>1.0.0-SNAPSHOT</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.cloud</groupId>
28+
<artifactId>spring-cloud-starter-hystrix</artifactId>
29+
<version>1.1.5.RELEASE</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.cloud</groupId>
33+
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
34+
<version>1.1.5.RELEASE</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-starter-feign</artifactId>
39+
<version>1.1.5.RELEASE</version>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-web</artifactId>
44+
<version>1.4.0.RELEASE</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
49+
<version>1.4.0.RELEASE</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-starter-actuator</artifactId>
54+
<version>1.4.0.RELEASE</version>
55+
</dependency>
56+
</dependencies>
57+
58+
<dependencyManagement>
59+
<dependencies>
60+
<dependency>
61+
<groupId>org.springframework.cloud</groupId>
62+
<artifactId>spring-cloud-starter-parent</artifactId>
63+
<version>Brixton.SR4</version>
64+
<type>pom</type>
65+
<scope>import</scope>
66+
</dependency>
67+
</dependencies>
68+
</dependencyManagement>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-compiler-plugin</artifactId>
75+
</plugin>
76+
<plugin>
77+
<groupId>org.springframework.boot</groupId>
78+
<artifactId>spring-boot-maven-plugin</artifactId>
79+
</plugin>
80+
</plugins>
81+
</build>
82+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring.cloud.hystrix.rest.consumer;
2+
3+
import com.baeldung.spring.cloud.hystrix.rest.producer.GreetingController;
4+
import org.springframework.cloud.netflix.feign.FeignClient;
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
8+
@FeignClient(
9+
name = "rest-producer",
10+
url = "http://localhost:9090/greeting/{username}",
11+
fallback = GreetingClient.GreetingClientFallback.class
12+
)
13+
public interface GreetingClient extends GreetingController {
14+
@Component
15+
public static class GreetingClientFallback implements GreetingClient {
16+
@Override
17+
public String greeting(@PathVariable("username") String username) {
18+
return "Hello User!";
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.spring.cloud.hystrix.rest.consumer;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
7+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
8+
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.ui.Model;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
14+
@SpringBootApplication
15+
@EnableCircuitBreaker
16+
@EnableHystrixDashboard
17+
@EnableFeignClients
18+
@Controller
19+
public class RestConsumerFeignApplication {
20+
@Autowired
21+
private GreetingClient greetingClient;
22+
23+
public static void main(String[] args) {
24+
SpringApplication.run(RestConsumerFeignApplication.class, args);
25+
}
26+
27+
@RequestMapping("/get-greeting/{username}")
28+
public String getGreeting(Model model, @PathVariable("username") String username) {
29+
model.addAttribute("greeting", greetingClient.greeting(username));
30+
return "greeting-view";
31+
}
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Greetings from Hystrix</title>
5+
</head>
6+
<body>
7+
<h2 th:text="${greeting}"/>
8+
</body>
9+
</html>

0 commit comments

Comments
 (0)