一:什么是Hystrix

在分布式环境中,许多服务依赖项中的一些将不可避免地失败。Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,停止其间的级联故障以及提供回退选项,从而提高系统的整体弹性。

Hystrix旨在执行以下操作

1:对通过第三方客户端库访问(通常通过网络)的依赖关系提供保护并控制延迟和故障。

2:隔离复杂分布式系统中的级联故障。

3:快速发现故障,尽快恢复。

4:回退,尽可能优雅地降级。

5:启用近实时监控,警报和操作控制。

二:为什么需要Hystrix?

大型分布式系统中,一个客户端或者服务依赖外部服务,如果一个服务宕了,那么由于我们设置了服务调用系统超时时间,势必会影响相应时间,在高并发的情况下大多数服务器的线程池就出现阻塞(BLOCK),影响整个线上服务的稳定性。

(图片官方图片)

当一切都健康时,请求可以看起来像这样

当许多后端服务系统中的一个宕掉时,整个用户请求:

如果多个客户端调用同一个异常服务的时候,出现的情况是:

三:Hystrix解决什么问题?

分布式架构中的应用程序具有几十个依赖关系,每个依赖关系在某个时刻将不可避免的出现异常。如果应用程序不与这些外部故障隔离,则可能出现线程池阻塞,引起系统雪崩。

1
2
3
4
例如,对于依赖30个服务的应用程序,每个服务的正常运行时间为99.99%,您可以:
99.99%的30次方 = 99.7%正常运行时间
0.3%的10亿次请求= 3,000,000次故障
2+小时宕机/月,即使所有依赖关系正常运行时间。

当使用Hystrix进行熔断后,每个依赖关系彼此隔离了,限制了当发生延迟时的阻塞。

四:Hystrix结合Feign使用

创建一个工程eureka_feign_hystrix_client

pom.xml文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

创建启动文件

FeignHystrixApplication

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignHystrixApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(FeignHystrixApplication.class, args);
    }
 
}

UserClient类

1
2
3
4
5
6
7
8
9
10
11
12
13
@FeignClient(value = "biz-service-0",fallback = UserClientHystrix.class)
public interface UserClient {
 
    @RequestMapping(method = RequestMethod.GET, value = "/getuser")
    public User getuserinfo();
     
    @RequestMapping(method = RequestMethod.GET, value = "/getuser")
    public String getuserinfostr();
     
    @RequestMapping(method = RequestMethod.GET, value = "/info")
    public  String  info();
 
}

创建熔断类UserClientHystrix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Service
public class UserClientHystrix implements UserClient {
 
    @Override
    public User getuserinfo() {
        throw new NullPointerException(" User getuserinfo() 服务不可用。。");
    }
 
    @Override
    public String getuserinfostr() {
 
        return " UserClientHystrix getuserinfostr() is fallback 服务不可用。。";
    }
 
    @Override
    public String info() {
 
        return " UserClientHystrix info() is fallback 服务不可用。。";
    }
 
}

当网络出现异常的时候或直接跳转到这里实现类里面

创建action类

UserController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Autowired
    UserClient userClient;
 
    @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
    public User getuserinfo() {
        return userClient.getuserinfo();
    }
     
    @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)
    public String getuserinfostr() {
        return userClient.getuserinfostr();
    }
     
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info() {
        return userClient.info();
    }

先启动:eureka_register_service(注册中心)工程

然后运行我们写好的FeignHystrixApplication

这个时候我们明显发现没有运行biz-service-0 服务,那么我们 打开 http://127.0.0.1:8005/getuserinfostr

出现

1
UserClientHystrix getuserinfostr() is fallback 服务不可用。。

这个就是我们自定义的熔断返回结果

如果不用熔断 页面会出现这个

1
2
3
4
5
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Mar 22 14:32:21 CST 2017
There was an unexpected error (type=Internal Server Error, status=500).
getuserinfo failed and fallback failed.

代码地址:https://github.com/zhp8341/SpringCloudDemo

本人也看了一些Hystrix相关原理,由于没有全部看完所以暂时没有写上去,本文是结合Feign使用和学习。

有兴起的可以看下官方的Hystrix很详细,就是看起来有点费劲,

Dubbo结合Hystrix使用:https://my.oschina.net/yangshaokai/blog/674685

【微服务架构】SpringCloud之Hystrix断路器(六)的更多相关文章

  1. Spring Cloud构建微服务架构(三)断路器

    在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待.这样就不会使得线程因 ...

  2. 微服务架构 - SpringCloud整合分布式服务跟踪zipkin

    1.zipkin zipkin是Twitter的一个开源项目,它基于Google Dapper实现.我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的REST API接口来辅助我们查询 ...

  3. SpringCloud微服务架构和SOA架构

    1,传统的三层架构 在传统的架构中,SSH,SSM,主要分为web 控制层,业务逻辑层,数据库访问层,单点项目,项目没有拆分,所有的开发任务全部写在一个项目中,耦合度比价高,如果程序中的一个功能出现了 ...

  4. 微服务之SpringCloud基础

    SpringCloud微服务基础 微服务架构--SpringCloud网站架构模式 单点应用/分布式系统面向于服务架构(SOA) /微服务架构web项目三层架构1.控制层2.业务逻辑层3.数据访问层传 ...

  5. Spring Cloud构建微服务架构

    Dalston版本 由于Brixton和Camden版本的教程已经停止更新,所以笔者计划在2017年上半年完成Dalston版本的教程编写(原计划完成Camden版本教程,但由于写了两篇Dalston ...

  6. 《Spring Cloud构建微服务架构》系列博文示例

    SpringCloud-Learning   源码下载地址:http://download.csdn.net/detail/k21325/9650968     本项目内容为Spring Cloud教 ...

  7. 浅谈微服务架构、容器技术与K8S

    关注嘉为科技,获取运维新知 企业应用系统:从单体应用走向微服务架构:从裸金属走向容器. 如果在诸多热门云计算技术诸如容器.微服务.DevOps.OpenStack等之中,找出一个最火的方向,那么可能非 ...

  8. 用SpringCloud进行微服务架构演进

    在<架构师必须要知道的阿里的中台战略与微服务> 中已经阐明选择SpringCloud进行微服务架构实现中台战略,因此下面介绍SpringCloud的一些内容,SpringCloud已经出来 ...

  9. 【微服务架构】SpringCloud组件和概念介绍(一)

    一:什么是微服务(Microservice) 微服务英文名称Microservice,Microservice架构模式就是将整个Web应用组织为一系列小的Web服务.这些小的Web服务可以独立地编译及 ...

随机推荐

  1. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

    证券代码 证券简称 大股东持股比例 [日期] 最新 [大股东排名] 第1名 [单位] % 总市值2 [交易日期] 最新收盘日 [单位] 亿元 000004.SZ 国农科技 28.4200 23.261 ...

  2. Linux - samba 服务

    暂时关闭 iptables 防火墙 [root@sch01ar ~]# systemctl stop iptables.service 暂时关闭 firewall 防火墙 [root@sch01ar ...

  3. 分享Axure RP8.0激活码一份

    用户名:aaa 注册码:2GQrt5XHYY7SBK/4b22Gm4Dh8alaR0/0k3gEN5h7FkVPIn8oG3uphlOeytIajxGU

  4. pandas入门学习

    索引对象 pandas的索引对象负责管理轴标签和其他元数据(比如轴名称等).构建series或DataFrame时,所用到的任何数组或其他序列的标签都会转换成一个index: In [1]: impo ...

  5. jQuery基本API小结(下)---工具函数-基本插件

    一.工具函数 1.获取浏览器的名称与版本信息 在jQuery中,通过$.browser对象可以获取浏览器的名称和版本信息,如$.browser.chrome为true,表示当前为Chrome浏览器,$ ...

  6. RabbitMQ双活实践(转)

    有货RabbitMQ双活实践   消息服务中间件在日常工作中用途很多,如业务之间的解耦,其中 RabbitMQ 是比较容易上手且企业使用比较广泛的一种,本文主要介绍有货在使用 RabbitMQ 的一些 ...

  7. 使用ffmpeg合并视频文件的三种方法

    ffmpeg合并视频的方法有三种.国内大多数仅介绍了其中之一.于是觉得有必要翻译一下.其实在ffmpeg的 FAQ文档中有比较详细的说明. 使用concat协议进行视频文件的合并 这种方式的适用场景是 ...

  8. java反射(Field的应用)

    //$Id: DirectPropertyAccessor.java 11405 2007-04-15 12:50:34Z max.andersen@jboss.com $ package org.h ...

  9. 通过devtools在centos系统中启用高版本的gcc

    C++11出来好久了,现在还是使用c++03的,需要在centos6.6的系统上实现gcc的升级,又不想自己编译代码. 于是选用了devtoolsset系列,安装脚本如下 安装脚本如下 functio ...

  10. 使用AddressSanitizer做内存分析(一)——入门篇

    使用AddressSanitizer做内存分析 新建文件mem_leak.cpp,键入代码: #include <iostream> int main() { ]; p = NULL; ; ...