Hystrix 概述
- Hystrix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务。第三方库,防止出现级联失败(雪崩)
- 雪崩:一个服务失败。导致整条链路的服务都失败的情形。
- Hystrix主要功能
- 隔离
- 线程池隔离
- 信号量隔离
- 降级: 异常 超时
- 熔断
- 限流
Hystrix 降级
- Hystrix降级:当服务发生异常或调用超时,返回默认数据
Hystrix降级 - 服务提供方
- 在服务提供方,引入Hystrix依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.定义降级方法
/**
*定义降级方法:
*1.方法的返回值需要和原方法一样
*2.方法的参数需要和原方法一样
*/
public Goods findOne_fallback(int id){
Goods goods = new Goods();
goods.setTitle("降级了....");
return goods;
}
3.使用@HystrixCommand
注解配置降级方法
/**
*降级:
*1.出现异常
*2.服务调用超时
* *默认1s超时
*/
@HystrixCommand(fallbackMethod = "findOne_fallback", commandProperties = {
//设置Hystrix的超时时间,默认1s
@HystruxProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
}) //fallbackMethod 指定降级后调用的方法
4.在启动类上开启Hystrix功能:@EnableCircuitBreaker
Hystrix降级 - 服务消费方
- fegin组件已经集成了hystrix组件。
- 定义fegin调用接口实现类,复写方法,即降级方法
package com.itheima.consumer.feign;
import com.itheima.consumer.domain.Goods;
import org.springframework.stereotype.Component;
/**
* Feign 客户端的降级处理类
* 1.定义类实现 Feign 客户端接口
* 2.使用@Component注解将该类的Bean加入Spring的IOC容器
*/
@Component
public class GoodsFeignClientFallback implements GoodsFeignClient {
@Override
public Goods findGoodsById(int id) {
Goods goods = new Goods();
goods.setTitle("又被降级了。。。");
return goods;
}
}
- 在
@FeignClient
注解中使用fallback属性设置降级处理类
@FeginClient(value = "FEIGN_PROVIDER", fallback = GoodsFeignClientFallback.class)
- yml配置开启
feign.hystrix.enabled = true
默认是关着的
Hystrix 熔断
- Hystrix熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阀值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。
三种状态 : 打开,关闭, 半开
熔断-1.png
- 设置参数
@HystrixCommand(fallbackMethod = "findOne_fallback", commandProperties = {
//设置Hystrix的超时时间,默认1s
@HystruxProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
//监控时间 默认5000毫秒
@HystruxProperty(name = "circuitBreaker.sleepWindowlnMilliseconds", value = "5000"),
//失败次数 默认20次
@HystruxProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
//失败率 默认50%
@HystruxProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
})
Hystrix 熔断监控
-
Hystrix提供了 Hystrix-dashboard功能,用于实时监控微服务运行状态。
熔断监控-1.png
- 但是Hystrix-dashboard只能监控一个微服务。
- Netflix还提供了Turbine,进行聚合监控。
一. 搭建监控模块
1. 创建监控模块
创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix-dashboard功能。
2. 引入Turbine聚合监控起步依赖
<?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>hystrix-parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hystrix-monitor</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. yml配置
spring:
application:
name: hystrix-monitor
server:
port: 8769
turbine:
combine-host-port: true
app-config: hystrix-provider,hystrix-consumer #配置需要被监控的服务名称列表
aggregator:
cluster-config: default #instanceUrlSuffix: /actuator/hystrix.stream
cluster-name-expression: "default"
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
4. 添加启动类
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableEurekaClient
@EnableTurbine //开启Turbine 聚合监控功能
@EnableHystrixDashboard //开启HyStrix仪表盘监控功能
public class HystrixMonitorApp {
public static void main(String[] args) {
SpringApplication.run(HystrixMonitorApp.class, args);
}
}
二. 修改被监控模块
需要分别修改hystrix-provider和provider-consumer模块:
1. 导入依赖:
<!--turnbin监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-hystrix-dashboard</artifactId>
</dependency>
3. 配置Bean
此处为了方便,将其配置在启动类里 添加注解
@EnableHystrixDashboard
ackage com.itheima.provider;
import com.netflix.hystrix.HystrixMetrics;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
/**
* 启动类
*/
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker //开启Hystrix功能
@EnableHystrixDashboard //开启Hystrix仪表盘监控功能
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class, args);
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
启动项目访问http://ip:8769/hystrix/ 进入Hystrix Dashboard界面
![](https://img.haomeiwen.com/i6573541/4c8ccdc1d807fef8.png)
![](https://img.haomeiwen.com/i6573541/2a5305038e6b9af6.png)
网友评论