目标
创建2个service,student service 和 school service,school service 会调用 student service,school service 中集成 hystrix。
启动2个服务,访问 school service,正常情况下调用 student service 返回结果,停掉 student service,返回 hystrix 的处理信息。
创建 student service
https://start.spring.io,Spring Boot 1.5.16,Artifact:hystrix-student,依赖:web, rest repositories, actuator。
pom.xml
<?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>com.example</groupId>
<artifactId>hystrix-student</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hystrix-student</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port = 8098
StudentServiceController.java
@RestController
public class StudentServiceController {
private static Map<String, List<Student>> schooDB = new HashMap<String, List<Student>>();
static {
schooDB = new HashMap<String, List<Student>>();
List<Student> lst = new ArrayList<Student>();
Student std = new Student("Sajal", "Class IV");
lst.add(std);
std = new Student("Lokesh", "Class V");
lst.add(std);
schooDB.put("abcschool", lst);
lst = new ArrayList<Student>();
std = new Student("Kajal", "Class III");
lst.add(std);
std = new Student("Sukesh", "Class VI");
lst.add(std);
schooDB.put("xyzschool", lst);
}
@RequestMapping(value = "/getStudentDetailsForSchool/{schoolname}", method = RequestMethod.GET)
public List<Student> getStudents(@PathVariable String schoolname) {
System.out.println("Getting Student details for " + schoolname);
List<Student> studentList = schooDB.get(schoolname);
if (studentList == null) {
studentList = new ArrayList<Student>();
Student std = new Student("Not Found", "N/A");
studentList.add(std);
}
return studentList;
}
}
Student.java
public class Student {
private String name;
private String className;
public Student(String name, String className) {
super();
this.name = name;
this.className = className;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
启动,访问 http://localhost:8098/getStudentDetailsForSchool/abcschool
创建 school service
Artifact:hystrix-school,依赖:web, rest repositories, actuator, hystrix, hystrix dashboard。
application.properties
server.port = 9098
启动类上开启 hystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
SchoolServiceController.java
@RestController
public class SchoolServiceController {
@Autowired
StudentServiceDelegate studentServiceDelegate;
@RequestMapping(value = "/getSchoolDetails/{schoolname}", method = RequestMethod.GET)
public String getStudents(@PathVariable String schoolname) {
System.out.println("Going to call student service to get data!");
return studentServiceDelegate.callStudentServiceAndGetData(schoolname);
}
}
StudentServiceDelegate.java
@Service
public class StudentServiceDelegate {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "callStudentServiceAndGetData_Fallback")
public String callStudentServiceAndGetData(String schoolname) {
System.out.println("Getting School details for " + schoolname);
String response = restTemplate
.exchange("http://localhost:8098/getStudentDetailsForSchool/{schoolname}"
, HttpMethod.GET
, null
, new ParameterizedTypeReference<String>() {
}, schoolname).getBody();
System.out.println("Response Received as " + response + " - " + new Date());
return "NORMAL FLOW !!! - School Name - " + schoolname + " ::: " +
" Student Details " + response + " - " + new Date();
}
@SuppressWarnings("unused")
private String callStudentServiceAndGetData_Fallback(String schoolname) {
System.out.println("Student Service is down!!! fallback route enabled...");
return "CIRCUIT BREAKER ENABLED!!! No Response From Student Service at this moment. " +
" Service will be back shortly - " + new Date();
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
启动,访问 http://localhost:9098/getSchoolDetails/abcschool
Hystrix 测试
停掉 student service,再次访问 http://localhost:9098/getSchoolDetails/abcschool
Hystrix Dashboard
hystrix stream 地址:http://localhost:9098/hystrix.stream
可视化仪表盘地址:http://localhost:9098/hystrix
把 hystrix stream 地址添加进去,就可以看到统计结果