【Spring Cloud 筆記和總結】五、 Hystrix Dashboard和Turbine監控

一、Hystrix Dashboard監控

  • 涉及項目

    • service-consumer-hystrix => 基於service-consumer修改
  • 依賴

    <?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">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.lxt</groupId>
            <artifactId>springcloud</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <groupId>com.lxt</groupId>
        <artifactId>service-consumer-hystrix</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>service-consumer-hystrix</name>
        <description>Demo project for Spring Boot</description>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </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-openfeign</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    
    
    
  • 配置文件

    spring:
      application:
        name: spring-cloud-consumer-hystrix
    server:
      port: 9004
    feign:
      hystrix:
        enabled: true
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8000/eureka/
    
  • 啓動類添加註解,注意getServlet()方法,用於解決spring cloud2 hystrix沒有hystrix.stream路徑

    package com.lxt.springcloudconsumerhystrix;
    
    import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients
    @EnableHystrixDashboard
    @EnableCircuitBreaker
    public class SpringCloudConsumerHystrixApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringCloudConsumerHystrixApplication.class, args);
        }
    
        /**
         * 解決:找不到/hystrix.stream 報錯:=>Unable to connect to Command Metric Stream.
         * @return
         */
        @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;
        }
    }
    
    
    
  • 啓動項目,瀏覽器輸入http://localhost:9004/hystrix,如下:
    在這裏插入圖片描述

    • 監控默認集羣:http://turbine-hostname:port/turbine.stream
    • 監控指定集羣:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
    • 監控的那個應用:http://hystrix-app:port/hystrix.stream
  • 輸入http://localhost:9004/hystrix.stream進入監控頁面,此時頁面顯示Loading
    在這裏插入圖片描述
    在這裏插入圖片描述

  • 瀏覽器訪問http://localhost:9004/hello/lxt之後,監控頁面如下
    在這裏插入圖片描述

  • 圖中參數解釋如下
    界面解讀
    在這裏插入圖片描述

實心圓:它有顏色和大小之分,分別代表實例的監控程度和流量大小。如上圖所示,它的健康度從綠色、黃色、橙色、紅色遞減。通過該實心圓的展示,我們就可以在大量的實例中快速的發現故障實例和高壓力實例。

曲線:用來記錄 2 分鐘內流量的相對變化,我們可以通過它來觀察到流量的上升和下降趨勢。
其他一些數量指標如下圖所示
在這裏插入圖片描述

  • 測試成功

二、Turbine集羣監控

  • 涉及項目
    • hystrix-dashboard-turbine
    • service-consumer-node01 => 基於service-consumer-hystrix
    • service-consumer-node02 => 基於service-consumer-hystrix

hystrix-dashboard-turbine項目添加

  • 依賴

    <?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">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.lxt</groupId>
            <artifactId>springcloud</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <groupId>com.lxt</groupId>
        <artifactId>hystrix-dashboard-turbine</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>hystrix-dashboard-turbine</name>
        <description>Demo project for Spring Boot</description>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </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-netflix-turbine</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    
    
  • 配置文件

    spring:
      application:
        name: turbine-client
    server:
      port: 9009
    turbine:
      # 需要監控的應用名稱,默認逗號隔開,內部使用Stringutils.commaDelimitedListToStringArray分割
      app-config: hystrix-client1,hystrix-client2
      aggregator:
        cluster-config: default
      # 集羣名稱
      cluster-name-expression: new String("default")
      combine-host-port: true
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8000/eureka/
      instance:
        # 啓用ip配置 這樣在註冊中心列表中看見的是以ip+端口呈現的
        prefer-ip-address: true
        # 實例名稱  最後呈現地址:ip:2000
        instance-id: ${spring.cloud.client.ip-address}:${server.port}
    
    
  • 啓動類

    package com.lxt.hystrixdashboardturbine;
    
    import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    import org.springframework.cloud.netflix.turbine.EnableTurbine;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    @EnableHystrixDashboard
    @EnableTurbine
    public class HystrixDashboardTurbineApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HystrixDashboardTurbineApplication.class, args);
        }
    
        /**
         * @return
         */
        @Bean
        public ServletRegistrationBean getServlet() {
            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
            registrationBean.setLoadOnStartup(1);
            registrationBean.addUrlMappings("/actuator/hystrix.stream");
            registrationBean.setName("HystrixMetricsStreamServlet");
            return registrationBean;
        }
    }
    

service-consumer-node01/02的依賴、代碼和service-consumer-hystrix一樣,區別在於一個配置,如下

  • 配置文件,關鍵點management.endpoints.web.exposure.include:hystrix.stream
    spring:
      application:
        name: hystrix-client1
    server:
      port: 9005
    
    feign:
      hystrix:
        enabled: true
    management:
      endpoints:
        web:
          exposure:
            # 2.x手動開啓  這個是用來暴露 endpoints 的。由於 endpoints 中會包含很多敏感信息,除了 health 和 info 兩個支持 web 訪問外,其他的默認不支持 web 訪問
            include: hystrix.stream
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8000/eureka/
    
    

測試

  • 分別啓動註冊中心、服務提供者、Turbine監控和兩個服務消費者(service-consumer-node01/02)
    在這裏插入圖片描述
    TURBINE-CLIENT再註冊中心顯示ip:port
  • 瀏覽器訪問http://localhost:9009/hystrix,監控默認集羣
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 瀏覽器訪問http://localhost:9005/hello/lxthttp://localhost:9006/hello/lxt
    在這裏插入圖片描述
  • 測試成功

三、相關

  • 父模塊介紹傳送門
  • 源碼地址傳送門
  • 參考
    • https://www.cnblogs.com/carrychan/p/9529418.html
    • http://www.ityouknow.com/spring-cloud.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章