源码: https://gitee.com/GXQ205153964/Hystrix-parent.git

Hystrix概述:

  • hystrix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)
  • 级联失败: 当C服务出现问题(硬盘烧了,网络断了)不可用了,B依旧会不停的调用C,B的资源耗尽,导致B也出现问题,同理A。这种现象被称为雪崩。

预防雪崩

Hystix主要功能:

问题:

这里有一个线程池对应3个服务,当哪个服务被调用时,线程池会分配一个线程运行。当C发生故障后线程池的线程依旧会执行C服务,从而导致线程池线程用完,从而导致BD服务也无法使用。

解决:

  • 隔离:

线程池隔离:把A分成3个线程池分别对应三个服务,C坏了后,BD不会收到影响。

信号量隔离:对BCD加一个阀门,只允许一定的数量的人经行访问,超过就不能访问了,从而阻止C将所有的线程池资源耗尽。

  • 降级 (异常,超时等)自己写个降级方案,在服务调用方和提供方都要写,网络不通时。降级就是执行自己B计划,预案。
  • 熔断   当C服务出现错误频率较高时,Hystrix直接将C的所有服务全部拒绝,C修复后,熔断器会自动将C恢复成原来样子,正常运行。熔断机制,用于监控微服务调用情况,当失败的情况打到预定的阀值(5秒失败20次),会打开短路器,拒绝所有请求,知道服务回复正常为止。
  • 限流   限制多少人/服务访问

降级:

降级——服务提供方

(c降级方案)

流程:

  1. 在服务提供方,引入hystrix依赖
  2. 定义降级方法
  3. 使用@HystrixCommand注解配置降级方法
  4. 在启动类上开启Hystrix功能:@EnableCircuitBreker

基础代码构建

SpringCloud EurekeServer Consumer和provider

C降级方案搭建

 provider:

pom.xml

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

ProviderApp启动类(开启熔断器)

//启动类
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker//开启Hystrix熔断器
public class ProviderApp {public static void main(String[] args){SpringApplication.run(ProviderApp.class,args);}
}

GoodsController

package com.gao.controller;import com.gao.domain.Goods;
import com.gao.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
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 {@Autowiredprivate GoodsService goodsService;/**///这里注入port可时刻监控哪个服务被调用@Value("${server.port}")private int port;/***降级:* 出现异常* 服务调用超时*/@GetMapping("/findOne/{id}")//指定降级后调用的方法@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties ={//修改几秒后超时@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000")})public Goods findOne(@PathVariable("id") int id){Goods goods = goodsService.findOne(id);//造个异常//int i =3/0;//超时try{//休眠两秒Thread.sleep(2000);}catch (InterruptedException e){e.printStackTrace();}//将端口号,设置到了商品标题上方便监控goods.setTitle(goods.getTitle()+"  "+"port" + ":" + port);return goods;}/*** 定义降级方法*方法的返回值需要和原方法一样* 方法的参数需要和原方法一样*/public Goods findOne_fallback(int id){Goods goods = new Goods();goods.setTitle("降级了~~~");return goods;}
}

