使用熔斷器儀表盤監控(hystrix)

概述

在 Ribbon 和 Feign 項目增長 Hystrix 儀表盤功能,兩個項目的改造方式相同。php

在 pom.xml 中增長依賴

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

 

 

在 Application 中增長 @EnableHystrixDashboard 註解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

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

 

 

Hystrix Dashboard監控啓動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {

public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}

 

 

測試 Hystrix Dashboard(根據端口訪問hystrix dashboard的頁面)

瀏覽器端訪問http://localhost:8764hystrix 界面以下:java

點擊 Monitor Stream,進入下一個界面,訪問 http://localhost:8764/hi?message=HelloRibbon 此時會出現監控界面:git

附:Hystrix 說明

什麼狀況下會觸發 fallback 方法

名字 描述 觸發fallback
EMIT 值傳遞 NO
SUCCESS 執行完成,沒有錯誤 NO
FAILURE 執行拋出異常 YES
TIMEOUT 執行開始,但沒有在容許的時間內完成 YES
BAD_REQUEST 執行拋出HystrixBadRequestException NO
SHORT_CIRCUITED 斷路器打開,不嘗試執行 YES
THREAD_POOL_REJECTED 線程池拒絕,不嘗試執行 YES
SEMAPHORE_REJECTED 信號量拒絕,不嘗試執行 YES

fallback 方法在什麼狀況下會拋出異常

名字 描述 拋異常
FALLBACK_EMIT Fallback值傳遞 NO
FALLBACK_SUCCESS Fallback執行完成,沒有錯誤 NO
FALLBACK_FAILURE Fallback執行拋出出錯 YES
FALLBACK_REJECTED Fallback信號量拒絕,不嘗試執行 YES
FALLBACK_MISSING 沒有Fallback實例 YES

Hystrix Dashboard 界面監控參數

Hystrix 經常使用配置信息

超時時間(默認1000ms,單位:ms)

  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:在調用方配置,被該調用方的全部方法的超時時間都是該值,優先級低於下邊的指定配置
  • hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds:在調用方配置,被該調用方的指定方法(HystrixCommandKey 方法名)的超時時間是該值

線程池核心線程數

  • hystrix.threadpool.default.coreSize:默認爲 10

Queue

  • hystrix.threadpool.default.maxQueueSize:最大排隊長度。默認 -1,使用 SynchronousQueue。其餘值則使用 LinkedBlockingQueue。若是要從 -1 換成其餘值則需重啓,即該值不能動態調整,若要動態調整,須要使用到下邊這個配置
  • hystrix.threadpool.default.queueSizeRejectionThreshold:排隊線程數量閾值,默認爲 5,達到時拒絕,若是配置了該選項,隊列的大小是該隊列

注意: 若是 maxQueueSize=-1 的話,則該選項不起做用github

斷路器

  • hystrix.command.default.circuitBreaker.requestVolumeThreshold:當在配置時間窗口內達到此數量的失敗後,進行短路。默認 20 個(10s 內請求失敗數量達到 20 個,斷路器開)
  • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds:短路多久之後開始嘗試是否恢復,默認 5s
  • hystrix.command.default.circuitBreaker.errorThresholdPercentage:出錯百分比閾值,當達到此閾值後,開始短路。默認 50%

allback

  • hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests:調用線程容許請求 HystrixCommand.GetFallback() 的最大數量,默認 10。超出時將會有異常拋出,注意:該項配置對於 THREAD 隔離模式也起做用

屬性配置參數

    • 參數說明:https://github.com/Netflix/Hystrix/wiki/Configuration
    • HystrixProperty 參考代碼:http://www.programcreek.com/java-api-examples/index.php?source_dir=Hystrix-master/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/configuration/command/BasicCommandPropertiesTest.java
相關文章
相關標籤/搜索