注:本人所有的spring-cloud系列的文章均为黑马的《Spring Cloud微服务架构开发》的个人笔记。
- 了解Hystrix的作用
- 掌握Hystrix的基本用法
- 熟悉如何在Feign 中使用Hystrix熔断器
- 熟悉如何使用Hystrix Dashboard监控熔断器的状态
- 熟悉如何使用Hystrix 和Turbine进行聚合监控
服务容错保护库Hystrix
Spring Cloud中的 Hystrix是Netflix开源的一款针对分布式系统 延迟和容错 的库,其目的 是通过添加延迟容忍和容错逻辑,从而控制分布式服务之间的交互。
对于一个复杂的分布式系统,包含的应用可能多达数十个,这些应用有许多依赖项目,每个依赖项目在某个时刻不可避免会失败导致故障,如果不对这些故障进行隔离,整个分布式系统都可能会崩溃。
当一个服务的某一个接口出现问题的时候,其他接口也可能不能正常使用,出现线程阻塞,从而影响用户的整个请求,也有可能导致雪崩效应(级联故障)
Hystrix被设计的目标是阻止级联故障,对通过第三方客户端访问的依赖项的延迟和故障进行保护和控制。
Hystrix.实现这一目标的大致思路具体如下:
(1)将外部依赖的访问请求封装在独立的线程中,进行资源隔离。
(2)对于超出设定阈值的服务调用,直接进行超时,不允许其耗费过长时间阻塞线程。
(3)每个依赖服务维护一个独立的线程池,一旦线程池满了,直接拒绝服务的调用。
(4)统计依赖服务调用的成功次数、失败次数、拒绝次数、超时次数等结果。
(5)在一段时间内,如果服务调用的异常次数超过一定阈值,就会触发熔断停止对特定服务的所有请求,在一定时间内对服务调用直接降级,一段时间后再次进行自动尝试恢复。
(6)如果某个服务出现调用失败、被拒绝、超时等异常情况,自动调用fallback降级机制。
( 7)实时监控指标和配置变化。
文章目录:
- Hystrix快速入门
- 在Feign中使用Hystrix熔断
- Hystrix整个工作流
- 使用Hystrix Dashboard 监控熔断器状态
- 使用Hystrix 和 Turbine 进行聚合监控
1.Hystrix快速入门
使用之前的eureka-server,创建新的模块hystrix-provider和eureka-hystrix-client
1.1 创建hystrix-provider模块
1.1.1 引入web ,eureka-client ,test依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.li</groupId>
<artifactId>hystrix-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hystrix-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
1.1.2 创建controller包,创建HystrixController类
package com.li.hystrixprovider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@GetMapping("/hi")
public String hi(String id){
return "hello,monkey:"+id+"I'm hystrix";
}
}
1.1.3 编辑application.yml
spring:
application:
name: hystrix-provider
server:
port: 7006
eureka:
client:
service-url:
defaultZone:
http://localhost:7000/eureka/
instance:
#该服务实例所在主机名
hostname: localhost
1.1.4 在启动类添加注解@EnableEurekaClient
.
1.2 创建eureka-hystrix-client模块
1.2.1 添加依赖test,eureka-client,web,hystrix,feign,ribbon
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.li</groupId>
<artifactId>eureka-hystrix-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-hystrix-client</name>
<description>eureka-hystrix-client</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.1.2 编辑application.yml
spring:
application:
name: eureka-hystrix-client
server:
port: 8764
eureka:
client:
service-Url:
defaultZone: http://localhost/euraka
1.2.3 在启动类中添加@EnableHystrix,@EnableEurekaClient
1.2.4 创建config包HystrixConfig配置类
package com.li.eurekahystrixclient.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class HystrixConfig {
@Bean
@LoadBalanced
public RestTemplate template(){
return new RestTemplate();
}
/* public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
} 两个方法都可以*/
}
1.2.5 创建service包HystrixService类
注意:
@HystrixCommand(fallbackMethod = “hiError”)
//此注解为当访问hi()异常时,访问hiError()方法,对其进行服务降级
package com.li.eurekahystrixclient.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;
@Service
public class HystrixService {
@Autowired
private RestTemplate restTemplate;
//此注解为当访问hi()失败时,访问hiError()方法
@HystrixCommand(fallbackMethod = "hiError")
public String hi(String id){
return restTemplate.getForObject("http://hystrix-provider/hi?id=" + id,String.class);
}
public String hiError(String id){
return "服务被降级,当前id="+id;
}
}
1.2.6 创建controller包HystrixController类
package com.li.eurekahystrixclient.controller;
import com.li.eurekahystrixclient.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@Autowired
HystrixService hystrixService;
@GetMapping("/hi")
public String hi(String id){
return hystrixService.hi(id);
}
}
1.3测试
关闭服务提供者,或者路径错误,即访问失败
.
2.在Feign中使用Hystrix熔断
Feign自带熔断功能,默认情况下,熔断功能是关闭的。如果要开启熔断,
只需在配置文件中将hystrix. enabled设置为true即可。
具体步骤如下:
2.1 开启Hystrix熔断功能。
在eureka-hystrix-client项目的配置文
件application. yml中添加开启熔断的配置,具体如下:
feign:
hystrix:
enabled:
true
注意:因为在Feign的起步依赖中引入了Hystrix 依赖,所以在Feign中使用
Hystrix不需要引入任何的依赖,只需要在配置文件中开启即可。
2.2 在消费者eureka-hystrix-client启动类中添加@EnableFeignClients
2.3 在消费者eureka-hystrix-client的service包中创建一个FeignHystrixService接口
package com.li.eurekahystrixclient.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Service
//fallback属性指定的用于回退逻辑的类,当访问出现异常,则找到FeignHystrixServiceImpl
@FeignClient(name = "hystrix-provider",fallback = FeignHystrixServiceImpl.class)
public interface FeignHystrixService {
@GetMapping("/hi")
public String hi(@RequestParam("id") String id);
}
2.4 在消费者eureka-hystrix-client的service包下创建impl.FeignHystrixServiceImpl类
package com.li.eurekahystrixclient.service.impl;
import com.li.eurekahystrixclient.service.FeignHystrixService;
import org.springframework.stereotype.Component;
@Component
public class FeignHystrixServiceImpl implements FeignHystrixService {
@Override
public String hi(String id) {
return "当前服务被降级,id为"+id;
}
}
2.5 在消费者eureka-hystrix-client修改controller的HystrixController类
package com.li.eurekahystrixclient.controller;
import com.li.eurekahystrixclient.service.FeignHystrixService;
import com.li.eurekahystrixclient.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HystrixController {
@Autowired
HystrixService hystrixService;
@Autowired
FeignHystrixService feignHystrixService;
@GetMapping("/hi")
public String hi(String id){
return hystrixService.hi(id);
}
//新增
@GetMapping("/hi/feign")
public String hiFeign(String id){ return feignHystrixService.hi(id);}
}
2.6 测试
访问异常:
3.Hystrix整个工作流如下:
1.构造一个HystrixCommand或HystrixObservableCommand对象, 用于封装请求
2.执行命令,Hystrix提供了4种执行命令的方法
3. 判断是否使用缓存响应请求,若启用了缓存,且缓存可用,直接使用缓存响应请求。Hystrix支持请求缓存, 但需要用户自定义启动;
4.判断熔断器是否打开,如果打开,跳到第8步;
5.判断线程池/队列/信号量是否已满,已满则跳到第8步:
6.执行HystrixobservableCommand. construct ()或HystrixCommand.run(), 如果执行失败或者超时,跳到第8步;否则,跳到第9步:
7.统计熔断器监控指标;
8.走Fallback备用逻辑
9.返回请求响应
4.使用Hystrix Dashboard监控熔断器状态
微服务架构中,为了保证服务的可用性,防止某个服务出现故障导致线程阻塞,设计出了Hystrix熔断器,因此熔断器成为一个反应程序健康性的重要指标,Dashboard是Hystrix熔断器健康状态是一个重要组件,它提供了数据监控和友好的图形化界面,操作如下:
4.1 在已有的eureka-hystrix-client项目中添加spring-cloud-starter-netflix-hystrix-dashboard 和 spring-boot-starter-actuator依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.2在eureka-hystrix-client项目启动类添加@EnableHystrixDashboard
4.3 创建hystrix.stream的servlet配置
不同的spring boot版本 开启Dashboard的方式有所不同,需要添加HystrixMetricsStreamServlet的配置, 在config包中创建HystrixDashboardConfiguration类
package com.li.eurekahystrixclient.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HystrixDashBoardConfiguration {
@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;
}
}
4.4启动测试,访问localhost:8764/hystrix
输入:http:localhost:8764/hystrix.stream
打开一个新的浏览器窗口,访问http://localhost:8764/hi?id=10
1.标注的是服务的健康程度。实心圆的颜色按照绿黄橙红的顺序依次递减,表示服务的健康程度依次降低,圆越大,表示服务的流量越大。
2.数字从上往下依次是服务消费者请求调用者的请求成功数,短路,熔断和失败的次数。
3.数字从上往下依次是服务消费者请求调用者的超时数,线程池拒绝数和请求异常数。
4.表示集群下的主机报告
5.表示服务消费者请求调用者的请求频率
.
5.使用Hystrix 和Turbine进行聚合监控
在使用Hystrix Dashboard组件监控服务的熔断器状态时,每个服务都有一个Hystrix Dashboard主页,服务数量很多时,监控非常不方便。为了同时监控多个服务的熔断器的状况,就需要用到 Turbine。Turbine将每个服务的Hystrix Dashboard 数据进行了整合,顾名思义是用于集合多个HystrixDashboard 组件的数据放在同一个页面上展示,从而做到集中监控。
5.1 搭建新消费者和提供者
在搭建了hystrix-provider和eureka-hystrix-client的情况下再搭建hystrix-provider1 7007和eureka-hystrix-client1 8765的提供者和消费者
5.2 搭建turibine-server的Spring-Boot项目
5.2.1 添加eureka-client,web ,test,turibine ,dashboard依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.li</groupId>
<artifactId>turbine-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>turbine-server</name>
<description>turbine-server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--父依赖相关信息-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
5.2.2 配置文件
spring:
application:
name: turbine-server
server:
port: 8769
turbine:
#用于设置路径
instanceUrlSuffix: /hystrix.stream
aggregator:
#指定聚合的集群
cluster-config: default
#用于配置需要监控的服务
app-config: eureka-hystrix-client,,eureka-hystrix-client1
#指定集群名称
cluster-name-expression: new String("default")
eureka:
client:
service-url:
defaultZone: http://localhost:7000/eureka/
5.2.3 在启动类添加@EurekaTurbine @EnableHystrixDashboard
5.2.4 测试
依次启动eureka-server, hystrix-provider, hystrix-provider1,eureka-hystrix-client,eureka-hystrix-client1,turbine-server
访问http:localhost:8769/hystrix
输入http://localhost:8769/turbine.stream
点击 “Monitor Stream” 进入监控界面