项目源码下载:(访问密码:9987)
Spring-Cloud-Circuit-Breaker.zip
学习在调用底层微服务的同时利用调用的Spring Cloud Netflix堆栈组件之一Hystrix
来实现断路器。在一些底层服务永久宕机/抛出错误的应用中,一般需要开启容错功能,我们需要自动回退到不同的程序执行路径。这与使用大量底层微服务的生态系统的分布式计算风格有关。这是断路器模式的帮助所在,也是构建此断路器的工具
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
@EnableCircuitBreaker
注释@EnableHystrixDashboard
注释@HystrixCommand(fallbackMethod = "myFallbackMethod")
Student
实体的一些基本功能。这将是一个基于 REST 的服务。我们将从School
Service调用此服务以了解断路器。它将8098
在本地主机的端口上运行。Hystrix
. Student
将从这里调用服务,一旦学生服务不可用,我们将测试回退路径。它将在本地主机的 9098 端口上运行。Web
、Rest Repositories
和Actuator
。给出其他maven GAV坐标并下载项目。application.properties
并添加端口信息。
server.port = 8098
这将使该应用程序在默认端口 8098 上运行。我们可以通过 -Dserver.port = XXXX
在启动服务器时提供参数轻松覆盖它。StudentServiceController
并公开一个 rest 端点,用于获取特定学校的所有学生详细信息。在这里,我们公开/getStudentDetailsForSchool/{schoolname}
端点以服务于业务目的。为简单起见,我们对学生的详细信息进行了硬编码。import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.howtodoinjava.springhystrixstudentservice.domain.Student;
@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;
}
}
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;
}
}
mvn clean install
使用 command进行最终构建并运行服务器java -jar target\spring-hystrix-student-service-0.0.1-SNAPSHOT.jar
。这将在默认端口启动学生服务8098
。http://localhost:8098/getStudentDetailsForSchool/abcschool
.application.properties
并添加端口信息。
server.port = 9098
这将使该应用程序在默认端口 9098 上运行。我们可以通过 -Dserver.port = XXXX
在启动服务器时提供参数来轻松覆盖它。SpringHystrixSchoolServiceApplication
即生成的类,@SpringBootApplication
并添加@EnableHystrixDashboard
和@EnableCircuitBreaker
注释。import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SpringHystrixSchoolServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringHystrixSchoolServiceApplication.class, args);
}
}
SchoolServiceController
Rest Controller,我们将在其中公开/getSchoolDetails/{schoolname}
端点,该端点将简单地返回学校详细信息及其学生详细信息。对于学生详细信息,它将调用已经开发的学生服务端点。我们将创建一个委托层StudentServiceDelegate.java
来调用学生服务。这个简单的代码看起来像import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.howtodoinjava.springhystrixschoolservice.delegate.StudentServiceDelegate;
@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);
}
}
学生服务代表RestTemplate
@HystrixCommand(fallbackMethod = "callStudentServiceAndGetData_Fallback")
– 这意味着我们将不得不添加另一个callStudentServiceAndGetData_Fallback
具有相同签名的方法,该方法将在实际学生服务关闭时调用。callStudentServiceAndGetData_Fallback
它只会返回一些默认值。import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@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();
}
}
mvn clean install
使用 command进行最终构建并运行服务器java -jar target\spring-hystrix-school-service-0.0.1-SNAPSHOT.jar
。这将在默认端口9098 中启动学校服务。http://localhost:9098/getSchoolDetails/abcschool
。它应该在浏览器中显示以下输出:http://localhost:9098/getSchoolDetails/abcschool
.CTRL + C
在学生服务服务器控制台(停止服务器)和浏览器再次测试该学校的服务。这次它将返回回退方法响应。在这里,Hystrix 出现了,它频繁地监视学生服务,当它关闭时,Hystrix 组件已打开电路并启用回退路径。项目源码下载:(访问密码:9987)
Spring-Cloud-Circuit-Breaker.zip
地址:https://www.leftso.com/article/860.html