天天看点

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

目录

  • 一、概述
    • 1、分布式系统面临的问题
    • 2、Hystrix简介
    • 3、作用
      • ① 服务降级
      • ② 服务熔断
      • ③ 接近实时的监控
    • 4、官方资料
    • 5、Hystrix官宣,停更进维
  • 二、Hystrix重要概念
    • 1、服务降级
    • 2、服务熔断
    • 3、服务限流
  • 三、Hystrix案例
    • 1、构建
      • ① 新建cloud-provider-hystrix-payment8001
      • ② POM
      • ③ YML
      • ④ 主启动类
      • ⑤ 业务类
    • 2、高并发测试
      • ① Jmeter压测测试
      • ② Jmeter压测结论
      • ③ 80新建加入
        • ① 新建cloud-consumer-feign-hystrix-order80
        • ② POM
        • ③ YML
        • ④ 主启动类
        • ⑤ 业务类
        • ⑥ 正常测试
        • ⑦ 高并发测试
    • 3、故障现象和导致原因
    • 4、上述结论
    • 5、如何解决?解决的要求
      • ① 超时导致服务器变慢
      • ② 出错(宕机或程序运行出错)
      • ③ 解决
    • 6、服务降级
      • ① 降级配置
      • ② 8001先从自身找问题
      • ③ 8001fallback
      • ④ 80fallback
        • (1)80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端降级保护
        • (2)我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务
        • (3)YML
        • (4)主启动类
        • (5)业务类
      • ⑤ 目前问题
      • ⑥ 解决代码膨胀
        • (1)@DefaultProperties(defaultFallback
        • (2)Controller
      • ⑦ 解决混乱
        • (1)服务降级,客户端去调用服务端,碰上服务端宕机或关闭
        • (2)本次案例服务降级处理是在客户端80实现完成的,与服务端8001没有关系,只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦
        • (3)未来我们要面对的异常
        • (4)再看我们的业务类PaymentController
        • (5)修改cloud-consumer-feign-hystrix-order80
        • (6)根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理
        • (7) YML
        • (8)测试
    • 7、服务熔断
      • ① 断路器
      • ② 熔断是什么
      • ③ 实操
        • (1)修改cloud-provider-hystrix-payment8001
        • (2)PaymentService
        • (3)PaymentController
        • (4)测试
      • ③ 原理(小总结)
        • (1)大神结论
        • (2)熔断类型
        • (3)官网断路器流程图
    • 8、服务限流
  • 四、hystrix工作流程
    • 1、https://github.com/Netflix/Hystrix/wiki/How-it-Works
    • 2、hystrix工作流程
      • ① 官网图例
      • ② 步骤说明
  • 五、服务监控hystrixDashboard
    • 1、概述
    • 2、仪表盘9001
      • ① 新建cloud-consumer-hystrix-dashboard9001
      • ② POM
      • ③ YML
      • ④ HystrixDashboardMain9001+新注解@EnableHystrixDashboard
      • ⑤ 所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置
      • ⑥ 启动cloud-consumer-hystrix-dashboard9001该微服务后续将监控微服务8001
    • 3、断路器演示
      • ① 修改cloud-provider-hystrix-payment8001
      • ② 测试

一、概述

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

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

服务雪崩

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

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

所以,通常当你发现一个模块下的某个实例失败后,这时候这个模块依然还会接受流量,然后这个有问题的模块害调用了其他模块,这样就会发生级联故障,或者叫雪崩。

2、Hystrix简介

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

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

3、作用

① 服务降级

② 服务熔断

③ 接近实时的监控

4、官方资料

https://github.com/Netflix/Hystrix/wiki/How-To-Use

5、Hystrix官宣,停更进维

https://github.com/Netflix/Hystrix

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

二、Hystrix重要概念

1、服务降级

  • 服务器忙,请稍候再试,不让客户端等待并立刻返回一个友好提示,fallback
  • 哪些情况会触发降级

    1.程序运行异常

    2.超时

    3.服务熔断触发服务降级

    4.线程池/信号量打满也会导致服务降级

2、服务熔断

  • 类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示
  • 就是保险丝

    服务的降级->进而熔断->恢复调用链路

3、服务限流

  • 秒杀高并发等操作,严禁一窝蜂的过来拥挤,大家排队,一秒钟N个,有序进行

三、Hystrix案例

1、构建

① 新建cloud-provider-hystrix-payment8001

② POM

<dependencies>
        <!--新增hystrix-->
        <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-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>com.radish.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
           

③ YML

server:
  port: 8001


eureka:
  client:
    register-with-eureka: true    #表识不向注册中心注册自己
    fetch-registry: true   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
    service-url:
      # defaultZone: http://eureka7002.com:7002/eureka/    #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
      defaultZone: http://eureka7001.com:7001/eureka/
#  server:
#    enable-self-preservation: false
spring:
  application:
    name: cloud-provider-hystrix-payment
#    eviction-interval-timer-in-ms: 2000
           

④ 主启动类

@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class,args);
    }
}
           

