Add hystrix/turbine amqp samples

This commit is contained in:
Dave Syer
2016-03-04 13:13:56 +00:00
parent 3461305e95
commit 7b8792cc1a
15 changed files with 412 additions and 1 deletions

View File

@@ -1,7 +1,6 @@
spring:
application:
name: bus
---
spring:
profiles: other

1
hystrix-amqp/README.md Normal file
View File

@@ -0,0 +1 @@
This project is a sample of using just the hystrix starter

View File

@@ -0,0 +1,5 @@
rabbitmq:
image: rabbitmq:management
ports:
- "5672:5672"
- "15672:15672"

108
hystrix-amqp/pom.xml Normal file
View File

@@ -0,0 +1,108 @@
<?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-amqp</artifactId>
<version>Brixton.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-sample-hystrix-amqp</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.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.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</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>

View 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);
}
}

View 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";
}
}

View File

@@ -0,0 +1,3 @@
# This way you get to see the exceptions when the fallback is accessed
logging.level.com.netflix.hystrix.AbstractCommand: DEBUG
logging.level.org.springframework.integration: DEBUG

View File

@@ -0,0 +1,63 @@
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.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.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.InputStream;
import java.net.URL;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HystrixApplication.class)
@IntegrationTest("server.port=0")
@WebAppConfiguration
@DirtiesContext
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());
}
@Test
public void hystrixStreamWorks() throws Exception {
String url = "http://localhost:" + port;
//you have to hit a Hystrix circuit breaker before the stream sends anything
ResponseEntity<String> response = new TestRestTemplate().getForEntity(url, String.class);
assertEquals("bad response code", HttpStatus.OK, response.getStatusCode());
URL hystrixUrl = new URL(url + "/hystrix.stream");
InputStream in = hystrixUrl.openStream();
byte[] buffer = new byte[1024];
in.read(buffer);
String contents = new String(buffer);
assertTrue(contents.contains("ping"));
in.close();
}
}

View File

@@ -0,0 +1,5 @@
rabbitmq:
image: rabbitmq:management
ports:
- "5672:5672"
- "15672:15672"

108
turbine-amqp/pom.xml Normal file
View File

@@ -0,0 +1,108 @@
<?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-turbine-amqp</artifactId>
<version>Brixton.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-sample-turbine-amqp</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>demo.DemoApplication</start-class>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine-amqp</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>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/release</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@@ -0,0 +1,14 @@
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;
@SpringBootApplication
@EnableTurbineStream
public class TurbineStreamApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineStreamApplication.class, args);
}
}

View File

@@ -0,0 +1,4 @@
server.port: 8989
spring.application.name: turbine
turbine.appConfig: simple
turbine.aggregator.clusterConfig: SIMPLE

View File

@@ -0,0 +1,31 @@
package demo;
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.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TurbineStreamApplication.class)
@DirtiesContext
public class TurbineStreamApplicationTests {
@Autowired
DiscoveryClient discoveryClient;
@Test
public void contextLoads() {
}
@Test
public void discoveryClientIsEureka() {
assertTrue("discoveryClient is wrong type", discoveryClient instanceof EurekaDiscoveryClient);
}
}

View File

@@ -0,0 +1,4 @@
server.port: 8761
spring.application.name: eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

View File

@@ -0,0 +1,2 @@
server.port: 8080
spring.application.name: simple