SpringCloud之Hystrix断路器

1. 分布式系统面临的问题

复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免失败!

2. 服务雪崩

​ 多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其他的微服务,这就是所谓的“扇出”,如果扇出的链路上某个微服务的调用响应时间过长,或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应

在这里插入图片描述
中间的请求处于响应时间过长,或者不可用,导致A一直占用系统资源,最终导致一系列的问题。

对于高流量的应用来说,单一的后端依赖可能会导致所有服务器上的所有资源都在几十秒内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障,这些都表示需要对故障和延迟进行隔离和管理,以达到单个依赖关系的失败而不影响整个应用程序或系统运行。

3. 什么是Hystrix?

​ Hystrix是一个应用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时,异常等,Hystrix 能够保证在一个依赖出问题的情况下,不会导致整个体系服务失败,避免级联故障,以提高分布式系统的弹性。

​ “断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控 (类似熔断保险丝) ,向调用方返回一个服务预期的,可处理的备选响应 (FallBack) ,而不是长时间的等待或者抛出调用方法无法处理的异常,这样就可以保证了服务调用方的线程不会被长时间,不必要的占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。

正常的请求情况:
在这里插入图片描述 当一个依赖的节点坏掉时,将阻塞整个的用户请求:
在这里插入图片描述
流量高峰时,一个单节点的宕机或延迟,会迅速导致所有服务负载达到饱和。应用中任何一个可能通过网络访问其他服务的节点,都有可能成为造成潜在故障的来源。更严重的是,还可能导致服务之间的延迟增加,占用队列、线程等系统资源,从而导致多系统之间的级联故障。
在这里插入图片描述
当你启用 Hystrix 封装了原有的远程调用请求后,整个流程图变为下图所示。
在这里插入图片描述

4. Hystrix能干嘛?

  • 服务降级
  • 服务熔断
  • 服务限流
  • 接近实时的监控

  • 官网资料:https://github.com/Netflix/Hystrix/wiki
    中文手册:https://www.cnblogs.com/flashsun/p/12579367.html

5. 服务熔断(服务端)

5.1 什么是服务熔断?

​ 熔断机制是赌赢雪崩效应的一种微服务链路保护机制

​ 当扇出链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。检测到该节点微服务调用响应正常后恢复调用链路。在SpringCloud框架里熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阀值缺省是5秒内20次调用失败,就会启动熔断机制。熔断机制的注解是:@HystrixCommand

服务熔断解决如下问题:

  • 当所依赖的对象不稳定时,能够起到快速失败的目的;
  • 快速失败后,能够根据一定的算法动态试探所依赖对象是否恢复。

5.2 创建子模块

复制springcloud-provider-dept-8001子模块并命名为springcloud-provider-dept-hystrix-8001。
在这里插入图片描述

5.3 导入依赖

在springcloud-provider-dept-hystrix-8001的pom.xml添加

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

5.4 修改Controller

DeptController.java
@HystrixCommand:出现雪崩调用备用的方法。.
fallbackMethod:指向调用备用的方法名

/**
 * 提供restful服务
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/get/{id}")
    @HystrixCommand(fallbackMethod="hystrixGet")
    public Dept queryById(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("这个id=>"+id+",不存在该用户,或信息无法找到~");
        }
        return dept;
    }

    /**
     * 根据id查询备选方案(熔断)
     * @param id
     * @return
     */
    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept().setDeptno(id)
                .setDname("这个id=>"+id+",没有对应的信息,null---@Hystrix~")
                .setDb_source("在MySQL中没有这个数据库");
    }
}

5.5 添加注解支持

Hystrix开启的注解支持:@EnableCircuitBreaker

/**
 * 启动类
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker
public class DeptProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderHystrix_8001.class,args);
    }
}

5.6 启动测试

在浏览器输入:http://localhost:8001/dept/get/6
在这里插入图片描述
在浏览器输入:http://localhost:8001/dept/get/5
在这里插入图片描述

6. 服务降级(客户端)

6.1 什么是服务降级?

​ 服务降级是指 当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理,或换种简单的方式处理,从而释放服务器资源以保证核心业务正常运作或高效运作。说白了,就是尽可能的把系统资源让给优先级高的服务。

服务降级主要用于什么场景呢?当整个微服务架构整体的负载超出了预设的上限阈值或即将到来的流量预计将会超过预设的阈值时,为了保证重要或基本的服务能正常运行,可以将一些 不重要 或 不紧急 的服务或任务进行服务的 延迟使用 或 暂停使用。

降级的方式可以根据业务来,可以延迟服务,比如延迟给用户增加积分,只是放到一个缓存中,等服务平稳之后再执行 ;或者在粒度范围内关闭服务,比如关闭相关文章的推荐。
在这里插入图片描述

6.2 服务降级案例

Feign中使用断路器

6.2.1 修改springcloud-api

新建DeptClientServiceFallbackFactory.java

@Component
public class DeptClientServiceFallbackFactory implements FallbackFactory {
    @Override
    public DeptClientService create(Throwable throwable) {
        return new DeptClientService() {
            @Override
            public boolean addDept(Dept dept) {
                return false;

            }

            @Override
            public Dept queryById(Long deptno) {
                return new Dept()
                        .setDeptno(deptno)
                        .setDname("deptno=>" + deptno + "没有对应的信息,客户端提供了降级的信息,这个服务现在已经被关闭")
                        .setDb_source("没有数据~");
            }

            @Override
            public List<Dept> quertAll() {
                return null;
            }
        };
    }
}

添加fallbackFactory指定降级配置类

fallbackFactory = DeptClientServiceFallbackFactory.class

完整的代码:

@Component
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientServiceFallbackFactory.class)//fallbackFactory指定降级配置类
public interface DeptClientService {
    @PostMapping("/dept/add")
    public boolean addDept(Dept dept);

    @GetMapping("/dept/get/{id}")
    public Dept queryById(@PathVariable("id") Long deptno);

    @GetMapping("/dept/list")
    public List<Dept> quertAll();
}

6.2.2 在使用Feign子模块开启Hystrix支持

application.yml

# 开启降级feign.hystrix
feign:
  hystrix:
    enabled: true

6.2.3 启动测试

正常情况下访问:http://localhost/consumer/dept/get/6
在这里插入图片描述
关闭服务提供方:DeptProvider_8001
再进行访问:http://localhost/consumer/dept/get/6
在这里插入图片描述

7. 服务熔断与服务降级区别

  • 服务熔断:服务端某个服务超时或者异常,引起熔断,类似保险丝作用;
  • 服务降级:客户端从整体网站请求负载考虑,当某个服务熔断或者关闭之后,服务将不再被调用~
    此时在客户端,我们可以准备一个 FaLlbackFactory,返回一个默认的值(缺省值),整体的服务水平下降了~但是,好歹能用,比直接挂掉强。

8. Dashboard 流监控

8.1 创建子模块

子模块名称:springcloud-consumer-hystrix-dashboard

8.2 导入依赖

<dependencies>
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>springcloud-api</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--ribbon spring-cloud-starter-ribbon不再维护-->
    <!-- <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-ribbon</artifactId>
         <version>1.4.7.RELEASE</version>
     </dependency>-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <!--eureka-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <!--dashboard-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
</dependencies>

8.3 编写配置

application.yml

server:
  port: 9001


# 配置dashboard监控 不然会出现 Unable to connect to Command Metric Stream
hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

8.4 编写启动类

/**
 * 启动类
 */
@SpringBootApplication
@EnableHystrixDashboard //开启监控
public class DeptConsumerDashboard_9001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumerDashboard_9001.class,args);
    }
}

8.5 启动测试

在这里插入图片描述

8.6 对某个模块进行监控

8.6.1 导入依赖

在被监控的模块的pom.xml下添加:

<!--actuator监控信息-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hystrix-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

8.6.2 添加ServletRegistrationBean

在被监控的启动类上添加:

1、@EnableCircuitBreaker
 2、@Bean
   public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }

完整的代码如下:

/**
 * 启动类
 */
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker
public class DeptProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderHystrix_8001.class,args);
    }
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        return registrationBean;
    }
}

8.6.3 使用熔断机制

@HystrixCommand(fallbackMethod=“hystrixGet”)
完整代码:

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/get/{id}")
    @HystrixCommand(fallbackMethod="hystrixGet")
    public Dept queryById(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("这个id=>"+id+",不存在该用户,或信息无法找到~");
        }
        return dept;
    }

    /**
     * 根据id查询备选方案(熔断)
     * @param id
     * @return
     */
    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept().setDeptno(id)
                .setDname("这个id=>"+id+",没有对应的信息,null---@Hystrix~")
                .setDb_source("在MySQL中没有这个数据库");
    }
}

8.7启动测试

发现出现错误: Unable to connect to Command Metric Stream
在这里插入图片描述
有可能是配置两个*,Hystrix Dashboard会通过proxyUrl解析到host部分,然后通过配置的proxyStreamAllowList。判定是否允许被访问。——来自百度
在springcloud-consumer-hystrix-dashboard的application.yml添加

# 配置dashboard监控 不然会出现 Unable to connect to Command Metric Stream
hystrix:
  dashboard:
    proxy-stream-allow-list: localhost

重新启动
在这里插入图片描述

在这里插入图片描述

8.8监控页面说明

在这里插入图片描述

  • 一圈
    实心圆:公有两种含义,他通过颜色的变化代表了实例的健康程度
    它的健康程度从绿色<黄色<橙色<红色递减
    该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大,该实心圆就越大,所以通过该实心圆的展示,就可以在大量的实例中快速发现故障实例和高压力实例
    在这里插入图片描述

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

    在这里插入图片描述

  • 整图说明
    在这里插入图片描述

  • 搞懂一个才能看懂复杂的
    在这里插入图片描述
    内容参考:https://www.bilibili.com/video/BV1jJ411S7xr
    仅用于学习!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值