Hystrix Dashboard是什么:
Hystrix提供了對於微服務調用狀態的監控信息,但是需要結合spring-boot-actuator模塊一起使用。Hystrix Dashboard是Hystrix的一個組件,Hystrix Dashboard提供一個斷路器的監控面板,可以使我們更好的監控服務和集群的狀態,僅僅使用Hystrix Dashboard只能監控到單個斷路器的狀態,實際開發中還需要結合Turbine使用。
Hystrix Dashboard作用:
Hystrix Dashboard主要用來實時監控Hystrix的各項指標信息。通過Hystrix Dashboard反饋的實時信息,可以幫助我們快速發現系統中存在的問題。
Hystrix Dashboard使用:
使用基於Hystrix的提供者訪問數據庫表數據,每訪問一次都會記錄是否成功以及最近10s錯誤百分比、超時數、熔斷數、線程拒絕數、錯誤請求數、失敗/異常數、服務請求頻率等相關信息
Hystrix Dashboard監控平台:
創建Hystrix Dashboard模塊
創建application.yml配置文件
創建主啟動類
1. 創建名為microService-hystrix-dashboard-9001的Maven項目
2. 配置pom.xml
<dependencies> <!--依賴關系--> <dependency> <groupId>cn.zf</groupId> <artifactId>microService-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--監聽方--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
3. 創建application.yml文件
server:
port: 9001
4. 創建主啟動類
package cn.zf.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashBoard_9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashBoard_9001.class,args);
}
}
5. 測試
首頁中並沒有具體的監控信息,從頁面上的內容可以知道,Hystrix Dashboard共支持三種不同的監控方式:
默認的集群監控: http://turbine-hostname:port/turbine.stream
指定的集群監控: http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
單體應用的監控: http://hystrix-app:port/actuator/hystrix.stream
頁面上面的幾個參數局域
最上面的輸入框: 輸入上面所說的三種監控方式的地址,用於訪問具體的監控信息頁面。
Delay: 該參數用來控制服務器上輪詢監控信息的延遲時間,默認2000毫秒。
Title: 該參數對應頭部標題Hystrix Stream之后的內容,默認會使用具體監控實例的Url。
Hystrix Dashboard監控平台的搭建:
監控平台:使用上面的監控平台
被監控方:服務提供者。這里接微服務之Hystrix熔斷器那期代碼
在microService-provider-hystrix-8002項目中的pom.xml添加新的依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--添加熔斷器依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在application.yml文件中添加暴露點:
# 在被監控的服務上添加暴露點
management:
endpoints:
web:
exposure:
include: hystrix.stream
# include: '*' #'*'代表開放所有端點。
啟動eureka注冊中心、服務提供者、服務注冊者,瀏覽器訪問http://localhost:8002/actuator/hystrix.stream出現以下頁面,因為監控的實例本身還沒有調用任何服務,所以監控端點也沒記錄任何信息:
訪問一下服務:
訪問后回到剛才的頁面點擊刷新,有數據返回,埋點生效。
從瀏覽器中的信息可以看出這些信息是剛剛請求微服務時所記錄的監控信息,但是直接去看這些信息肯定是不友好的,所以Hystrix Dashboard就派上用場了。