⑤ 业务类

PaymentService

@Service
public class PaymentService {

    //成功
    public String paymentInfo_OK(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"   paymentInfo_OK,id:  "+id+"\t"+"哈哈哈"  ;
    }

    //失败
    public String paymentInfo_TimeOut(Integer id){
        int timeNumber = 3;
        try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
        return "线程池:"+Thread.currentThread().getName()+"   paymentInfo_TimeOut,id:  "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber;
    }
}
           

PaymentController

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_OK(id);
        log.info("*******result:"+result);
        return result;
    }
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentService.paymentInfo_TimeOut(id);
        log.info("*******result:"+result);
        return result;
    }
}
           

2、高并发测试

① Jmeter压测测试

创建线程组

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

配置200个线程,发100次,也就是20000并发

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

配置地址

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

再次访问http://localhost:8001/payment/hystrix/ok/31后,速度变卡

为什么会被卡死

tomcat的默认的工作线程数被打满了,没有多余的线程来分解压力和处理。

② Jmeter压测结论

上面还是服务提供者8001自己测试,假如此时外部的消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接被拖死

③ 80新建加入

① 新建cloud-consumer-feign-hystrix-order80

② POM

<dependencies>
        <!--新增hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.radish.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
           

③ YML

server:
  port: 80

eureka:
  client:
    register-with-eureka: true    #表识不向注册中心注册自己
    fetch-registry: true   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

spring:
  application:
    name: cloud-provider-hystrix-order
           

④ 主启动类

@SpringBootApplication
@EnableFeignClients
public class OrderHystrixMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderHystrixMain80.class,args);
    }
}
           

⑤ 业务类

PaymentHystrixService

@Service
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
           

OrderHystrixController

@RestController
@Slf4j
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("*******result:"+result);
        return result;
    }
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("*******result:"+result);
        return result;
    }
}
           

⑥ 正常测试

http://localhost/consumer/payment/hystrix/ok/31

⑦ 高并发测试

2W个线程压8001

消费端80微服务再去访问正常的OK微服务8001地址

http://localhost/consumer/payment/hystrix/timeout/31

消费者80变卡

3、故障现象和导致原因

8001同一层次的其他接口服务被困死,因为tomcat线程里面的工作线程已经被挤占完毕

80此时调用8001,客户端访问响应缓慢

4、上述结论

正因为有上述故障或不佳表现,才有我们的降级/容错/限流等技术诞生

5、如何解决?解决的要求

① 超时导致服务器变慢

超时不再等待

② 出错(宕机或程序运行出错)

③ 解决

对方服务(8001)超时了,调用者(80)不能一直卡死等待,必须有服务降级

对方服务(8001)down机了,调用者(80)不能一直卡死等待,必须有服务降级

对方服务(8001)OK,调用者(80)自己出故障或有自我要求(自己的等待时间小于服务提供者),自己处理降级

6、服务降级

① 降级配置

@HystrixCommand

② 8001先从自身找问题

设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,作服务降级fallback

③ 8001fallback

业务类启用

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

主配置类激活

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

配置了5秒访问后,访问失败,因为配置了3秒钟后服务降级

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

④ 80fallback

(1)80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端降级保护

(2)我们自己配置过的热部署方式对java代码的改动明显,但对@HystrixCommand内属性的修改建议重启微服务

(3)YML

feign:
  hystrix:
    enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。
           

(4)主启动类

@EnableHystrix

(5)业务类

80controller

@GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")  //3秒钟以内就是正常的业务逻辑
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("*******result:"+result);
        return result;
    }

    //兜底方法
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "我是消费者80,对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬_┬)";
    }
           
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

⑤ 目前问题

每个业务方法对应一个兜底的方法,代码膨胀

统一和自定义的分开

⑥ 解决代码膨胀

(1)@DefaultProperties(defaultFallback

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

@DefaultProperties(defaultFallback = "")

1:1 每个方法配置一个服务降级方法,技术上可以,实际上很傻

1:N 除了个别重要核心业务有专属,其它普通的可以通过

@DefaultProperties(defaultFallback = "")

统一跳转到统一处理结果页面

通过的和独享的各自分开,避免了代码膨胀,合理减少了代码量

(2)Controller

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        log.info("*******result:"+result);
        return result;
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")  //3秒钟以内就是正常的业务逻辑
//    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        log.info("*******result:"+result);
        return result;
    }

    public String payment_Global_FallbackMethod(){
        return "Global 222 对方系统繁忙或者已经宕机,请10秒后再次尝试";
    }
    //兜底方法
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "我是消费者80,对付支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,(┬_┬)";
    }
}
           

