add s-c-hystrix-starter test
This commit is contained in:
1
hystrix/README.md
Normal file
1
hystrix/README.md
Normal file
@@ -0,0 +1 @@
|
||||
This project is a sample of using just the hystrix starter
|
||||
96
hystrix/pom.xml
Normal file
96
hystrix/pom.xml
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-sample-hystrix</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>spring-cloud-sample-hystrix</name>
|
||||
<description>Demo project for Spring Cloud</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-parent</artifactId>
|
||||
<version>1.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath /> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-hystrix</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!--skip deploy (this is just a test module) -->
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
|
||||
</project>
|
||||
41
hystrix/src/main/java/demo/HystrixApplication.java
Normal file
41
hystrix/src/main/java/demo/HystrixApplication.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
23
hystrix/src/main/java/demo/MyService.java
Normal file
23
hystrix/src/main/java/demo/MyService.java
Normal file
@@ -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";
|
||||
}
|
||||
}
|
||||
0
hystrix/src/main/resources/application.properties
Normal file
0
hystrix/src/main/resources/application.properties
Normal file
43
hystrix/src/test/java/demo/HystrixApplicationTests.java
Normal file
43
hystrix/src/test/java/demo/HystrixApplicationTests.java
Normal file
@@ -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<String> 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<String> 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user