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

前言

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

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

概念

Hystrix Dashboard時Hystrix提供的一個能夠查看hystrix監控數據的控制面板。Hystrix提供了近實時的數據監控,Hystrix會實時、累加的記錄全部關於HystrixCommand的執行信息,包括每秒執行多少請求,多少成功和多少失敗等。git

建立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斷路器看板相關配置github

1.5 啓動hystrix-dashboard服務

打開瀏覽器,輸入http://localhost:8500/hystrix顯示結果以下:web

file

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

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框中輸入:
fileapp

點擊Monitor Stream會顯示:
file分佈式

  • Unable to connect to Command Metric Stream問題緣由:由於咱們開啓監控的斷路器流Hystrix Stream路徑http://localhost:8901/hystrix.stream不是spring boot的默認路徑,也不是hystrix的默認路徑,全部要增長Hystrixconfig來制定斷路器的指標流Servlet。spring-boot

  • 首次成功啓動頁面也有可能會顯示loading,這表明尚未訪問咱們要監控的服務,這是咱們能夠屢次請求要訪問的服務就能夠看到指標數據post

按照一樣的方式我能夠設置eureka-feign的配置來進行監看。

總結

本文簡單的搭建了Hystrix Dashborad數據監控平臺。

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

代碼地址

gitHub地址


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

轉載請註明出處,

  • 聯繫方式:4272231@163.com
相關文章
相關標籤/搜索