⑦ 解决混乱

(1)服务降级,客户端去调用服务端,碰上服务端宕机或关闭

(2)本次案例服务降级处理是在客户端80实现完成的,与服务端8001没有关系,只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦

(3)未来我们要面对的异常

  • 运行
  • 超时
  • 宕机

(4)再看我们的业务类PaymentController

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

(5)修改cloud-consumer-feign-hystrix-order80

(6)根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理

@Component
public class PaymentFallbackService implements PaymentHystrixService {
    @Override
    public String paymentInfo_OK(Integer id) {
        return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)";
    }
}

           

(7) YML

@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)

(8)测试

故意关闭微服务8001

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

7、服务熔断

① 断路器

一句话就像家里保险丝

② 熔断是什么

大神论文https://martinfowler.com/bliki/CircuitBreaker.html

熔断机制概述

熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务出错不可用或者响应时间太长,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。

当检测到该节点微服务调用响应正常后,恢复调用链路

在SpringCloud框架里,熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是是5秒内20次调用失败,就会启动熔断机制,熔断机制的注解是@HystrixCommand。

③ 实操

(1)修改cloud-provider-hystrix-payment8001

(2)PaymentService

//服务熔断
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
        @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),  //是否开启断路器
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),   //请求次数
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),  //时间范围
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
    if (id < 0){
        throw new RuntimeException("*****id 不能负数");
    }
    String serialNumber = IdUtil.simpleUUID();

    return Thread.currentThread().getName()+"\t"+"调用成功,流水号:"+serialNumber;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
    return "id 不能负数,请稍候再试,(┬_┬)/~~     id: " +id;
}
           
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

(3)PaymentController

//===服务熔断
@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
    String result = paymentService.paymentCircuitBreaker(id);
    log.info("*******result:"+result);
    return result;
}
           

(4)测试

自测cloud-provider-hystrix-payment8001

正确:http://localhost:8001/payment/circuit/31

错误:http://localhost:8001/payment/circuit/-31

多次错误,然后慢慢正确,发现刚开始不满足条件,就算是正确的访问地址也不能进行访问,需要慢慢的恢复链路

③ 原理(小总结)

(1)大神结论

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

(2)熔断类型

  • 熔断打开:请求不再进行调用当前服务,内部设置时钟一般为MTTR(平均故障处理时间),当打开时长达到所设时钟则进入熔断状态
  • 熔断关闭:熔断关闭不会对服务进行熔断
  • 熔断半开:部分请求根据规则调用当前服务,如果请求成功且符合规则则认为当前服务恢复正常,关闭熔断

(3)官网断路器流程图

官网步骤

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

断路器在什么情况下开始起作用

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

断路器开启或者关闭的条件

  • 当满足一定阀值的时候(默认10秒内超过20个请求次数)
  • 当失败率达到一定的时候(默认10秒内超过50%请求失败)
  • 到达以上阀值,断路器将会开启
  • 当开启的时候,所有请求都不会进行转发
  • 一段时间之后(默认是5秒),这个时候断路器是半开状态,会让其中一个请求进行转发。如果成功,断路器会关闭,若失败,继续开启。重复4和5

断路器打开之后

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

All配置

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

8、服务限流

后面高级篇讲解alibaba的Sentinel说明

四、hystrix工作流程

1、https://github.com/Netflix/Hystrix/wiki/How-it-Works

2、hystrix工作流程

① 官网图例

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

② 步骤说明

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

五、服务监控hystrixDashboard

1、概述

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

2、仪表盘9001

① 新建cloud-consumer-hystrix-dashboard9001

② POM

<dependencies>
        <!--新增hystrix dashboard-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
           

③ YML

server:
  port: 9001
           

④ HystrixDashboardMain9001+新注解@EnableHystrixDashboard

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardMain9001.class,args);
    }
}
           

⑤ 所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
           

⑥ 启动cloud-consumer-hystrix-dashboard9001该微服务后续将监控微服务8001

http://localhost:9001/hystrix

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard

3、断路器演示

① 修改cloud-provider-hystrix-payment8001

注意:新版本Hystrix需要在主启动类MainAppHystrix8001中指定监控路径

/**
     * 此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
     * ServletRegistrationBean因为springboot的默认路径不是“/hystrix.stream”
     * 只要在自己项目配置上下面的servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
           

② 测试

9001监控8001

SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard
SpringCloud2020学习笔记09——Hystrix断路器一、概述二、Hystrix重要概念三、Hystrix案例四、hystrix工作流程五、服务监控hystrixDashboard