微服務學習之Hystrix圖形化DashBoard監控【Hoxton.SR1版】

目錄

 

1 前言

2 新建module

2.1 pom.xml

2.2 application.yml

2.3 啓動類

2.4 啓動項目

2.5 監控


1 前言

除了隔離依賴服務的調用以外,Hystrix還提供了準實時調用監控(Hystrix Dashboard),Hystrix會持續記錄所有通過Hystrix發起請求的執行信息,並以統計報表和圖形的形式展示給用戶,包括每秒執行多少請求,有多少是成功的,有多少是失敗的。

Netflix通過hystrix-metrics-event-stream項目實現了對以上項目的監控。SpringCloud提供了對Hystrix Dashboard的整合,將監控內容轉換爲可視化頁面。

2 新建module

2.1 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>cloud2020</artifactId>
        <groupId>com.bighuan.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>

    <dependencies>
        <!--hystrix-dashboard-->
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.bighuan.springcloud.HystrixDashboardMain9001</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

2.2 application.yml

只指定端口爲9001,沒有其他配置。

2.3 啓動類

通過@EnableHystrixDashboard開啓Hystrix Dashboard

@SpringBootApplication
@EnableHystrixDashboard // 開啓hystrix dashboard
public class HystrixDashboardMain9001 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardMain9001.class, args);
    }
}

2.4 啓動項目

訪問hystrix:http://127.0.0.1:9001/hystrix

2.5 監控

1)在新版本Hystrix監控的項目主啓動類中需要指定監控路徑,否則會報錯。如下被監控服務的主啓動類:

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain8001.class, args);
    }

    /**
     * 此配置是爲了hystrix服務監控而配置,與服務容錯本身無關,SpringCloud升級後的坑
     *ServletRegistrationBean因爲SpringBoot的默認路徑不是/hystrix.stream
     * 只要在自己的項目上配置好下面的servlet即可
     */
    @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;
    }

}

啓動被監控的服務類,在Hystrix Dashboard頁面進行監控:

點擊Monitor Stream,進入信息監控頁面(並訪問被監控的8001項目):

說明:

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