目錄
- 前言
- 1. Hystrix 基礎知識
- 1.1 Hystrix 斷路器強調呼叫
- 1.2 兩大類別的 Hystrix 實作
- 1.3 艙壁策略
- 1.4 Hystrix 在遠程資源呼叫失敗時的決策程序
- 1.5 當 Hystrix 斷路器打開后
- 1.6 Hystrix 的所有配置
- 2. 對服務使用 Hystrix 斷路器
- 2.1 引入 pom.xml 依賴
- 2.2 修改 bootstrap.yml 組態檔
- 2.3 在主程式類上標注注解
- 2.4 在業務類上使用 @HystrixCommand 注解(斷路器模式)
- 2.5 定制斷路器(后備策略、艙壁策略)
- 2.6 使用類級注解統一 Hystrix 配置
- 3. 使用 HystrixConcurrencyStrategy 聯系執行緒背景關系
- 3.1 Hystrix 的背景關系隔離
- 3.2 自定義 Hystrix 井發策略類
- 3.3 定義一個 Java Callable 類,將 UserContext 注入 Hystrix 命令中
- 3.4 配置 Spring Cloud 以使用自定義 Hystrix 井發策略
- 4. 使用 hystrixDashboard 實作服務監控
- 4.1 引入 pom.xml 依賴
- 4.2 修改 application.yml 組態檔
- 4.3 在主程式類上標注注解
- 4.4 配置被監控的服務
- 1. 添加 pom.xml 依賴檔案
- 2. 在主啟動類中指定監控路徑
- 4.5 訪問圖形化界面
- 4.6 查看監控圖
- 最后
前言
參考資料:
《Spring Microservices in Action》
《Spring Cloud Alibaba 微服務原理與實戰》
《B站 尚硅谷 SpringCloud 框架開發教程 周陽》
Hystrix 是一個延遲和容災庫,旨在隔離遠程系統、服務和第三方庫的訪問點,停止級聯故障,并在故障不可避免的復雜分布式系統中實作彈性;
1. Hystrix 基礎知識
1.1 Hystrix 斷路器強調呼叫
- Hystrix 斷路器沒有提供者與消費者的區別,它強調的是服務與資源之間的中間人,如:服務請求資料庫與服務內部呼叫;
- 因此在服務消費者與服務提供者里,凡是涉及資料庫訪問與服務間呼叫都可以使用 Hystrix 斷路器;
- 構建斷路器模式、后備模式和艙壁模式的實作需要對執行緒和執行緒管理有深入的理解;
- Netflix 的 Hystrix 庫里封裝了執行緒操作,開發人員可以只需要關注業務開發與 Spring Cloud;
1.2 兩大類別的 Hystrix 實作
- 使用 Hystrix 斷路器包裝所有服務中所有對資料庫的呼叫;
- 使用 Hystrix 斷路器包裝所有服務之間的內部服務呼叫;

1.3 艙壁策略

1.4 Hystrix 在遠程資源呼叫失敗時的決策程序

- 快照時間窗:查看在 10 s 內發生的呼叫次數:
- 請求總數閥值:如果呼叫次數少于在這個視窗內設定的最小呼叫次數,那么即使有幾個呼叫失敗,Hystrix 也不會采取行動;
- 反之,進行下一步;
- 查看整體故障的百分比:
- 錯誤百分比閥值:如果故障的總體百分比超過閾值, Hystrix 將觸發斷路器,使將來幾乎所有的呼叫都失敗;
- 當 Hystrix 斷路器被觸發時,它將嘗試啟動一個新的活動視窗:
- 每隔 5s(可配置的), Hystrix 會通過一個遠程呼叫, 如果呼叫成功, Hystrix將重置斷路器并重新開始讓呼叫通過, 如果呼叫失敗, Hystrix將保持斷路器斷開;
1.5 當 Hystrix 斷路器打開后
- 再有請求呼叫的時候,將不會呼叫主邏輯,而是直接呼叫降級fallback,通過斷路器,實作了自動地發現錯誤并將降級邏輯切換為主邏輯,減少回應延遲的效果;
- hystrix 會啟動一個休眠時間窗,在這個時間窗內,降級邏輯是臨時的成為主邏輯;
- 當休眠時間窗到期,斷路器將進入半開狀態,釋放一次請求到原來的主邏輯上;
- 如果此次請求正常回傳,那么斷路器將繼續閉合,主邏輯恢復;
- 反之,斷路器繼續進入打開狀態,休眠時間窗重新計時;
1.6 Hystrix 的所有配置


