SpringCloud Hystrix Dashboard

SpringCloud Hystrix Dashboard

歡迎關注微信公衆號:程序員小圈圈
原文首發於:www.zhangruibin.com
本文出自於:RebornChang的博客
轉載請標明出處^_^

SpringCloud Hystrix Dashboard是什麼

除了上節中提到的熔斷機制,Hystrix還提供了準實時的調用監控(Hystrix Dashboard),Hystrix會持續地記錄所有通過Hystrix發起的請求的執行信息,並以統計報表和圖形的形式展示給用戶,包括每秒執行多少請求多少成功,多少失敗等。

SpringCloud Hystrix Dashboard怎麼用

新建一個dashboard model項目

項目名稱:springclouddemo-06-hystrix-dashboard-7000
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>springcloud-demo-01</artifactId>
        <groupId>com.zrb.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springclouddemo-06-hystrix-dashboard-7000</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.zrb.springcloud</groupId>
            <artifactId>springcloud-02-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--導入 hystrix 與 hystrix-dashboard 依賴-->
  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!-- 添加對hystrix斷路器的依賴-->
  <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
            <version>1.4.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

新建application.yml文件

application.yml

server:
 port: 7000

eureka:
 client: 
   serviceUrl: 
     defaultZone: http://127.0.0.1:9000/eureka/,http://127.0.0.1:9002/eureka/
spring:
 application: 
   name: hystrix-dashboard

新建啓動類HystrixDashboard_7000

HystrixDashboard_7000.java

package com.zhrb.springcloud;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
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.context.annotation.Bean;

/**
 * @ClassName HystrixDashboard_7000
 * @Description TODO
  * @Author zhrb
 * @Date 2019/9/10 10:47
 * @Version
  */ 
@EnableHystrix
@EnableDiscoveryClient @EnableHystrixDashboard @SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class HystrixDashboard_7000 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboard_7000.class,args);
    }
    @Bean
  public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

然後修改一個之前的項目,將其納入監控,例如8011項目:
首先確定8011項目有pom依賴:

<!-- hystrix --> <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 添加對hystrix斷路器的依賴--> <dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-core</artifactId>
    <version>1.5.6</version>
</dependency>
<!--hystrix監控--> <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然後修改application.yml
增加:

# 在被監控服務上添加暴露端點 management:
 endpoints: 
 web: 
 exposure: 
 include: hystrix.stream

流程原理

1.hystrix讓哪個項目納入監控就將項目增加依賴及配置如8011;
2.然後用戶正常訪問時,hystrix就可以去ping接口訪問路徑,然後獲取返回值狀態等信息,通過訪問8011hystrix監控頁面可以看到json格式的數據顯示;
3.完成上面兩步之後,可以再hystrix-dashboard項目中,配置指定的8011hystrix json 格式信息訪問路徑,然後hystrix-dashboard項目會將json信息,轉化成視圖儀表盤格式展示。

效果

通過上面的流程原理簡單介紹,我們此時可以依次來驗證效果:

springclouddemo-06-hystrix-dashboard-7000

先啓動 springclouddemo-06-hystrix-dashboard-7000 項目,驗證下項目啓動訪問:http://localhost:7000/hystrix
頁面顯示如下圖所示:

啓動EurekaServer等項目驗證

啓動順序:9000->9002->8001->8011
然後多次訪問:http://localhost:8011/product/1
然後訪問:http://localhost:8011/actuator/hystrix.stream
我們可以看到json型數據如下:

然後我們來將這個json格式數據地址納入dashboard監控,訪問:http://localhost:7000/hystrix
並且填寫相關信息如下圖所示:

第一個輸入框填寫監控地址: http://localhost:8011/actuator/hystrix.stream

Delay:控制服務器上輪詢監控信息的延遲時間,默認爲2000毫秒,可以通過配置該屬性來降低客
戶端的網絡和CPU消耗。

Title:可以通過配置該信息來展示更合適的標題,默認會使用具體監控實例的URL

監控結果:
如果沒有請求會一直顯示 “Loading…”

這時訪問一下 http://localhost:8001/product/

然後再多次訪問:http://localhost:8011/product/1

在儀表盤界面我們就可以看到圖形化監控了,類似如下:

常見錯誤
報: Unable to connect to Command Metric Stream
解決方式:
看被監控服務中是否添加 spring-cloud-starter-netflix-hystrix 與spring-boot-starter-actuato依賴,並且暴露的端
點(要納入監控的端點)是否有配置: management.endpoints.web.exposure.include=hystrix.stream
後記:一定要注意springboot及springcloud的版本,之前博主最初用hystrix-dashboard的時候路徑填寫沒有/actuator。

後續還會有其他組件的使用入門介紹,有興趣的小夥伴可以關注公衆號的推送哦~

,博主的微信公衆號
程序員小圈圈’開始持續更新了喲~~
識別二維碼或者直接搜索名字 ‘程序員小圈圈’ 即可關注本公衆號喲~~
不只是有技術喲~~
還有很多的學習娛樂資源免費下載哦~~
還可以學下教育知識以及消遣娛樂喲~~
求關注喲~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章