天天看點

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