Hystrix Dashboard,它主要用來實時監控Hystrix的各項指標信息。通過Hystrix Dashboard反饋的實時信息,可以幫助我們快速發現系統中存在的問題。下面通過一個例子來學習。
一、新建一個Spring Cloud 項目,命名為hystrix-dashboard
1.1在pom.xml引入相關的依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
1.2在spring boot 的啟動類上面引入注解@EnableHystrixDashboard,啟用Hystrix Dashboard功能。
package org.hope.hystrix.dashboard; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @EnableHystrixDashboard @SpringCloudApplication public class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
1.3修改配置文件application.properties
spring.application.name=hystrix-dashboard
server.port=2001
1.4啟動應用,然后再瀏覽器中輸入http://localhost:2001/hystrix可以看到如下界面
通過Hystrix Dashboard主頁面的文字介紹,我們可以知道,Hystrix Dashboard共支持三種不同的監控方式
☞默認的集群監控:通過URL:http://turbine-hostname:port/turbine.stream開啟,實現對默認集群的監控。
☞指定的集群監控:通過URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啟,實現對clusterName集群的監控。
☞單體應用的監控:通過URL:http://hystrix-app:port/hystrix.stream開啟,實現對具體某個服務實例的監控。
☞Delay:控制服務器上輪詢監控信息的延遲時間,默認為2000毫秒,可以通過配置該屬性來降低客戶端的網絡和CPU消耗。
☞Title:該參數可以展示合適的標題。
二、要有一個eureka-server用來提供eureka的服務注冊中心,在碼雲上有,可以作為參考。此處不再粘代碼。
三、要有一個eureka-service來提供服務,工程名為hello-service,項目地址同上。
四、新建一個服務被監控的工程,工程名為ribbon-customer。
4.1pom.xml引入相關依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
4.2在啟動類上添加@EnableCircuitBreaker 開啟斷路器功能
package com.didispace; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableCircuitBreaker //開啟斷路器功能 @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
4.3 RestController
package com.didispace.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class ConsumerController { @Autowired HelloService helloService; @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET) public String helloConsumer() { return helloService.hello(); } }
4.4 application.properties配置文件
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
通過上面的步驟,已經基本完成了准備工作,下面我們進行測試。
1.啟動eureka-server
2.啟動hello-service
3.啟動ribbon-customer
4.啟動hystrix-dashboard
5.在瀏覽器輸入http://localhost:2001/hystrix
6.在瀏覽器的新窗口輸入http://localhost:9000/ribbon-consumer
7.在Hystrix-Dashboard的主界面上輸入: http://localhost:9000/hystrix.stream然后點擊 Monitor Stream按鈕
在監控的界面有兩個重要的圖形信息:一個實心圓和一條曲線。
▪實心圓:1、通過顏色的變化代表了實例的健康程度,健康程度從綠色、黃色、橙色、紅色遞減。2、通過大小表示請求流量發生變化,流量越大該實心圓就越大。所以可以在大量的實例中快速發現故障實例和高壓實例。
▪曲線:用來記錄2分鍾內流浪的相對變化,可以通過它來觀察流量的上升和下降趨勢。
注意:當使用Hystrix Board來監控Spring Cloud Zuul構建的API網關時,Thread Pool信息會一直處於Loading狀態。這是由於Zuul默認會使用信號量來實現隔離,只有通過Hystrix配置把隔離機制改成為線程池的方式才能夠得以展示。
參考:
[1]《Spring Cloud微服務實戰》,翟永超
[2]博客,純潔的微笑,http://blog.csdn.net/ityouknow/article/details/72625646