@HystrixCommand(fallbackMethod = "str_fallbackMethod",
groupKey = "strGroupCommand",
commandKey = "strCommand",
threadPoolKey = "strThreadPool",
commandProperties = {
// 設定隔離策略,THREAD 表示執行緒池 SEMAPHORE:信號池隔離
@HystrixProperty(name = "execution.isolation.strategy", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/THREAD"),
// 當隔離策略選擇信號池隔離的時候,用來設定信號池的大小(最大并發數)
@HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/10"),
// 配置命令執行的超時時間
@HystrixProperty(name = "execution.isolation.thread.timeoutinMilliseconds", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/10"),
// 是否啟用超時時間
@HystrixProperty(name = "execution.timeout.enabled", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
// 執行超時的時候是否中斷
@HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
// 執行被取消的時候是否中斷
@HystrixProperty(name = "execution.isolation.thread.interruptOnCancel", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
// 允許回呼方法執行的最大并發數
@HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/10"),
// 服務降級是否啟用,是否執行回呼函式
@HystrixProperty(name = "fallback.enabled", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
// 是否啟用斷路器
@HystrixProperty(name = "circuitBreaker.enabled", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
// 該屬性用來設定在滾動時間窗中,斷路器熔斷的最小請求數,例如,默認該值為 20 的時候,
// 如果滾動時間窗(默認10秒)內僅收到了19個請求, 即使這19個請求都失敗了,斷路器也不會打開,
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/20"),
// 該屬性用來設定在滾動時間窗中,表示在滾動時間窗中,在請求數量超過
// circuitBreaker.requestVolumeThreshold 的情況下,如果錯誤請求數的百分比超過50,
// 就把斷路器設定為 "打開" 狀態,否則就設定為 "關閉" 狀態,
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/50"),
// 該屬性用來設定當斷路器打開之后的休眠時間窗, 休眠時間窗結束之后,
// 會將斷路器置為 "半開" 狀態,嘗試熔斷的請求命令,如果依然失敗就將斷路器繼續設定為 "打開" 狀態,
// 如果成功就設定為 "關閉" 狀態,
@HystrixProperty(name = "circuitBreaker.sleepWindowinMilliseconds", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/5000"),
// 斷路器強制打開
@HystrixProperty(name = "circuitBreaker.forceOpen", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/false"),
// 斷路器強制關閉
@HystrixProperty(name = "circuitBreaker.forceClosed", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/false"),
// 滾動時間窗設定,該時間用于斷路器判斷健康度時需要收集資訊的持續時間
@HystrixProperty(name = "metrics.rollingStats.timeinMilliseconds", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/10000"),
// 該屬性用來設定滾動時間窗統計指標資訊時劃分"桶"的數量,斷路器在收集指標資訊的時候會根據
// 設定的時間窗長度拆分成多個 "桶" 來累計各度量值,每個"桶"記錄了一段時間內的采集指標,
// 比如 10 秒內拆分成 10 個"桶"收集這樣,所以 timeinMilliseconds 必須能被 numBuckets 整除,否則會拋例外
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/10"),
// 該屬性用來設定對命令執行的延遲是否使用百分位數來跟蹤和計算,如果設定為 false, 那么所有的概要統計都將回傳 -1,
@HystrixProperty(name = "metrics.rollingPercentile.enabled", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/false"),
// 該屬性用來設定百分位統計的滾動視窗的持續時間,單位為毫秒,
@HystrixProperty(name = "metrics.rollingPercentile.timeInMilliseconds", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/60000"),
// 該屬性用來設定百分位統計滾動視窗中使用 “ 桶 ”的數量,
@HystrixProperty(name = "metrics.rollingPercentile.numBuckets", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/60000"),
// 該屬性用來設定在執行程序中每個 “桶” 中保留的最大執行次數,如果在滾動時間窗內發生超過該設定值的執行次數,
// 就從最初的位置開始重寫,例如,將該值設定為100, 滾動視窗為10秒,若在10秒內一個 “桶 ”中發生了500次執行,
// 那么該 “桶” 中只保留 最后的100次執行的統計,另外,增加該值的大小將會增加記憶體量的消耗,并增加排序百分位數所需的計算時間,
@HystrixProperty(name = "metrics.rollingPercentile.bucketSize", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/100"),
// 該屬性用來設定采集影響斷路器狀態的健康快照(請求的成功、 錯誤百分比)的間隔等待時間,
@HystrixProperty(name = "metrics.healthSnapshot.intervalinMilliseconds", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/500"),
// 是否開啟請求快取
@HystrixProperty(name = "requestCache.enabled", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
// HystrixCommand的執行和事件是否列印日志到 HystrixRequestLog 中
@HystrixProperty(name = "requestLog.enabled", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/true"),
},
threadPoolProperties = {
// 該引數用來設定執行命令執行緒池的核心執行緒數,該值也就是命令執行的最大并發量
@HystrixProperty(name = "coreSize", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/10"),
// 該引數用來設定執行緒池的最大佇列大小,當設定為 -1 時,執行緒池將使用 SynchronousQueue 實作的佇列,
// 否則將使用 LinkedBlockingQueue 實作的佇列,
@HystrixProperty(name = "maxQueueSize", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/-1"),
// 該引數用來為佇列設定拒絕閾值, 通過該引數, 即使佇列沒有達到最大值也能拒絕請求,
// 該引數主要是對 LinkedBlockingQueue 佇列的補充,因為 LinkedBlockingQueue
// 佇列不能動態修改它的物件大小,而通過該屬性就可以調整拒絕請求的佇列大小了,
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "https://www.cnblogs.com/dlhjw/archive/2022/01/29/5"),
}
)
2. 對服務使用 Hystrix 斷路器
2.1 引入 pom.xml 依賴
<!--拉取 Spring Cloud Hystrix 依賴項-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
- 以下依賴是核心 Netflix Hystrix 庫,一般情況下不需要我們引入;
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.9</version>
</dependency>
2.2 修改 bootstrap.yml 組態檔
- 如果需要用到 Feign 呼叫才需要進行配置;
feign:
hystrix:
#開啟feign的hystrix支持,默認是false
enabled: true
2.3 在主程式類上標注注解
-
@EnableCircuitBreaker:表示激活使用服務降級相關策略;
-
@EnableHystrix:繼承了@EnableCircuitBreaker,并對其進行了在封裝;
-
如果忘記將該注解添加到主程式類中,那么 Hystrix 斷路器不會處于活動狀態,在服務啟動時,也不會收到任何警告或錯誤訊息;
2.4 在業務類上使用 @HystrixCommand 注解(斷路器模式)
在 service 包下的業務類里;方法級注解;
@HystrixCommand
private Xxx getXxx(String xxxId) {
return xxxService.getXxx(xxxId);
}
@HystrixCommand注解將動態生成一個代理,該代理將包裝該方法,并通過專門用于處理遠程呼叫的執行緒池來管理對該方法的所有呼叫;- 當呼叫時間超過 1000 ms 時,斷路器將中斷對 getXxx(),并拋出r如下例外:
- com.nextflix.hystrix.exception.HystrixRuntimeException;
2.5 定制斷路器(后備策略、艙壁策略)
- 默認情況下,不帶屬性配置的
@HystrixCommand注解,會將所有遠程服務呼叫都放在同一執行緒池下,可能會導致應用程式中出現問題;
@HystrixCommand(
fallbackMethod="getYyy", //【可選】艙壁策略,定義執行緒池的唯一名稱后備策略,如果 getXxx 呼叫失敗,那么就會呼叫該方法,注意兩個方法引數需保持一致
threadPoolKey="xxxThreadPool", //【可選】艙壁策略,定義執行緒池的唯一名稱艙壁策略,定義執行緒池的唯一名稱
threadPoolProperties={
@HystrixProperty(name="coreSize",value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/30"), //執行緒池中執行緒的最大數量
@HystrixProperty(name="maxQueueSize", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/10") //定義一個位于執行緒池前的佇列,對傳入的請求進行排隊
},
//通過 commandProperties 屬性來定制斷路器的行為
commandProperties={
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/10"), //斷路器觸發前 10s 內需要發生的連續呼叫數量
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/75"), //在斷路器跳閘之前必須達到的呼叫失敗百分比
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/7000"), //斷路器跳閘之后, Hystrix 允許一個呼叫通過以便查看服務是否恢復健康之前 Hystrix 的休眠時間
@HystrixProperty(name="metrics.rollingStats.timeInMilliseconds", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/15000"), //Hystr ix 用來監視服務呼叫問題的視窗大小,其默認值為 10 000ms
@HystrixProperty(name="metrics.rollingStats.numBuckets", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/5"), //定義的滾動視窗中收集統計資訊的次數
//@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/5000"), //設定斷路器超時時間
@HystrixProperty(name="execution.isolation.strategy", value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/SEMAPHORE"), //修改命令池的隔離設定
}
)
private Xxx getXxx(String xxxId) {
return xxxService.getXxx(xxxId);
}
private Yyy getYyy(String yyyId) {
return yyyService.getYyy(yyyId);
}
屬性詳解:
-
threadPoolProperties:
maxQueueSize:如果將其值設定為 -1,則將使用 Java SynchronousQueue 來保存所有傳人的請求,同步佇列本質上會強制要求正在處理中的請求數量永遠不能超過執行緒池中可用執行緒的數量;maxQueueSize:設定為大于 1 的值將使用 Java LinkedBlockingQueue;LinkedBlockingQueue 的使用允許開發人員即使所有執行緒都在忙于處理請求,也能對請求進行排隊;maxQueueSize屬性只能在執行緒池首次初始化時設定(例如,在應用程式啟動時),Hystrix 允許通過使用 queueSizeRejectionThreshold 屬性來動態更改佇列的大小,但只有在 maxQueueSize 屬性的值大于 0 時,才能設定此屬性;
-
commandProperties:
execution.isolation.thread.timeoutInMilliseconds:設定斷路器超時時間,在實際開發中應該把問題放在解決性能問題而不是增加默認超時,如果確實遇到一些比其他服務呼叫需要更長時間的服務呼叫,務必將這些服務呼叫隔離到單獨的執行緒池中;metrics.rollingStats.numBuckets:其值需要被metrics.rollingStats.timeInMilliseconds整除,上例 Hystrix 使用 15s 的視窗,并將統計資料收集到長度為 3 s 的 5 個桶中,查的統計視窗越小且在視窗中保留的桶的數量越多,就越會加劇高請求服務的 CPU 利用率和記憶體使用率;execution.isolation.strategy:斷路器執行時,有兩種不同的隔離策略;- THREAD(執行緒):默認,保護呼叫的每個 Hystrix命令都在一個單獨的執行緒池中運行;
- SEMAPHORE(信號量):輕量級隔離,適用于服務量很大且正在使用異步 l/O 編程模型(假設使用的是像 Netty 這樣的異步 IO 容器)運行的情況;
-
其他配置屬性詳情請見本篇《1.6 Hystrix 的所有配置》;
2.6 使用類級注解統一 Hystrix 配置
- @DefaultProperties:相當于修改該類下 Hystrix 配置的默認值;
@DefaultProperties(
commandProperties={
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="https://www.cnblogs.com/dlhjw/archive/2022/01/29/10000")}
class MyService{
...
}
3. 使用 HystrixConcurrencyStrategy 聯系執行緒背景關系
3.1 Hystrix 的背景關系隔離
- 默認情況下, Hystrix 以 THREAD 隔離策略運行;
- 這使每個 Hystrix 命令都在一個單獨的執行緒池中運行,該執行緒池不與父錢程共享它的背景關系;
- 因此默認情況下,對被父執行緒呼叫并由 @HystrixComman 保護的方法而言,在父執行緒中設定為 ThreadLocal 值的值都是不可用的;
- 解決方法:定義一種并發策略,能將附加的父執行緒背景關系注入由 由 Hystrix 命令管理的執行緒中;
3.2 自定義 Hystrix 井發策略類
//擴展基本的 Hystrix ConcurrencyStrategy 類
public class ThreadLocalAwareStrategy extends HystrixConcurrencyStrategy{
private HystrixConcurrencyStrategy existingConcurrencyStrategy;
//將已存在的并發策略傳入到構造器中
public ThreadLocalAwareStrategy(HystrixConcurrencyStrategy existingConcurrencyStrategy) {
this.existingConcurrencyStrategy = existingConcurrencyStrategy;
}
@Override
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
return existingConcurrencyStrategy != null
? existingConcurrencyStrategy.getBlockingQueue(maxQueueSize)
: super.getBlockingQueue(maxQueueSize);
}
@Override
public <T> HystrixRequestVariable<T> getRequestVariable(
HystrixRequestVariableLifecycle<T> rv) {
return existingConcurrencyStrategy != null
? existingConcurrencyStrategy.getRequestVariable(rv)
: super.getRequestVariable(rv);
}
@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
HystrixProperty<Integer> corePoolSize,
HystrixProperty<Integer> maximumPoolSize,
HystrixProperty<Integer> keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
return existingConcurrencyStrategy != null
? existingConcurrencyStrategy.getThreadPool(threadPoolKey, corePoolSize,
maximumPoolSize, keepAliveTime, unit, workQueue)
: super.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize,
keepAliveTime, unit, workQueue);
}
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
//注入 Callable 實作,用來設定 UserContext
return existingConcurrencyStrategy != null
? existingConcurrencyStrategy
.wrapCallable(new DelegatingUserContextCallable<T>(callable, UserContextHolder.getContext()))
: super.wrapCallable(new DelegatingUserContextCallable<T>(callable, UserContextHolder.getContext()));
}
}
3.3 定義一個 Java Callable 類,將 UserContext 注入 Hystrix 命令中
public final class DelegatingUserContextCallable<V> implements Callable<V> {
private final Callable<V> delegate;
private UserContext originalUserContext;
//傳遞原始 Callable 類,自定義 Callable 將呼叫 Hystrix 保護的代碼與來自父執行緒的 UserContext
public DelegatingUserContextCallable(Callable<V> delegate,
UserContext userContext) {
this.delegate = delegate;
this.originalUserContext = userContext;
}
//call() 方法在被 @HystrixCommand 注解保護的方法之前呼叫
public V call() throws Exception {
//已設定 UserContext,存盤 UserContext 的 ThreadLocal 變數與運行受 Hystrix 保護的方法的執行緒相關聯
UserContextHolder.setContext( originalUserContext );
try {
//在 Hystrix 保護的方法上呼叫 call() 方法
return delegate.call();
}
finally {
this.originalUserContext = null;
}
}
public static <V> Callable<V> create(Callable<V> delegate,
UserContext userContext) {
return new DelegatingUserContextCallable<V>(delegate, userContext);
}
}
3.4 配置 Spring Cloud 以使用自定義 Hystrix 井發策略
@Configuration
public class ThreadLocalConfiguration {
//當構造配置物件時,它將自動裝配在現有的 HystrixConcurrencyStrategy 中
@Autowired(required = false)
private HystrixConcurrencyStrategy existingConcurrencyStrategy;
@PostConstruct
public void init() {
// 保留現有的 Hystrix 插件的參考
//因為要注冊一個新的并發策略,所以要獲取所有其他的 Hystrix 組件,然后重新設定 Hystrix 組件
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance()
.getEventNotifier();
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance()
.getMetricsPublisher();
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance()
.getPropertiesStrategy();
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance()
.getCommandExecutionHook();
HystrixPlugins.reset();
//使用 Hystrix 插件注冊自定義的 Hystrix 并發策略(ThreadConcurrency Strategy)
HystrixPlugins.getInstance().registerConcurrencyStrategy(new ThreadLocalAwareStrategy(existingConcurrencyStrategy));
//重新注冊 Hystrix 插件使用的所有組件
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
}
}
4. 使用 hystrixDashboard 實作服務監控
- Hystrix 提供了準實時的呼叫監控(Hystrix Dashboard),其會持續地記錄所有通過Hystrix發起的請求的執行資訊,并以統計報表和圖形的形式展示給用戶,包括每秒執行多少請求多少成功,多少失敗等;
- Netflix通過 hystrix-metrics-event-stream 專案實作了對以上指標的監控;
- Spring Cloud 也提供了 Hystrix Dashboard 的整合,對監控內容轉化成可視化界面;
4.1 引入 pom.xml 依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
4.2 修改 application.yml 組態檔
主要是修改埠號;
server:
port: 9001
4.3 在主程式類上標注注解
@EnableHystrixDashboard:表名啟用 Hystrix Dashboard 對呼叫進行監控;
4.4 配置被監控的服務
1. 添加 pom.xml 依賴檔案
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. 在主啟動類中指定監控路徑
@SpringBootApplication
@EnableEurekaClient //本服務啟動后會自動注冊進eureka服務中
@EnableCircuitBreaker//對hystrixR熔斷機制的支持
public class Application{
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
/**
*此配置是為了服務監控而配置,與服務容錯本身無關,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;
}
4.5 訪問圖形化界面
- 使用介面:http://localhost:9001/hystrix

4.6 查看監控圖
- 監控圖示例:

- 監控圖圖例解釋:


- 實心圈:有兩種含義:
- 通過顏色的變化代表了實體的健康程度,它的健康度從:綠色 < 黃色 < 橙色 < 紅色 遞減;
- 其大小也會根據實體的請求流量發生變化,流量越大該實心圓就越大;
- 所以通過該實心圓的展示,就可以在大量的實體中快速的發現故障實體和高壓力實體;
- 曲線:
- 用來記錄2分鐘內流量的相對變化,可以通過它來觀察到流量的上升和下降趨勢;
最后

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/423146.html
標籤:其他
上一篇:HTTP狀態碼1XX深入理解
