公衆號: java樂園java
在微服務架構中如何去監控服務的狀態是否正常,很是重要。爲例保證微服務的可用性,防止程序斷路器模型。運維人員須要一個友好的程序來監控微服務的狀態, Hystrix Dashboard做爲斷路器監控的一個重要組件,提供了數據監控及很是友好的圖形化界面,方便運維人員對服務進行監控;,經過界面反饋的信息能夠快速發現系統中存在的問題。另外Hystrix Dashboard是一個獨立的服務結點,不須要配置任何的註冊中心。web
一、 新建項目sc-hystrix-dashboard,對用的pom.xml文件以下spring
<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> <groupId>spring-cloud</groupId> <artifactId>sc-hystrix-dashboard</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-hystrix-dashboard</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.5.RELEASE</version> </dependency> --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.5.RELEASE</version> </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> </dependencies> </project>
說明:spring cloud 2.x以後spring-cloud-starter-hystrix-dashboard和spring-cloud-starter-hystrix都標誌爲過時,具體推薦使用兩個依賴請看下面的圖片apache
二、 新建spring boot啓動類DashboardApplication.javabootstrap
package sc.hystrix.dashboard; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableHystrixDashboard public class DashboardApplication { public static void main(String[] args) { SpringApplication.run(DashboardApplication.class, args); } }
只需添加註解EnableHystrixDashboard,啓動Hystrix Dashboard便可架構
三、 配置Hystrix Dashboard對應的Servletapp
package sc.hystrix.dashboard; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; @Configuration public class DashboardServletConfig { @Bean public ServletRegistrationBean getDashboardServlet(){ HystrixMetricsStreamServlet servlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean bean = new ServletRegistrationBean(servlet); bean.setLoadOnStartup(1); bean.addUrlMappings("/hystrix.stream"); bean.setName("hystrix"); return bean; } }
四、 新建配置文件bootstrap.yml運維
spring: application: name: sc-hystrix-dashboard server: port: 2001
五、 啓動並驗證是否啓動成功
方法一:查看日誌配置的端口2001,在日誌中標誌啓動成功maven
方法二:從上圖標誌的1中能夠知道能夠方法地址:http://127.0.0.1:2001/hystrixspring-boot
六、 分析一下下圖中的英文
大概的意思是:若是查看默認集羣使用第一個url,查看指定集羣使用第二個url,單個應用監控使用最後一個
七、 在url中輸入http://127.0.0.1:2001/hystrix.stream
而後點擊Monitor Stream按鈕
或者訪問http://localhost:2001/hystrix.stream也會不斷的顯示ping