SpringCloud Hystrix-Dashboard儀表盤的實現(xiàn)
Hystrix Dashboard,它主要用來實時監(jiān)控Hystrix的各項指標(biāo)信息。通過Hystrix Dashboard反饋的實時信息,可以幫助我們快速發(fā)現(xiàn)系統(tǒng)中存在的問題。下面通過一個例子來學(xué)習(xí)。
一、新建一個Spring Cloud 項目,命名為hystrix-dashboard
1.1在pom.xml引入相關(guān)的依賴
<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啟動應(yīng)用,然后再瀏覽器中輸入http://localhost:2001/hystrix可以看到如下界面

通過Hystrix Dashboard主頁面的文字介紹,我們可以知道,Hystrix Dashboard共支持三種不同的監(jiān)控方式
☞默認(rèn)的集群監(jiān)控:通過URL:http://turbine-hostname:port/turbine.stream開啟,實現(xiàn)對默認(rèn)集群的監(jiān)控。
☞指定的集群監(jiān)控:通過URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]開啟,實現(xiàn)對clusterName集群的監(jiān)控。
☞單體應(yīng)用的監(jiān)控:通過URL:http://hystrix-app:port/hystrix.stream開啟,實現(xiàn)對具體某個服務(wù)實例的監(jiān)控。
☞Delay:控制服務(wù)器上輪詢監(jiān)控信息的延遲時間,默認(rèn)為2000毫秒,可以通過配置該屬性來降低客戶端的網(wǎng)絡(luò)和CPU消耗。
☞Title:該參數(shù)可以展示合適的標(biāo)題。
二、要有一個eureka-server用來提供eureka的服務(wù)注冊中心,在碼云上有,可以作為參考。此處不再粘代碼。
三、要有一個eureka-service來提供服務(wù),工程名為hello-service,項目地址同上。
四、新建一個服務(wù)被監(jiān)控的工程,工程名為ribbon-customer。
4.1pom.xml引入相關(guān)依賴
<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
通過上面的步驟,已經(jīng)基本完成了準(zhǔn)備工作,下面我們進(jìn)行測試。
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按鈕

在監(jiān)控的界面有兩個重要的圖形信息:一個實心圓和一條曲線。
- 實心圓:1、通過顏色的變化代表了實例的健康程度,健康程度從綠色、黃色、橙色、紅色遞減。2、通過大小表示請求流量發(fā)生變化,流量越大該實心圓就越大。所以可以在大量的實例中快速發(fā)現(xiàn)故障實例和高壓實例。
- 曲線:用來記錄2分鐘內(nèi)流浪的相對變化,可以通過它來觀察流量的上升和下降趨勢。
注意:當(dāng)使用Hystrix Board來監(jiān)控Spring Cloud Zuul構(gòu)建的API網(wǎng)關(guān)時,Thread Pool信息會一直處于Loading狀態(tài)。這是由于Zuul默認(rèn)會使用信號量來實現(xiàn)隔離,只有通過Hystrix配置把隔離機(jī)制改成為線程池的方式才能夠得以展示。
參考:
[1]《Spring Cloud微服務(wù)實戰(zhàn)》,翟永超
[2]博客,純潔的微笑,http://www.dbjr.com.cn/article/167053.htm
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
模仿mybatis-plus實現(xiàn)rpc調(diào)用
這篇文章主要為大家介紹了模仿mybatis-plus實現(xiàn)rpc調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Java泛型extends關(guān)鍵字設(shè)置邊界的實現(xiàn)
這篇文章主要介紹了Java泛型extends關(guān)鍵字設(shè)置邊界的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
springboot整合druid及多數(shù)據(jù)源配置的demo
這篇文章主要介紹了springboot整合druid及多數(shù)據(jù)源配置的demo,本篇主要分兩部分 ①springboot整合druid的代碼配置,以及druid的監(jiān)控頁面演示;②對實際場景中多數(shù)據(jù)源的配置使用進(jìn)行講解,需要的朋友可以參考下2024-01-01
Mybatis 中Mapper使用package方式配置報錯的解決方案
這篇文章主要介紹了Mybatis 中Mapper使用package方式配置報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

