diff --git a/hystrix/README.md b/hystrix/README.md new file mode 100644 index 0000000..e146fe4 --- /dev/null +++ b/hystrix/README.md @@ -0,0 +1 @@ +This project is a sample of using just the hystrix starter diff --git a/hystrix/pom.xml b/hystrix/pom.xml new file mode 100644 index 0000000..920b825 --- /dev/null +++ b/hystrix/pom.xml @@ -0,0 +1,96 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-hystrix + 1.0.0.BUILD-SNAPSHOT + jar + + spring-cloud-sample-hystrix + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-hystrix + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + maven-deploy-plugin + + true + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/hystrix/src/main/java/demo/HystrixApplication.java b/hystrix/src/main/java/demo/HystrixApplication.java new file mode 100644 index 0000000..d7a22e1 --- /dev/null +++ b/hystrix/src/main/java/demo/HystrixApplication.java @@ -0,0 +1,41 @@ +package demo; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Configuration +@ComponentScan +@EnableAutoConfiguration +@EnableCircuitBreaker +@RestController +public class HystrixApplication { + + @Bean + public MyService myService() { + return new MyService(); + } + + @Autowired + private MyService myService; + + @RequestMapping("/") + public String ok() { + return myService.ok(); + } + + @RequestMapping("/fail") + public String fail() { + return myService.fail(); + } + + public static void main(String[] args) { + SpringApplication.run(HystrixApplication.class, args); + } +} diff --git a/hystrix/src/main/java/demo/MyService.java b/hystrix/src/main/java/demo/MyService.java new file mode 100644 index 0000000..ad995e3 --- /dev/null +++ b/hystrix/src/main/java/demo/MyService.java @@ -0,0 +1,23 @@ +package demo; + +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; + +/** + * @author Spencer Gibb + */ +public class MyService { + + @HystrixCommand(fallbackMethod = "fallback") + public String ok() { + return "OK"; + } + + @HystrixCommand(fallbackMethod = "fallback") + public String fail() { + throw new RuntimeException("fail now"); + } + + public String fallback() { + return "from the fallback"; + } +} diff --git a/hystrix/src/main/resources/application.properties b/hystrix/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/hystrix/src/test/java/demo/HystrixApplicationTests.java b/hystrix/src/test/java/demo/HystrixApplicationTests.java new file mode 100644 index 0000000..c812e32 --- /dev/null +++ b/hystrix/src/test/java/demo/HystrixApplicationTests.java @@ -0,0 +1,43 @@ +package demo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HystrixApplication.class) +@IntegrationTest("server.port=0") +@WebAppConfiguration +public class HystrixApplicationTests { + + @Value("${local.server.port}") + int port; + + @Test + public void testOk() { + ResponseEntity response = new TestRestTemplate().getForEntity("http://localhost:" + port, String.class); + assertNotNull("response was null", response); + assertEquals("Bad status code", HttpStatus.OK, response.getStatusCode()); + assertEquals("Wrong response text", "OK", response.getBody()); + } + + @Test + public void testFallback() { + ResponseEntity response = new TestRestTemplate().getForEntity("http://localhost:" + port+"/fail", String.class); + assertNotNull("response was null", response); + assertEquals("Bad status code", HttpStatus.OK, response.getStatusCode()); + assertEquals("Wrong response text", "from the fallback", response.getBody()); + } +} diff --git a/pom.xml b/pom.xml index 6a4788c..de3e4b5 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,7 @@ feign feign-eureka + hystrix zuul-proxy zuul-proxy-eureka