在 Ribbon 和 Feign 項目增長 Hystrix 儀表盤功能,兩個項目的改造方式相同。php
pom.xml
中增長依賴<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
@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); } }
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);
}
}
瀏覽器端訪問http://localhost:8764hystrix 界面以下:java
點擊 Monitor Stream,進入下一個界面,訪問 http://localhost:8764/hi?message=HelloRibbon 此時會出現監控界面:git
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.command.default.execution.isolation.thread.timeoutInMilliseconds
:在調用方配置,被該調用方的全部方法的超時時間都是該值,優先級低於下邊的指定配置hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
:在調用方配置,被該調用方的指定方法(HystrixCommandKey 方法名)的超時時間是該值hystrix.threadpool.default.coreSize
:默認爲 10hystrix.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
:短路多久之後開始嘗試是否恢復,默認 5shystrix.command.default.circuitBreaker.errorThresholdPercentage
:出錯百分比閾值,當達到此閾值後,開始短路。默認 50%hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
:調用線程容許請求 HystrixCommand.GetFallback()
的最大數量,默認 10。超出時將會有異常拋出,注意:該項配置對於 THREAD 隔離模式也起做用