Skip to content

Commit d8e92f9

Browse files
Sample Hystrix implementation
1 parent 9450837 commit d8e92f9

9 files changed

Lines changed: 237 additions & 0 deletions

File tree

‎spring-cloud-hystrix/pom.xml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
<groupId>com.baeldung.spring.cloud</groupId>
7+
<artifactId>spring-cloud-hystrix</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
<modules>
10+
<module>spring-cloud-hystrix-rest-producer</module>
11+
<module>spring-cloud-hystrix-rest-consumer</module>
12+
</modules>
13+
<packaging>pom</packaging>
14+
15+
<name>Spring Cloud Hystrix</name>
16+
<description>Spring Cloud Hystrix Demo</description>
17+
18+
<parent>
19+
<groupId>com.baeldung</groupId>
20+
<artifactId>parent-modules</artifactId>
21+
<version>1.0.0-SNAPSHOT</version>
22+
<relativePath>..</relativePath>
23+
</parent>
24+
25+
<properties>
26+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
27+
</properties>
28+
29+
<build>
30+
<pluginManagement>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>3.5.1</version>
36+
<configuration>
37+
<source>1.8</source>
38+
<target>1.8</target>
39+
</configuration>
40+
</plugin>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
<version>1.4.0.RELEASE</version>
45+
</plugin>
46+
</plugins>
47+
</pluginManagement>
48+
</build>
49+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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-rest-consumer</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Spring Cloud Hystrix REST Consumer</name>
11+
<description>Spring Cloud Hystrix 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>org.springframework.cloud</groupId>
23+
<artifactId>spring-cloud-starter-hystrix</artifactId>
24+
<version>1.1.5.RELEASE</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
<version>1.4.0.RELEASE</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
34+
<version>1.4.0.RELEASE</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<dependencyManagement>
39+
<dependencies>
40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-parent</artifactId>
43+
<version>Brixton.SR4</version>
44+
<type>pom</type>
45+
<scope>import</scope>
46+
</dependency>
47+
</dependencies>
48+
</dependencyManagement>
49+
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-maven-plugin</artifactId>
59+
</plugin>
60+
</plugins>
61+
</build>
62+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.spring.cloud.hystrix.rest.consumer;
2+
3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
@Service
9+
public class GreetingService {
10+
@HystrixCommand(fallbackMethod = "defaultGreeting")
11+
public String getGreeting(String username) {
12+
return new RestTemplate().getForObject("http://localhost:9090/greeting/{username}", String.class, username);
13+
}
14+
15+
private String defaultGreeting(String username) {
16+
return "Hello User!";
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
12+
@SpringBootApplication
13+
@EnableCircuitBreaker
14+
@Controller
15+
public class RestConsumerApplication {
16+
@Autowired
17+
private GreetingService greetingService;
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(RestConsumerApplication.class, args);
21+
}
22+
23+
@RequestMapping("/get-greeting/{username}")
24+
public String getGreeting(Model model, @PathVariable("username") String username) {
25+
model.addAttribute("greeting", greetingService.getGreeting(username));
26+
return "greeting-view";
27+
}
28+
}
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>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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-rest-producer</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>Spring Cloud Hystrix REST Producer</name>
11+
<description>Spring Cloud Hystrix Sample REST Producer 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>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-web</artifactId>
24+
<version>1.4.0.RELEASE</version>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
</plugin>
34+
<plugin>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-maven-plugin</artifactId>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring.cloud.hystrix.rest.producer;
2+
3+
import org.springframework.web.bind.annotation.PathVariable;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
7+
public interface GreetingController {
8+
@RequestMapping("/greeting/{username}")
9+
String greeting(@PathVariable("username") String username);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.spring.cloud.hystrix.rest.producer;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@SpringBootApplication
10+
@RestController
11+
public class RestProducerApplication implements GreetingController {
12+
public static void main(String[] args) {
13+
SpringApplication.run(RestProducerApplication.class, args);
14+
}
15+
16+
@Override
17+
public String greeting(@PathVariable("username") String username) {
18+
return String.format("Hello %s!\n", username);
19+
}
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=9090

0 commit comments

Comments
 (0)