1. 程式人生 > 程式設計 >spring cloud 2.x版本 Hystrix Dashboard斷路器教程

spring cloud 2.x版本 Hystrix Dashboard斷路器教程

前言

本文采用Spring cloud本文為2.1.8RELEASE,version=Greenwich.SR3

本文基於前兩篇文章eureka-server、eureka-client、eureka-ribbon和eureka-feign的實現。 參考

概念

Hystrix Dashboard時Hystrix提供的一個可以檢視hystrix監控資料的控制面板。Hystrix提供了近實時的資料監控,Hystrix會實時、累加的記錄所有關於HystrixCommand的執行資訊,包括每秒執行多少請求,多少成功和多少失敗等。

建立Hystrix Dashboard工程

1.1 建立sping boot工程:hysteric-dashboard

1.2 新增pom.xml相關依賴

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId
>
<artifactId>spring-boot-starter-web</artifactId> </dependency> <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-dashboard</artifactId> </dependency> 複製程式碼

1.3 application新增配置資訊

spring:
  application:
    name: hystrix-dashboard
server:
  port: 8500

eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
複製程式碼

1.4 啟動類HystrixDashboardApplication增加註解

package spring.cloud.demo.hystrixdashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class,args);
    }

}
複製程式碼

@EnableHystrixDashboard:啟動Hystrix Dashboard斷路器看板相關配置

1.5 啟動hystrix-dashboard服務

開啟瀏覽器,輸入http://localhost:8500/hystrix顯示結果如下:

file

url輸入框:代表要監控的服務消費者

Single Hystrix App: https://hystrix-app:port/actuator/hystrix.stream:要監控路徑url格式

1.6 監控ribbon服務

1.6.1 eureka-ribbon增加Hystrix的Configuration

package spring.cloud.demo.eurekaribbon.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@Configuration
public class HystrixConfig {

    @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;
    }
}

複製程式碼

啟動eureka-client和eureka-ribbon服務。然後在hystrix dashboard控制面板url框中輸入:

file

點選Monitor Stream會顯示:

file

  • Unable to connect to Command Metric Stream問題原因:因為我們開啟監控的斷路器流Hystrix Stream路徑http://localhost:8901/hystrix.stream不是spring boot的預設路徑,也不是hystrix的預設路徑,所有要增加Hystrixconfig來制定斷路器的指標流Servlet。

  • 首次成功啟動頁面也有可能會顯示loading,這代表還沒有訪問我們要監控的服務,這是我們可以多次請求要訪問的服務就可以看到指標資料

按照同樣的方式我可以設定eureka-feign的配置來進行監看。

總結

本文簡單的搭建了Hystrix Dashborad資料監控平臺。

Hystrix現在已經是停止開發,處於維護階段,在Github上Hystrix已經說明,建議我們使用Resilience4J。後續會更新關於Resilience4J的簡單實用。

程式碼地址

gitHub地址


《Srping Cloud 2.X小白教程》目錄

轉載請註明出處,