Hystrix除了可以對不可用的服務進行斷路隔離外,還能夠對服務進行實時監控。Hystrix可以實時、累加地記錄所有關於HystrixCommand的執行信息,包括每秒執行多少、請求成功多少、失敗多少等。
要想實時地對服務進行監控,需要在項目中添加相關的監控依賴,具體如下:
(1)分別啟動注冊中心、服務提供者(7901)和服務消費者工程。
(2)通過瀏覽器訪問地址http://localhost:8030/findOrder-sByUser/1(此步驟不可省略,否則將由於系統應用的所有接口都未被調用,而只輸出ping:)。
(3)通過瀏覽器訪問地址http://localhost:8030/hystrix.stream,將看到如下所示的輸出信息。
為此,我們可以通過Hystrix Dashboard以可視化的方式來查看實時監控數據。Hystrix Dashboard是Hystrix的一個組件,它提供了數據監控和友好的圖形化界面支持。
下面通過一個具體的應用來演示Hystrix Dashboard的使用。
(1)新建xcservice-springcloud的子工程xcservice-hystrix-dashboard,在其pom.xml文件中添加監控依賴和Hystrix Dashboard依賴,如文件5-5所示。
文件5-5 pom.xml
文件5-6 application.yml
文件5-7 Application.java
要想實時地對服務進行監控,需要在項目中添加相關的監控依賴,具體如下:
<dependency><!--監控依賴--><!--http://localhost:8030/hystrix.stream--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
在xcservice-eureka-user-hystrix工程的pom.xml中引入上述依賴后,即可查看監控信息,具體操作步驟如下。
(1)分別啟動注冊中心、服務提供者(7901)和服務消費者工程。
(2)通過瀏覽器訪問地址http://localhost:8030/findOrder-sByUser/1(此步驟不可省略,否則將由於系統應用的所有接口都未被調用,而只輸出ping:)。
(3)通過瀏覽器訪問地址http://localhost:8030/hystrix.stream,將看到如下所示的輸出信息。
ping:
data: {"type":"HystrixCommand","name":"findOrdersByUser","group":"UserController","currentTime":1563192973047,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"UserController"}
data: {"type":"HystrixThreadPool","name":"UserController","currentTime":1563192973047,"currentActiveCount":0,"currentCompletedTaskCount":6,"currentCorePoolSize":10,"currentLargestPoolSize":6,"currentMaximumPoolSize":10,"currentPoolSize":6,"currentQueueSize":0,"currentTaskCount":6,"rollingCountThreadsExecuted":0,"rollingMaxActiveThreads":0,"rollingCountCommandRejections":0,"propertyValue_queueSizeRejectionThreshold":5,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"reportingHosts":1}
為此,我們可以通過Hystrix Dashboard以可視化的方式來查看實時監控數據。Hystrix Dashboard是Hystrix的一個組件,它提供了數據監控和友好的圖形化界面支持。
下面通過一個具體的應用來演示Hystrix Dashboard的使用。
(1)新建xcservice-springcloud的子工程xcservice-hystrix-dashboard,在其pom.xml文件中添加監控依賴和Hystrix Dashboard依賴,如文件5-5所示。
文件5-5 pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xc</groupId> <artifactId>xcservice-springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.xc</groupId> <artifactId>xcservice-hystrix-dashboard</artifactId> <version>0.0.1-SNAPSHOT</version> <name>xcservice-hystrix-dashboard</name> <description>Hystrix Dashboard是Hystrix的一個組件,它提供了數據監控和友好的圖形化界面支持。</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency><!--數據監控和友好的圖形化界面支持--><!--http://localhost:8031/hystrix.stream--> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency><!--監控依賴--><!--http://localhost:8030/hystrix.stream--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(2)編寫配置文件application.yml,指定應用的端口號和名稱等信息,如文件5-6所示。
文件5-6 application.yml
server:
port: 8031 # 指定該Eureka實例的端口號
spring:
application:
name: xcservice-hystrix-dashboard # 指定應用名稱
(3)編寫啟動類Application.java,並在其類上添加@EnableHystrixDashboard注解來開啟Hystrix儀表板功能,如文件5-7所示。
文件5-7 Application.java
package com.xc.xcservicehystrixdashboard; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableHystrixDashboard public class XcserviceHystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(XcserviceHystrixDashboardApplication.class, args); } }
(4)啟動工程后,通過瀏覽器訪問地址http://local-host:8031/hystrix.stream將會看到如圖5-8所示的信息。


在圖5-8中,Hystrix Dashboard下的輸入框用於輸入需要監控的服務URL,Delay中的參數表示服務器上的輪詢時間間隔,Title中的輸入框用於設置瀏覽器中所監視服務的標題。
在Hystrix Dashboard下的輸入框中輸入http://localhost:8030/hystrix.stream,並設置Title為“訂單微服務”后,單擊【Monitor Stream】按鈕。
此時如果通過另一個瀏覽器訪問http://localhost:8030/findOrdersByUser/1,並且不斷地刷新地址,將出現如下所示頁面。
在Hystrix Dashboard下的輸入框中輸入http://localhost:8030/hystrix.stream,並設置Title為“訂單微服務”后,單擊【Monitor Stream】按鈕。
此時如果通過另一個瀏覽器訪問http://localhost:8030/findOrdersByUser/1,並且不斷地刷新地址,將出現如下所示頁面。

關於圖中各個指標的含義,在Hystrix Dashboard Wiki上已經給出了詳細說明,讀者可訪問地址https://github.com/Netflix/Hystrix/wiki/Dashboard查看。