springcloud-Dashboard 流量監控 (九)


新建springcloud-consumer-dashboard-82模塊

  1. pom.xml

關鍵依賴

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
     <version>1.4.6.RELEASE</version>
 </dependency>
 <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>
      <version>1.4.6.RELEASE</version>
  </dependency>

其它依賴

<!--Feign的依賴-->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
      <version>1.4.6.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
      <version>1.4.6.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-ribbon</artifactId>
      <version>1.4.6.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>com.dong</groupId>
      <artifactId>springcloud-common</artifactId>
      <version>1.0-SNAPSHOT</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--熱部署工具-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
  </dependency>
  1. application.yml
server:
  port: 82
  1. 主啟動類 @EnableHystrixDashboard開啟流量監控
package com.dong.consumer3;

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

到此dashboard流量監控模塊就搭建完畢了。(可以認為它是一個工具,用來監控某個模塊的訪問量)

springcloud-provider-8001 配置流量監控

  1. 添加依賴
<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</artifactId>
      <version>1.4.6.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  1. 主啟動類 關鍵注解@EnableDiscoveryClient

注意:起初沒有使用@EnableCircuitBreaker注解,訪問http://localhost:8001/actuator/hystrix.stream 一直z在ping。並且流量監控頁面也是一直loading。最后發現需要加上@EnableCircuitBreaker 添加熔斷的支持。

package com.dong.provider;

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

    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        //訪問該頁面就是監控頁面
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
  1. controller 訪問的接口上加一個@HystrixCommand注解,可以不寫相關熔斷的方法。監控頁面就可以監控到了。(此注解的詳細用戶見服務熔斷
    @GetMapping("/dept/get/{id}")
    @HystrixCommand
    public Dept getDept(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("Fail");
        }
        return dept;
    }

測試

啟動springcloud-provider-8001,springcloud-consumer-dashboard-82和Eureka

啟動springcloud-consumer-dashboard-82可能會報一些錯,我們可以不管它。

訪問:http://localhost:8001/actuator/hystrix.stream 如果一直ping,我們可以訪問http://localhost:8001/dept/get/1 。還是不行就多訪問幾次,就可以ping到相關信息。

ping

最后訪問:http://localhost:82/hystrix

hystrix

填入要監測的地址,delay和title后,點擊Monitor Stream就能跳到對應的監控頁面了。

dashboard

最后多次訪問http://localhost:8001/dept/get/1 我們發現上圖箭頭指的灰色區域會逐漸變大。這就是說訪問量越多,圈越大。

補充

  1. 七色

七色

  1. 一圈

實心圓:公有兩種含義,他通過顏色的變化代表了實例的健康程度

它的健康程度從綠色<黃色<橙色<紅色遞減

該實心圓除了顏色的變化之外,它的大小也會根據實例的請求流量發生變化,流量越大,該實心圓就越大,所以通過該實心圓的展示,就可以在大量的實例中快速發現故障實例和高壓力實例。

一圈

  1. 一線

曲線:用來記錄2分鍾內流量的相對變化,可以通過它來觀察到流量的上升和下降趨勢!

一線

  1. 整圖說明

整圖說明

參考教程:https://www.kuangstudy.com/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM