【spring cloud 第五篇】Hystrix 熔断器
文章目录
- 1 Hystrix 介绍
- 1.1 简介
- 1.2 hystrix设计原则
- 1.3 hystrix 工作机制
- 2 RestTemplate和Ribbon 上使用熔断器的demo
- 2.1 pom.xml添加 hystrix依赖
- 2.2 EurekaRibbonClientApplication启动类
- 2.3 新建RibbonServiceImpl类
- 2.4 编写 RibbonController
- 2.5 查看结果
- 3 使用 hystrix dashboard监控熔断器状态
- 3.1 hystrix dashboard简介
- 3.2 集成hystrix dashboard遇到的坑
- 3.3 集成hystrix dashboard 的demo
1 Hystrix 介绍
1.1 简介
分布式系统中,服务于服务之间的调用错综复杂,当某个服务出现故障,会导致依赖它的服务远程调用时线程阻塞。
hystrix提供了熔断器功能,可以阻止分布式系统中出现联动的故障。
hystrix 是通过隔离服务访问点阻止联动故障,从而提高整个分布式系统的高可用;
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。
1.2 hystrix设计原则
- 1 快速失败(如果一个服务出现故障,则调用该服务的请求快速失败,而不是线程等待)
- 2 提供回退[fallback]方案(在请求发生故障时,提供设定好的回退方案)
- 3 提供熔断机制,防止故障扩散到其他服务
- 4 提供熔断器监控组件 hystrix dashboard,实时监控熔断器状态
1.3 hystrix 工作机制
- 当服务的某个API失败次数在一定时间内小于设置的阈值时,熔断器处于- 关闭状态,该API正常提供服务。
- 失败次数 > 设定阈值,hystrix判定API接口出现故障,打开熔断器
- 一定时间后出于半打开状态,处理一定数量请求,失败,则熔断器继续打开;成功,则关闭熔断器【具有自我修复】
该图片来自 https://blog.csdn.net/qq\_25484147/article/details/83375225 (有趣的灵魂配图)
2 RestTemplate和Ribbon 上使用熔断器的demo
在【spring cloud 第三篇】ribbon 负载均衡文章 1.2 @LoadBalanced结合RestTemplate实现负载均衡 的基础上进行重构
修改eureka-ribbon-client服务如下:
- pom.xml添加 hystrix依赖
2.1 pom.xml添加 hystrix依赖
<?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">
<parent>
<artifactId>eureka-master</artifactId>
<groupId>com.qgg</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-ribbon-client</artifactId>
<dependencies>
<!--eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<!--ribbon 负载均衡依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
</dependencies>
</project>
2.2 EurekaRibbonClientApplication启动类
在EurekaRibbonClientApplication启动类上添加 @EnableHystrix ,开启hystrix熔断器功能
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix // 开启hystrix熔断器功能
public class EurekaRibbonClientApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonClientApplication.class, args);
}
}
2.3 新建RibbonServiceImpl类
- 新建RibbonServiceImpl类
- 新建getPortForHystrix方法,
- 在getPortForHystrix方法上添加@HystrixCommand注解,指定fallbackMethod(回退)方法
getPortError回退方法必须跟getPortForHystrix的入参和返回参数一样
@Service
public class RibbonServiceImpl {@Autowired
private RestTemplate restTemplate;
/** * @HystrixCommand 的fallbackMethod指定回退的方法 * @param name * @return */
@HystrixCommand(fallbackMethod = "getPortError")
public String getPortForHystrix(String name) {
String result = restTemplate.getForObject("http://eureka-client-4/getPort/"+name,String.class);
return result;
}
public String getPortError(String name) {
String result = "获取端口出错了-hystrix.test》:"+name;
return result;
}
2.4 编写 RibbonController
@RestController
@RequestMapping("/myRibbon")
public class RibbonController {
@Autowired
private RibbonServiceImpl ribbonServiceImpl;
@RequestMapping(value = "/getPortForHystrix/{name}")
public String getPortForHystrix(@PathVariable String name) {
String result = ribbonServiceImpl.getPortForHystrix(name);
return result;
}
}
2.5 查看结果
- 启动 eureka-server 和 eureka-client,再启动eureka-ribbon-client
- 访问http://localhost:8761/ 如下图:
- 访问http://localhost:8766/myRibbon/getPortForHystrix/恰恰 ,返回正常
【奇怪现象:debug模式调试的时候都是返回:获取端口出错了-hystrix.test》:恰恰】
恰恰-8763
关闭eureka-client服务继续访问 http://localhost:8766/myRibbon/getPortForHystrix/恰恰 结果返回如下:
获取端口出错了-hystrix.test》:恰恰
当被调用的服务不可用时,会开启熔断器,进入fallback方法,线程不在阻塞
3 使用 hystrix dashboard监控熔断器状态
3.1 hystrix dashboard简介
Hystrix Dashboard 是监控hystrix的熔断器状态的一个组件,提供了数据监控和油耗的图形化展示界面。
3.2 集成hystrix dashboard遇到的坑
- 原来使用spring-cloud-dependencies的版本Dalston.SR1 ,对应的spring-boot-starter-parent的版本是1.5.2,在集成hystrix dashboard后,访问http://localhost:8766/hystrix.stream 一直打印ping…,访问http://localhost:8766/hystrix 也一直显示在loading中,刷新页面还报错
- 找了一些资料说是版本问题,在升级spring-cloud和spring-boot对应的版本兼容也遇到一些坑,查找spring-cloud和spring-boot的版本对应关系:访问 https://spring.io/ —> projects —> spring-cloud – doc,再仔细找找就能找到
- 将spring-cloud-dependencies版本跟换成了Finchley.SR3,spring-boot-starter-parent版本换成了2.0.4.RELEASE
- eureka-server服务中的 spring-cloud-starter-eureka-server 从1.3.1升级到1.4.3;eureka-client和eureka-ribbon-client服务中的 spring-cloud-starter-eureka从1.3.1升级到1.4.3,spring-cloud-starter-ribbon也是从1.3.1升级到1.4.3,包括spring-cloud-starter-hystrix和spring-cloud-starter-hystrix-dashboard都是使用1.4.3的
3.3 集成hystrix dashboard 的demo
在本章第2节的基础上进行重构
在pom文件中添加Actuator起步依赖,hystrix-dashboard起步依赖和hystrix起步依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.3.RELEASE</version>
<!--<version>1.3.1.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.3.RELEASE</version>
<!--<version>1.3.1.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
在程序启动类添加 @EnableHystrixDashboard // 开启监控熔断器状态
同时要注入 ServletRegistrationBean 实例,不然在访问 http://localhost:8766/hystrix.stream是404
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix // 开启hystrix熔断器功能
@EnableHystrixDashboard // 开启监控熔断器状态
public class EurekaRibbonClientApplication {@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
public static void main(String[] args) {
SpringApplication.run(EurekaRibbonClientApplication.class, args);
}
}
一次启动工程 eureka-server,eureka-client,eureka-ribbon-client,在浏览器访问http://localhost:8766/hystrix.stream,在调用http://localhost:8766/myRibbon/getPortForHystrix/熔断监控测试,打印熔断器的数据指标如下![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FpYXppbGlwaW5n_size_16_color_FFFFFF_t_70_pic_center 6]
- 再访问http://localhost:8766/hystrix,依次填写http://localhost:8766/hystrix.stream、2000、xxx(这个随意填写)
- 点击“monitor” ,显示了熔断器的各种指标,各指标含义可查看官方文档^^
还没有评论,来说两句吧...