降级——Hystrix降级-服务提供方

  1. 在服务提供方,引入hystrix依赖(consumer使用了feign,而feign里已经集成了Hystrix,不用再引入feign依赖了,如果没有过使用feign,按照provider写
  2. 定义降级方法
  3. 使用@HystrixCommand注解配置降级方法
  4. 在启动类上开启Hystrix功能:@EnableCircuitBreaker

consumer使用了feign,而feign里已经集成了Hystrix,不用再引入feign依赖了

A级降级方案搭建

开启feign对hystrix的支持

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

pom.xml

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- eureka-server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--feign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency></dependencies>

GoodsFeignClient

package com.gao.feign;@FeignClient(value = "HYSTRIX-PROVIDER",fallback = GoodsFeignClientFallback.class)
public interface GoodsFeignClient {@GetMapping("/goods/findOne/{id}")public Goods findGoodById(@PathVariable("id") int id);
}

GoodsFeignClientFallback

package com.gao.feign;@Component
public class GoodsFeignClientFallback implements GoodsFeignClient {@Overridepublic Goods findGoodById(int id) {Goods goods = new Goods();goods.setTitle("又被降级了~~~");return goods;}
}

启动类上加

@EnableFeignClients//启动feign

测试结果

当服务C坏掉后,启动了降级方案,返回给A是好的值,所以A不会启动降级方案。在A和C之间不能通信时(例如断网,或者直接关掉C),A会启动降级方案

熔断

在provider里的GoodsController里添加

 当id=1时,出现异常id=2时正常       //熔断//如果id==1,则出现异常,id !=1 则正常访问if(id == 1){//造个异常int i =3/0;}

启动ProviderApp,5秒失败20次,刷新,当达到这个情况下,当id变为2时也会降级。说明启动了熔断,等五秒后会自动启动,id=2时又可以用了。

package com.gao.controller;import com.gao.domain.Goods;
import com.gao.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
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 {@Autowiredprivate GoodsService goodsService;/**///这里注入port可时刻监控哪个服务被调用@Value("${server.port}")private int port;/***降级:* 出现异常* 服务调用超时*/@GetMapping("/findOne/{id}")//指定降级后调用的方法@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties ={//修改几秒后超时@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")})public Goods findOne(@PathVariable("id") int id){Goods goods = goodsService.findOne(id);//熔断//如果id==1,则出现异常,id !=1 则正常访问if(id == 1){//造个异常int i =3/0;}//降级//造个异常int i =3/0;//超时/*try{//休眠两秒Thread.sleep(2000);}catch (InterruptedException e){e.printStackTrace();}*///将端口号,设置到了商品标题上方便监控goods.setTitle(goods.getTitle()+"  "+"port" + ":" + port);return goods;}/*** 定义降级方法*方法的返回值需要和原方法一样* 方法的参数需要和原方法一样*/public Goods findOne_fallback(int id){Goods goods = new Goods();goods.setTitle("降级了~~~");return goods;}
}

修改熔断基础属性

//监控时间 默认5000 毫秒 @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "5000"), //失败次数。默认20次 @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "20"), //失败率 默认50% @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "50")

provider GoodsController

package com.gao.controller;//服务提供方
@RestController
@RequestMapping("/goods")
public class GoodsController {@Autowiredprivate GoodsService goodsService;/**///这里注入port可时刻监控哪个服务被调用@Value("${server.port}")private int port;/***降级:* 出现异常* 服务调用超时*/@GetMapping("/findOne/{id}")//指定降级后调用的方法@HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties ={//修改几秒后超时@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")})public Goods findOne(@PathVariable("id") int id){Goods goods = goodsService.findOne(id);//熔断//如果id==1,则出现异常,id !=1 则正常访问if(id == 1){//造个异常int i =3/0;}//降级//造个异常int i =3/0;//超时/*try{//休眠两秒Thread.sleep(2000);}catch (InterruptedException e){e.printStackTrace();}*///将端口号,设置到了商品标题上方便监控goods.setTitle(goods.getTitle()+"  "+"port" + ":" + port);return goods;}/*** 定义降级方法*方法的返回值需要和原方法一样* 方法的参数需要和原方法一样*/public Goods findOne_fallback(int id){Goods goods = new Goods();goods.setTitle("降级了~~~");return goods;}
}

熔断监控

简介

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

搭建:

搭建监控模块:

创建监控模块:

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

引入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.gao</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><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

application.yml

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

启动类:

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和hystrix-consumer模块

添加依赖

<!--turbine 监控--><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>

启动类里配置Bean再添加启动注解

//启动类
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker//开启Hystrix熔断器
@EnableHystrixDashboard//开启Hystrix仪表盘监控功能
public class ProviderApp {public static void main(String[] args){SpringApplication.run(ProviderApp.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;}
}

consumer OrderController这里注入的goodsFeignClient会报红,可以忽略。

//服务的调用方
@RestController
@RequestMapping("/order")
public class OrderController {@Autowiredprivate GoodsFeignClient goodsFeignClient;@GetMapping("/goods/{id}")public Goods findGoodsById(@PathVariable("id") int id){//3.调用方法//Goods goods = restTemplate.getForObject(url,Goods.class);Goods goods = goodsFeignClient.findGoodById(id);return goods;}
}
  • 测试结果:

  • 分别访问:以下url显示下面情况说明监控到了对应服务下的数据
  • localhost:8000/actuator/hystrix.stream
  • localhost:9000/actuator/hystrix.stream
  • localhost:8769/turbine.stream

web界面输入Url地址:

http://localhost:8769/turbine.stream                监控所有监管的服务

http://localhost:9000/actuator/hystrix.stream  监控单个服务consumer

http://localhost:8000/actuator/hystrix.stream  监控provider

图像注释:

实心圆:他有颜色大小区分,分别代表实例的监控程序和流量大小。他从上图所示健康度从绿色 黄色 橙色 红色递减。通过实心球的展示我们可以在大量的实例中快速的发现故障实例和高压力实例

曲线: 用来记录两分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。

hystrix 熔断器相关推荐

  1. Spring Cloud 之 Ribbon,Spring RestTemplate 调用服务使用Hystrix熔断器

    在服务调用中增加Hystrix熔断器,是在单个服务出现故障的时候快速失败,也可以提供回退方案,避免因为线程等待耗尽整个服务的线程资源:Hystrix DashBoard监控组件,可以实时监控熔断器的状 ...

  2. Spring cloud微服务 Hystrix熔断器

    以下demo代码:https://github.com/wades2/HystrixtDemo 官网定义:Hystrix是一个延迟容错库.在分布式环境中,许多服务依赖项中的一些不可避免地会失败.Hys ...

  3. SpringCloud Hystrix熔断器

    SpringCloud Hystrix熔断器 15.Hystrix熔断器:简介及作用 目标:理解Hystrix的作用 介绍:Hystrix,英文意思是豪猪,全身是刺,看起来就不好惹,是一种保护机制. ...

  4. Hystrix熔断器的理解

    Hystrix熔断器的理解 什么是熔断器 为什么要使用熔断器 熔断器工作原理![在这里插入图片描述](https://img-blog.csdnimg.cn/20200823210834884.png ...

  5. springCloud 之 Hystrix 熔断器

    Hystrix 熔断器属于⼀种容错机制 微服务中的雪崩效应 什么是微服务中的雪崩效应呢? 微服务中,⼀个请求可能需要多个微服务接⼝才能实现,会形成复杂的调⽤链路. 扇⼊:代表着该微服务被调⽤的次数,扇 ...

  6. hystrix threadpool coresize_SpringCloud 基础教程(八)-Hystrix熔断器(上)

    我的博客:兰陵笑笑生,欢迎浏览博客! 关注公众号:"程序员笑笑生", 回复 "Spring Cloud"."Spring Boot" 获取 ...

  7. 一文带你了解Ribbon负载均衡和Hystrix熔断器

    文章目录 1. Ribbon负载均衡 1.1 关于负载均衡 1.2 Ribbon高级应用 1.3 Ribbon负载均衡策略 1.4 Ribbon核心源码剖析 2. Hystrix熔断器 2.1 微服务 ...

  8. Ribbon 负载均衡 和 Hystrix熔断器

    1 Ribbon负载均衡 1.1 关于负载均衡 负载均衡一般分为服务器端负载均衡和客户端负载均衡 所谓服务器端负载均衡,比如Nginx.F5这些,请求到达服务器之后由这些负载均衡器根据一定的算法将请求 ...

  9. Spring Cloud 集成 Hystrix 熔断器

    文章目录 Hystrix 熔断器 Hystrix 概述 Hystrix 降级 服务提供方 步骤 pom.xml(依赖) GoodsController.java(包含降级方法) 启动类(@Enable ...

最新文章

  1. 北大女生拿下阿里数学预赛第一名!决赛入围率不到1%,最小晋级选手只有14岁...
  2. spark任务优先级设置:spark.yarn.priority
  3. Socket 多线程FTP软件开发
  4. 面试官留步!听我跟你侃会儿Docker原理
  5. OpenShift 4 - Pod的亲和性/反亲和性
  6. 如何在传统 ASP 和 ASP.NET 之间共享会话状态
  7. 动态代理 aop切面实现事务管理
  8. RubyOnRails杂记
  9. Win7-64位系统安装vs2003
  10. 总体设计(五个基本原理、软件结构图)
  11. 学习笔记-MATLAB函数调用关系查看
  12. ubuntu中firfox插件xmarks的同步问题
  13. 英语日常短语积累(1)
  14. float a=1.0f 中的“f”代表什么
  15. 即时消息服务器eChat 与 通信解决方案
  16. 电子计算机行业爆品打造,二类电商|爆品打造,不是你想造就能造
  17. python:Base16, Base32, Base64, Base85 数据编码
  18. 沟通新法则:请不要一个人吃饭
  19. resnet50特征图分辨率和通道数的变化
  20. babylonjs使用笔记

热门文章

  1. UnityHLSLShader 函数笔记
  2. 向Gitlab项目工程提交代码时出现无法推送的问题
  3. 怀揣坚定与执着,踏实稳步向前
  4. linux运行python
  5. 嵌入式设备启动过程(ARM )
  6. 荣耀手表es鸿蒙,荣耀手表ES评测:方形大屏+轻盈机身,599元腕上健康全能管家...
  7. JavaScript(JS) string.italics( )
  8. 易语言 图片插入超级列表框_科技资讯:在PPT文档中如何将一张图片做成九宫格效果...
  9. 这些java基础编程练习题你都会了吗?
  10. 维克森林大学计算机科学专业好不好,维克森林大学商业分析硕士怎么样?