Hystrix断路器 (豪猪)-保险丝

news/2025/2/12 4:04:53/

一、概述

重点:能让服务的调用方,够快的知道被调方挂了!不至于说让用户在等待。
Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。
雪崩:一个服务失败,导致整条链路的服务都失败的情形。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Hystix 主要功能

在这里插入图片描述

二、隔离

2.1 线程池隔离

没有hystrix,a重试100次,才知道c挂了!

在这里插入图片描述

使用了hystrix,更细分线程池,只需要重试40次,让a更快的知道c挂了

在这里插入图片描述

2.2 信号量隔离

没有hystrix,a一个带着认证信息的线程,重试100次,才知道c挂了!

在这里插入图片描述

使用了hystrix,更细分线程池,一个带着认证信息的线程,只需要重试40次,让a更快的知道c挂了

在这里插入图片描述

三、服务降级

3.1 服务提供方降级(异常,超时)

在这里插入图片描述
a、在服务提供方,引入 hystrix 依赖

<!-- hystrix -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

b、方法

/** 定义降级方法   返回特殊对象*  1方法的返回值要和原方法一致*  2方法参数和原方法一样*/public Goods findById_fallback(Integer id){Goods goods=new Goods();goods.setGoodId(-1);goods.setTitle("provider提供方降级!");goods.setPrice(-9.9);goods.setStock(-10);return goods;}

c、使用 @HystrixCommand 注解配置降级方法

    @GetMapping("/findById/{id}")@HystrixCommand(fallbackMethod = "findById_fallback",commandProperties = {//设置Hystrix的超时时间,默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")})public Goods findById(@PathVariable("id") Integer id){Goods goods = goodsService.findById(id);goods.setTitle(goods.getTitle()+"|端口号:"+port);//模拟出异常
//        int a=1/0;//模拟业务逻辑比较繁忙try {Thread.sleep(5000);} catch (InterruptedException e) {e.printStackTrace();}return goods;}

d、在启动类上开启Hystrix功能:@EnableCircuitBreaker

测试:http://localhost:9000/order/add/10

1.出错,服务方降级了

在这里插入图片描述

2.3000超时,服务方降级了

在这里插入图片描述

3.2 消费方降级

在这里插入图片描述

a、feign 组件已经集成了 hystrix 组件

在这里插入图片描述

b、定义feign 调用接口实现类,复写方法,即 降级方法

GoodsFeignClientFallback

package com.liming.consumer.feign;import com.liming.consumer.domain.Goods;
import org.springframework.stereotype.Component;@Component
public class GoodsFeignCallback implements GoodsFeign{@Overridepublic Goods findById(Integer id) {Goods goods=new Goods();goods.setGoodId(-2);goods.setTitle("调用方降级了!");goods.setPrice(-5.5);goods.setStock(-5);return goods;}
}

c、在 @FeignClient 注解中使用 fallback 属性设置降级处理类

GoodsFeignClient

package com.liming.consumer.feign;import com.liming.consumer.config.FeignLogConfig;
import com.liming.consumer.domain.Goods;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "EUREKA-PROVIDER",configuration = FeignLogConfig.class,fallback = GoodsFeignCallback.class)
public interface GoodsFeign {@GetMapping("/goods/findById/{id}")public Goods findById(@PathVariable("id") Integer id);
}

d、配置开启

# 开启feign对hystrix的支持
feign:hystrix:enabled: true

测试:停掉provider

http://localhost:9000/order/add/10

在这里插入图片描述

四、熔断

provider

package com.liming.proviver.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.liming.proviver.domain.Goods;
import com.liming.proviver.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/goods")
public class GoodsController {@AutowiredGoodsService goodsService;@Value("${server.port}")int port;@GetMapping("/findById/{id}")@HystrixCommand(fallbackMethod = "findById_fallback",commandProperties = {//设置Hystrix的超时时间,默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")})public Goods findById(@PathVariable("id") Integer id){Goods goods = goodsService.findById(id);goods.setTitle(goods.getTitle()+"|端口号:"+port);if(id==1){//模拟出异常int a=1/0;}//模拟出异常
//        int a=1/0;//模拟业务逻辑比较繁忙
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }return goods;}/** 定义降级方法   返回特殊对象*  1方法的返回值要和原方法一致*  2方法参数和原方法一样*/public Goods findById_fallback(Integer id){Goods goods=new Goods();goods.setGoodId(-1);goods.setTitle("provider提供方降级!");goods.setPrice(-9.9);goods.setStock(-10);return goods;}
}

访问两个接口

1、http://localhost:9000/order/add/10

在这里插入图片描述

2、多次访问 http://localhost:9000/order/add/1

在这里插入图片描述

3、导致10也不能访问了

在这里插入图片描述

4.再过一会儿,半开状态

http://localhost:9000/order/add/10

在这里插入图片描述

Hyst rix 熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。

circuitBreaker.sleepWindowInMilliseconds:监控时间
circuitBreaker.requestVolumeThreshold:失败次数
circuitBreaker.errorThresholdPercentage:失败率

提供者controller中

    @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {//设置Hystrix的超时时间,默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),//监控时间 默认5000 毫秒@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"),//失败次数。默认20次@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"),//失败率 默认50%@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")})

五、熔断监控-运维

Hystrix 提供了 Hystrix-dashboard 功能,用于实时监控微服务运行状态。
但是Hystrix-dashboard只能监控一个微服务。
Netflix 还提供了 Turbine ,进行聚合监控。

在这里插入图片描述

5.1 Turbine聚合监控

a、 创建监控模块

创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix dashboard功能

b、引入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>spring-cloud-parent</artifactId><groupId>com.liming</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>hystrix-monitor</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></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><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

c、修改application.yml

spring:application:name: hystrix-monitor
server:port: 8769
turbine:combine-host-port: true# 配置需要监控的服务名称列表app-config: EUREKA-PROVIDER,EUREKA-CONSUMERcluster-name-expression: "'default'"aggregator:cluster-config: default#instanceUrlSuffix: /actuator/hystrix.stream
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
hystrix:dashboard:proxy-stream-allow-list: "*"

d、创建启动类

package com.liming.monitor;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);}
}

