一、在分布式系统中,服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务会出现故障,导致依赖于它们的其他服务出现远程调度的线程阻塞 Hystrix是Netflix 公司开源的一个项目,它提供了熔断器功能,能够阻止分布式系统中出现联动故障 Hystrix 是通过隔离服务的访问点阻止联动故障的,并提供了故障的解决方案,从而提高了整个分布式系统的弹性

  二、Hystrix的产生:

  在复杂的分布式系统中,可能有几十个服务相互依赖,这些服务由于某些原因,例如机房的不可靠性、网络服务商的不可靠性,导致某个服务不可用。如果系统不隔离该不可用的服务,可能会导致整个系统不可用。

  对于依赖 30 个服务的应用程序,每个服务的正常运行时间为 99.99% ,对于单个服务来说, 99.99% 的可用是非常完美的。有99.99^30 = 99.7% 的可正常运行时间和 0.3% 的不可用时间,那么 10 亿次请求中有 3000000次失败,实际的情况可能比这更糟糕。

  如果不设计整个系统的韧性,即使所有依赖关系表现良好,单个服务只有 0.01% 的不可用,由于整个系统的服务相互依赖,最终对整个系统的影响是非常大的。

  

  在上图中一个用户需要请求A、H、I、P。如果中间I服务网络延迟或者发生故障,即A、H、P不可用。在高并发的情况下,单个服务的延迟会导致整个请求都处于延迟状态,可能在几秒钟就使整个服务处于线程负载饱和的状态。

  某个服务的单个点的请求故障会导致用户的请求处于阻塞状态,最终的结果就是整个服务的线程资源消耗殆尽。由于服务的依赖性,会导致依赖于该故障服务的其他服务也处于线程阻塞状态,最终导致这些服务的线程资源消耗殆尽 直到不可用,从而导致整个问服务系统都不可用,即雪崩效应。

  为了防止雪崩效应,因而产生了熔断器模型。 Hystrix 是在业界表现非常好的 个熔断器模型实现的开源组件,它是 Spring Cloud 组件不可缺少的一部分。

  三、Hystrix的设计原则:

  1)防止单个服务的故障耗尽整个服务的 Servlet 容器(例如 Tomcat )的线程资源。

  2)快速失败机制,如果某个服务出现了故障,则调用该服务的请求快速失败,而不是线程等待。

  3)提供回退( fallback )方案,在请求发生故障时,提供设定好的回退方案。

  4)使用熔断机制,防止故障扩散到其他服务。

  5)提供熔断器的监控组件 Hystrix Dashboard ,可以实时监控熔断器的状态。

  四、Hystrix的工作机制:

  

  首先,当服务的某个 API 接口的失败次数在一定时间内小于设定的阀值时,熔断器处于关闭状态,该 API 接口正常提供服务 。当该API 接口处理请求的失败次数大于设定的阀值时, Hystrix 判定该 API 接口出现了故障,打开熔断器,这时请求该 API 接口会执行快速失败的逻辑(即 fall back 回退的逻辑),不执行业务逻辑,请求的线程不会处于阻塞状态。处于打开状态的熔断器,一段时间后会处于半打开状态,并将一定数量的请求执行正常逻辑。剩余的请求会执行快速失败,若执行正常逻辑的请求失败了,则熔断器继续打开。若成功了 ,则将熔断器关闭。这样熔断器就具有了自我修复的能力

  五、服务中对Hystrix的基本使用

  1)Ribbon中使用Hystrix熔断器(我们基于前面的Spring-Cloud之Ribbon负载均衡-3代码进行改造)。

  a、加入依赖

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

  b、加入注解(@EnableHystrix)开启熔断器功能

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class HystrixRibbonApplication { public static void main(String[] args) {
SpringApplication.run(HystrixRibbonApplication.class, args);
}
}

  c、修改RibbonServiceImpl.class的实现,在getPort方法上面加入@HystrixCommand注解,这样这个接口就有了熔断器功能了,其中@HystrixCommand中的fallbackMethod为执行快速失败的方法。

package com.cetc.service.impl;

import com.cetc.service.IRibbonService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; @Service
public class RibbonServiceImpl implements IRibbonService { @Autowired
private RestTemplate restTemplate; @Override
@HystrixCommand(fallbackMethod = "error")
public Integer getPort() {
return restTemplate.getForObject("http://client/api/test/getPort", Integer.class);
} public Integer error() {
return 0
;
}

}

  d、测试。启动Eureka-Server、Eureka-Client、Hystrix-Ribbon端口分别为8670、8673、8677。

  

  正常访问时:

  

  服务异常时:

  

  2)Feign中使用Hystrix(代码基于前面的Spring-Cloud之Feign声明式调用-4进行调整)

  a、因为Feign中自带了Hystrix功能,所以不需要重新加入依赖。

  b、在application.yaml中开启Hystrix的配置:

feign:
hystrix:
enabled: true

  c、编写TestHystrix服务错误的执行方式,继承Feign接口

package com.cetc.feign.hystrix;

import com.cetc.feign.client.TestFeign;
import org.springframework.stereotype.Component; @Component
public class TestHystrix implements TestFeign{ @Override
public Integer getPort() {
return 0;
}
}

  d、在@FeignClient中加入回调的class。

package com.cetc.feign.client;

import com.cetc.config.FeignConfiguration;
import com.cetc.feign.hystrix.TestHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping; @Component
@FeignClient(value = "client", configuration = {FeignConfiguration.class}, fallback = TestHystrix.class)
public interface TestFeign { @GetMapping("/api/test/getPort")
Integer getPort();
}

  e、测试、启动Eureka-Server、Eureka-Client、Hystrix-Feign端口分别为8670、8673、8678。

  

  正常访问:

  

  服务器异常:

  

  六、Hystrix Dashboard监控熔断器的状态。

  在微服务架构中 ,为了保证服务实例的可用性,防止服务实例出现故障导致线程阻塞,而出现了熔断器模型 烙断器的状况反映了 个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard 是监控 Hystrix 的熔断器状况的 个组件,提供了数据监控和 友好的图形化展示界面。

  1)Hystrix Dashboard和Ribbon结合使用(代码基于上面的熔断器代码编写)

  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-netflix-hystrix-dashboard</artifactId>
</dependency>

  说明:因为是监控所以加入actuator。hystrix为起步依赖这里需要。

  b、加入注解@EnableHystrixDashboard

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class DashboardRibbonApplication { public static void main(String[] args) {
SpringApplication.run(DashboardRibbonApplication.class, args);
}
}

  c、加入相关配置到application.yaml,放开actuator的访问

management:
endpoints:
web:
exposure:
include: ["*"]

  d、测试。服务包含Eureka-Server、Eureka-Client、Dashboard-Ribbon。端口分别为8670、8673、8679

  

  测试接口:

  

  访问:http://127.0.0.1:8679/hystrix

  

  按照提示在输入框中对应链接:http://127.0.0.1:8679/actuator/hystrix.stream,点击Monitor Stream。测试效果

  

  上面数字代表的意思如下:

  

  2)Hystrix Dashboard和Feign结合使用(代码基于上面的熔断器代码编写)

  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-netflix-hystrix-dashboard</artifactId>
</dependency>

  说明:虽然Feign继承了hystrix,但那不是起步依赖说以这里还是需要hystrix的依赖

  b、加入注解:

package com.cetc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
public class DashboardFeignApplication { public static void main(String[] args) {
SpringApplication.run(DashboardFeignApplication.class, args);
}
}

  说明:为什么还是需要加入@EnableHystrix的注解呢,在使用Dashboard的时候需要这个注解,不然会出现错误。

  c、修改application.yaml配置

feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: ["*"]

  d、测试。启动服务有Eureka-Server、Eureka-Client、Dashboard-Feign。端口分别为8670、8673、8680.

  

  测试正常的接口:

  

  访问:http://127.0.0.1:8680/hystrix

  

  按提示输入监控链接:http://127.0.0.1:8680/actuator/hystrix.stream,点击Monitor Stream。效果如下:

  

  数字代表的意义:

  

   七、Turbine聚合监控。

  在使用 Hystrix Dashboard 组件监控服务的熔断器状况时, 每个服务都有一个HystrixDashboard 主页,当服务数量很多时,监控非常不方便。为了同时监控多个服务的熔断器的状况, Netflix 开源了 Hystrix 的另 个组件 Turbine Turbine 用于聚合多个 Hystrix Dashboard,将多个 Hystrix Dashboard 组件的数据放在一个页面上展示,进行集中监控。

  1)加入依赖:

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>

  2)修改配置application.yaml

server:
port: 8681
eureka:
client:
service-url:
defaultZone:
http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式
spring:
application:
name: turbine
turbine:
aggregator:
cluster-config: default
app-config: dashboard-ribbon,dashboard-feign
cluster-name-expression: new String("default")
management:
endpoints:
web:
exposure:
include: ["*"]

  说明:这里的turbine配置主要是加入服务app-config,两个服务均是之前写好的服务。

  3)编写启动项加入注解:

package com.cetc;

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
@EnableHystrixDashboard
@EnableTurbine
public class TurbineApplication { public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}

  4)测试。启动服务有Eureka-Server、Eureka-Client、Dashboard-Ribbon、Dashboard-Feign、Turbine。端口分别为8670、8673、8679、8680、8681.

  

  测试正常服务访问:

  

  

  打开:http://127.0.0.1:8681/hystrix

  

  按照提示输入:http://127.0.0.1:8681/turbine.stream。点击Monitor Stream。效果如下

  

  八、源码地址:https://github.com/lilin409546297/spring-cloud/tree/master/hystrix

Spring-Cloud之Hystrix熔断器-5的更多相关文章

  1. spring cloud 的hystrix 熔断器 和feign 调用的使用

    1, 添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  2. Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?

    导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...

  3. Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失问题分析

    最近spring boot项目中由于使用了spring cloud 的hystrix 导致了threadLocal中数据丢失,其实具体也没有使用hystrix,但是显示的把他打开了,导致了此问题. 导 ...

  4. Spring Cloud断路器Hystrix

    在微服务架构中,存在着那么多的服务单元,若一个单元出现故障,就会因依赖关系形成故障蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构就更加的不稳定.为了解决这样的问题,因此产生了断路器模式. 什么是 ...

  5. Spring Cloud之Hystrix服务保护框架

    服务保护利器 微服务高可用技术 大型复杂的分布式系统中,高可用相关的技术架构非常重要. 高可用架构非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的 ...

  6. Spring Cloud 之 Hystrix.

    一.概述  在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依 ...

  7. Spring Cloud 学习--Hystrix应用

    上一篇介绍了Hystrix基本功能和单独使用的方式,今天继续学习如何将Hystrix融入SpringCloud组件中去. 在Ribbon上使用熔断器 在 pom.xml 文件中引入 hystrix 的 ...

  8. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  9. 架构师系列文:通过Spring Cloud组件Hystrix合并请求

    在前文里,我们讲述了通过Hystrix进行容错处理的方式,这里我们将讲述通过Hystrix合并请求的方式 哪怕一个URL请求调用的功能再简单,Web应用服务都至少会开启一个线程来提供服务,换句话说,有 ...

  10. 从零开始学spring cloud(十一) -------- hystrix监控

    一.官方文档阅读 服务启动后,可以通过/health和hystrix.stream查看效果,实际上,访问上述两个地址,会出现404,这是因为spring boot版本的问题, 我在这里使用的sprin ...

随机推荐

  1. mybatis多参数查询问题:org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available par

    错误如下:  这个是由于在执行sql的时候无法匹配sql语句的通配符造成的,有两种方式可以解决 第一种:在sql语句中通配符这样写,用0,1匹配:  第二种:在dao接口的方法中的参数前面加param ...

  2. 禁用F12,屏蔽鼠标右击、

    $(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }) ...

  3. mysql优化查找执行慢的sql

    想要进行sql优化,肯定得先找出来需要优化的sql语句 一.mysql有一个自带的sql执行慢记录日志文件,所记录的日志取决于参数long_query_time控制,默认情况下long_query_t ...

  4. adb: failed to install app-debug.apk: Failure [INSTALL_FAILED_ABORTED: User rejected permissions]

    一.使用adb 的usb真机调试,安装错误: adb: failed to install app-debug.apk: Failure [INSTALL_FAILED_ABORTED: User r ...

  5. 探索ENCODE数据库 | Encyclopedia of DNA Elements

    ENCODE: Encyclopedia of DNA Elements 目标:按不同组织,收集人类(还有小鼠.worm.fly)基因组里面的所有功能元件 The primary goal of th ...

  6. RocketMQ常用命令【转】

    首先进入 RocketMQ 工程,进入/RocketMQ/bin   在该目录下有个 mqadmin 脚本 . 查看帮助:   在 mqadmin 下可以查看有哪些命令 a: 查看具体命令的使用 :  ...

  7. Refused to execute script from '...' because its MIME type ('') is not executable, and strict MIME type checking is enabled.

    写在前面 部署项目到weblogic上启动首页访问空白, 浏览器控制台报如题错误. web.xml中把响应头添加防止攻击的报文过滤器禁用就行了(仅仅是为了启动), 以下为转载内容, 可以根据需要自行测 ...

  8. spring容器干掉if-else

    场景说明 最近新做一个项目,需要对不同店铺的商品做不同处理.例如storeA需要进行handleA操作,storeB需要进行handleB操作,如此类推 大家很容易会想到下面的实现方法 public ...

  9. 源码编译Redis Desktop Manager ---(转载)

    精美文章转载: 版权声明:本文作者为「Kany.Wang」,本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议.转载请注明出处!原文链接:https://kany.me/20 ...

  10. Kafka安装教程(详细过程)

    安装前期准备: 1,准备三个节点(根据自己需求决定) 2,三个节点上安装好zookeeper(也可以使用kafka自带的zookeeper) 3,关闭防火墙 chkconfig  iptables o ...