這是一個從零開始的springcloud的系列教程,如果你從中間開始看,可能會看不明白.請進(jìn)入我的系列教程開始從頭開始學(xué)習(xí).spring-cloud教程
Hytrix-Dashboard監(jiān)控
在微服務(wù)架構(gòu)中為例保證程序的可用性,防止程序出錯導(dǎo)致網(wǎng)絡(luò)阻塞,出現(xiàn)了斷路器模型。斷路器的狀況反應(yīng)了一個程序的可用性和健壯性,它是一個重要指標(biāo)。Hystrix Dashboard是作為斷路器狀態(tài)的一個組件,提供了數(shù)據(jù)監(jiān)控和友好的圖形化界面。
沿用上節(jié)課創(chuàng)建eureka-client-feign工程,修改eureka-client-feign工程.
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">
<parent>
<artifactId>spring-cloud-learn</artifactId>
<groupId>com.jack</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-client-feign</artifactId>
<dependencies>
<!--
一定要寫稱spring-cloud-starter-netflix-eureka-client
如果寫錯成spring-cloud-netflix-eureka-client,將無法注冊
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入hytrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--引入hytrix dashboard-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--
spring-boot-starter-actuator
springboot的Actuator提供了運(yùn)行狀態(tài)監(jiān)控的功能,可以通過REST、遠(yuǎn)程Shell和JMX方式來查看
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
修改EurekaClientFeignApplication
package com.jack.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
// 啟動feign的注解
@EnableFeignClients
// 啟動dashboard
@EnableHystrixDashboard
// 啟動hystrix
@EnableHystrix
public class EurekaClientFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientFeignApplication.class, args );
}
}
application.yml需要加入以下配置,非常關(guān)鍵,因?yàn)閿?shù)據(jù)是通過springboot的actuator來持續(xù)返回的,我們需要讓actuator開放節(jié)點(diǎn)給我們,它默認(rèn)開放health和info來個節(jié)點(diǎn)
# springboot的Actuator提供了運(yùn)行狀態(tài)監(jiān)控的功能,可以通過REST、遠(yuǎn)程Shell和JMX方式來查看。
# 這個配置非常重要,否則會導(dǎo)致/actuator/hystrix.stream無法訪問,hystrix.stream節(jié)點(diǎn)會持續(xù)提供熔斷狀態(tài)
management:
endpoints:
web:
# 2.0之前默認(rèn)是/ 2.0默認(rèn)是 /actuator
# base-path: /actuator
# 顯示健康具體信息 默認(rèn)不會顯示詳細(xì)信息
# management.endpoint.health.show-details=always
exposure:
# 默認(rèn)開啟health、info,可以通過/actuator/info訪問,如果要暴露所有接口,填"*"
include: hystrix.stream
cors:
allowed-origins: "*"
allowed-methods: "*"
ok,啟動服務(wù),訪問http://localhost:7773/actuator/hystrix.stream,會發(fā)現(xiàn)不斷有數(shù)據(jù)返回,不過因?yàn)闆]有訪問api,返回都是空

當(dāng)我們訪問http://localhost:7773/hello_store api (eureka-client服務(wù)沒有啟動)
此時服務(wù)就會出現(xiàn)數(shù)據(jù)

我們來打開圖形界面控制面板,http://localhost:7773/hystrix

對著中間輸入我們運(yùn)行狀態(tài)流服務(wù)節(jié)點(diǎn)http://localhost:7773/actuator/hystrix.stream

點(diǎn)擊monitor stream.觀察流.并且請求一次/hello_store服務(wù).會發(fā)現(xiàn)出現(xiàn)一次錯誤.方法為FeignService#sayStore

這樣我們就可以通過dashboard來觀察熔斷器的狀態(tài)了.
但是這個遠(yuǎn)遠(yuǎn)是不夠的,只能觀察一臺機(jī)器,并沒有什么意義.接下來我們會介紹turbine.可以收集所有服務(wù)熔斷器狀態(tài)流