5.2 2、修改被监控模块

需要分别修改 hystrix-provider 和 hystrix-consumer 模块:

a、导入依赖:

<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>

b、配置Bean

此处为了方便,将其配置在启动类中

@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;
}

c、启动类上添加注解@EnableHystrixDashboard

@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients 
@EnableHystrixDashboard // 开启Hystrix仪表盘监控功能
public class ConsumerApp {public static void main(String[] args) {SpringApplication.run(ConsumerApp.class,args);}@Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/actuator/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;}
}

5.3 启动测试

a、启动服务:

  • eureka-server

  • hystrix-provider

  • hystrix-consumer

  • hystrix-monitor

b、访问:

在浏览器访问http://localhost:8769/hystrix/ 进入Hystrix Dashboard界面

在这里插入图片描述

界面中输入监控的Url地址 http://localhost:8769/turbine.stream,监控时间间隔2000毫秒和title,如下图

在这里插入图片描述

  • 实心圆:它有颜色和大小之分,分别代表实例的监控程度和流量大小。如上图所示,它的健康度从绿色、黄色、橙色、红色递减。通过该实心圆的展示,我们就可以在大量的实例中快速的发现故障实例和高压力实例。
  • 曲线:用来记录 2 分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。

http://www.ppmy.cn/news/58540.html

相关文章

MATLAB程序员投简历的技巧解析,如何写出有亮点的简历

如果你想在简历中展示你的项目经验&#xff0c;一定要有亮点。一个导出的 Excel 文件过大导致浏览器卡顿的例子就是一个很好的亮点。你可以在简历中写明这个例子。如果面试官问起&#xff0c;可以用浏览器的原理来解释。浏览器内核可以简单地分为以下 5 个线程&#xff1a;GUI …

知识管理在企业中的重要性

随着经济全球化和信息化的快速发展&#xff0c;企业面临着越来越多的竞争和挑战。如何把握市场动态、满足客户需求、提高产品质量和效率等&#xff0c;成为了企业发展中亟待解决的问题。而知识管理作为一种新兴的管理方式&#xff0c;逐渐引起了企业们的重视。本文将从以下几个…

风控系统就该这么设计(万能通用),那是相当稳定

一、背景 1.为什么要做风控? 2.为什么要自己写风控? 3.其它要求 二、思路 1.风控规则的实现 2.调用方式的实现 三、具体实现 1.风控计数规则实现 2.注解的实现 四、测试一下 1.写法 2.Debug看看 一、背景 1.为什么要做风控? 这不得拜产品大佬所赐&#xff0c;我们…

【华为OD机试真题】Linux发行版的数量(javaC++python) 100%通过率 超详细代码注释

Linux发行版的数量 知识点DFS搜索BFS搜索并查集 时间限制:1s 空间限制:256MB 限定语言:不限题目描述 Linux操作系统有多个发行版,distrowatch.com提供了各个发行版的资料。这些发行版互相存在关联,例如Ubuntu基于Debian开发,而Mint又基于Ubuntu开发,那么我们认为Mint同Deb…

Mysql 分库分表 Mycat

0 课程视频 https://www.bilibili.com/video/BV1Kr4y1i7ru?p163&spm_id_frompageDriver&vd_sourceff8b7f852278821525f11666b36f180a 1 单库问题 1.1 热点数据多 -> 缓冲区不足 ->内存不足 1.2 数据多 -> 磁盘不足 1.3 请求数据量多 -> 带宽不足 1…

CF1060E Sergey and Subway

CF1060E Sergey and Subway 树上计数dp&#xff0c;考虑每条边的贡献&#xff0c;树上两点距离用深度与LCA表示 长度为2的两点间可以连一条边&#xff0c;所以对于任意两点 i , j i,j i,j&#xff0c; d i s 2 i , j ⌈ d i s i , j / 2 ⌉ ( d i s i , j ( d i s i , j %…

while语句和until语句顺便带点小实验

while语句和until语句 一、while用法二、Until循环语句三、趣味小实验猜价格的游戏&#xff08;价格是随机数&#xff09;写一个计算器脚本闲来无事去购物 一、while用法 for循环语句非常适用于列表对象无规律&#xff0c;且列表来源以固定&#xff08;如某个列表文件&#xf…

【SpringBoot2】三:基础入门---自动配置原理(自动配置原理入门+开发技巧)

文章目录 1.自动配置原理入门1.1 引导加载自动配置类1.2 按需开启自动配置项1.3 修改默认配置1.4 最佳实践 2.开发小技巧2.1 Lombok2.1.1 简化Bean开发2.1.2 简化日志开发 2.2 dev-tools2.3 Spring Initailizr&#xff08;项目初始化向导&#xff09; 1.自动配置原理入门 1.1 …