Hystrix
1.概念
官网:https://github.com/Netflix/Hystrix/wiki
什么是Hystrix?
在分布式环境中,许多服务依赖项中的一些不可避免地会失败。Hystrix是一个库,可通过添加延迟容错和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,阻止它们之间的级联故障以及提供后备选项来实现这一目标,所有这些都可以提高系统的整体弹性。
1.1.雪崩效应
**分布式系统环境下,服务间类似依赖非常常见,一个业务调用通常依赖多个基础服务。当某个服务出现无法访问、异常、超时等问题时,用户的请求将会被阻塞,当有大批量请求调用该服务时,有可能造成大量的阻塞现象,并且这种不可用可能沿请求调用链向上传递,这种现象被称为雪崩效应。==**
原因:
- 硬件故障:如服务器宕机,机房断电,光纤被挖断等。
- 流量激增:如异常流量,重试加大流量等。
- 缓存穿透:一般发生在应用重启,所有缓存失效时,以及短时间内大量缓存失效时。大量的缓存不命中,使请求直击后端服务,造成服务提供者超负荷运行,引起服务不可用。
- 程序BUG:如程序逻辑导致内存泄漏,JVM长时间FullGC等。
- 同步等待:服务间采用同步调用模式,同步等待造成的资源耗尽。
策略:
- 硬件故障:多机房容灾、异地多活等。
- 流量激增:服务自动扩容、流量控制(限流、关闭重试)等。
- 缓存穿透:缓存预加载、缓存异步加载等。
- 程序BUG:修改程序bug、及时释放资源等。
- 同步等待:资源隔离、MQ解耦、不可用服务调用快速失败等。资源隔离通常指不同服务调用采用不同的线程池;不可用服务调用快速失败一般通过熔断器模式结合超时机制实现。
1.2.Hystrix作用
- 通过第三方客户端库访问(通常通过网络)依赖关系,以防止和控制延迟和故障。
- 在复杂的分布式系统中停止级联故障。
- 快速失败并迅速恢复。
- 在可能的情况下,后退并优雅地降级。
- 实现近实时监控,警报和操作控制。
2.GAV(pom)
<!--Hystrix:断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- hystrix仪表盘 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
3.主启动类上
@EnableCircuitBreaker//对hystrixR熔断机制的支持
@EnableHystrixDashboard //hystrixR仪表盘
4.controller
package com.qin.eurekaconsumer.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
/**
* @author WZB
* @date 2019/4/22 21:48
* @description 服务熔断
*/
@RestController
@RequestMapping("/hystrix/consumer/")
public class ConsumerEurekaHystrixController {
@Autowired
private RestTemplate restTemplate;
/*
*服务名
*
* */
private static final String SERVICE_NAME = "http://EUREKA-PROVIDER";
@RequestMapping("getProviderTest")
@HystrixCommand(fallbackMethod ="dealExceptionByHystrix")
public List<String> getProviderTest() {
//手动抛出异常,模拟断路器功能
int x=2/0;
List<String> o= restTemplate.getForObject(SERVICE_NAME+"/eurekacluster/getResult", List.class);
System.out.println(o);
return o;
}
public List<String> dealExceptionByHystrix(){
List<String> list=new ArrayList<>();
list.add("hystrix测试");
return list;
}
}
5.Feign支持
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
@RequestMapping(method = RequestMethod.GET, value = "/hello")
Hello iFailSometimes();
}
static class HystrixClientFallback implements HystrixClient {
@Override
public Hello iFailSometimes() {
return new Hello("fallback");
}
}
二.HystrixDashboard监控
1. 被监控的springcloud项目
被监控的项目如上1至5步骤,注意:
1.1. 主启动类上注入如下Bean
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
//springboot2.0 版本
//registrationBean.addUrlMappings("/actuator/hystrix.stream");
//springboot 低版本
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
1.2. yml配置
# 监控权限
management:
endpoint:
hystrix:
stream:
enabled: true
2. HystrixDashboard监控项目
2.1. GAV(pom)
<!-- hystrix仪表盘 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
3. 测试
启动被监控项目-----启动监控项目—访问监控项目(如http://localhost:8787/hystrix)------填入被监控项目的IP及port和后缀(如 http://localhost:6767/hystrix.stream 或者http://localhost:6767/actuator/hystrix.stream)
显示被监控服务的访问状态