SpringCloud(7)-Hystrix
SpringCloud(7)-Hystrix
前言
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免的失败!
服务雪崩
多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其他的微服务,这就是所谓的“扇出”。如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应"。
对于高流量的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几秒中内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列.线程和其他系统资源紧张,导致整个系统发生更多的级联故障,这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。
所以我们需要弃车保帅。
什么是Hystrix
Hystrix是一个用于处理分布式系统的延迟和容错的开源库, 在分布式系统里,许多依赖不可避免的会调用失败,比如超时,异常等,Hystrix能够保证在一 个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
”断路器“本身是一种开关装置, 当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝) ,向调用方返回一个服务预期的,可处理的备选响应(FallBack) 。而不是长时间的等待或者抛出调用方法无法处理的异常,这样就可以保证了服务调用方的线程不会被长时间,不必要的占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。Hystrix能够服务降级,服务熔断,服务限流,接近实时的监控等等。官网地址:https://github.com/Netflix/Hystrix/wiki。
Hystrix服务熔断
服务熔断:写在服务提供者。某个服务超时或者异常时,引起熔断。
- 创建maven子工程springcloud-provider-dept-hystrix-8001。
- 复制springcloud-provider-dept-8001的所有内容。
- 添加Hystrix的pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 修改主启动类DeptProvider_Hystrix_8001:
@EnableEurekaClient //在服务启动后自动注册到eureka中
@SpringBootApplication
@EnableCircuitBreaker //添加熔断支持
public class DeptProvider_Hystrix_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_Hystrix_8001.class,args);
}
}
- 修改controller类,添加熔断方法hystrixQueryById,修改方法queryById:
@GetMapping("/dept/get/{id}")
@HystrixCommand(fallbackMethod = "hystrixQueryById")
public Dept queryById(@PathVariable("id") long id){
Dept dept = deptServiceImpl.queryById(id);
if (dept==null){
throw new RuntimeException();
}
return dept;
}
public Dept hystrixQueryById(@PathVariable("id") long id){
Dept dept = new Dept();
dept.setDeptno(id);
dept.setDeptname("id=>"+id+",没有对应信息");
dept.setDbSource("在MySQL中未找到对应信息");
return dept;
}
- 启动Eureka服务器7001,Hystrix服务提供者,服务消费者80,访问http://localhost/consumer/dept/get/10,测试:

服务降级
服务降级:写在服务消费者。当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或换种简单的方式处理,从而释放服务器资源以保证核心交易正常运作或高效运作。
- 在springcloud-api项目中的service包下新建一个DeptService_FallbackFactory类,实现FallbackFactory接口。
public class DeptService_FallbackFactory implements FallbackFactory<DeptService_Feign> {
public DeptService_Feign create(Throwable throwable) {
return new DeptService_Feign() {
public List<Dept> queryAll() {
return null;
}
public Dept queryById(long id) {
Dept dept = new Dept();
dept.setDeptname("因其他服务器压力大,暂时关闭此服务。");
return dept;
}
public boolean addDept(Dept dept) {
return false;
}
};
}
}
- 修改类DeptService_Feign中的@FeignClient注解
@FeignClient(value = "springcloud-provider-dept",fallbackFactory = DeptService_FallbackFactory.class)
- 在springcloud-consumer-dept-feign-80项目中修改application.yml,添加服务降级的支持:
feign:
hystrix:
enabled: true
- 启动Eureka服务器7001,服务提供者,服务消费者80,访问http://localhost/consumer/dept/get/10,此时关闭服务提供者,再次访问测试:

Hystrix流监控Dashboard
- 创建maven子项目springcloud-consumer-dept-hystrix-dashboard-9001。
- 在pom文件中添加Hystrix依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
- 添加主启动类HystrixDashboard_9001:
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboard_9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboard_9001.class,args);
}
}
- 在项目springcloud-provider-dept-hystrix-8001中,必须要有监控依赖,要检测的方法上要有@HystrixCommand注解。
@GetMapping("/dept/get/{id}")
@HystrixCommand(fallbackMethod = "hystrixQueryById")
public Dept queryById(@PathVariable("id") long id){
Dept dept = deptServiceImpl.queryById(id);
if (dept==null){
throw new RuntimeException();
}
return dept;
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 在springcloud-provider-dept-hystrix-8001的主启动类中增加一个servlet:
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
- 启动Hystrix服务提供者8001,服务消费者80,Eureka服务器7001,Hystrix流监控9001测试:

- 相关解释:


浙公网安备 33010602011771号