1. 程式人生 > >Spring Cloud【Finchley】-12使用Hystrix Dashboard實現Hystrix資料的視覺化監控

Spring Cloud【Finchley】-12使用Hystrix Dashboard實現Hystrix資料的視覺化監控

文章目錄

概述

Spring Cloud【Finchley】-11Feign專案整合Hystrix監控中,我們通過 http://ip:port/actuator/hystrix.stream 的形式來檢視到的Hystrix的資料都是文字形式,可讀性非常差。 好在Spring Cloud為我們提供了Hystrix Dashboard,來看下如何使用吧。

官方文件: https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_circuit_breaker_hystrix_dashboard


Hystrix Dashboard

為了方便統一管理,這裡的例子我們也將Hystrix Dashboard這個微服務註冊到Eureka上,當然了也可以不註冊。


Step 1 新建專案

我們在父工程的maven上右鍵新建module , 起名為 micorservice-hystrix-dashboard

如下
在這裡插入圖片描述


Step2 增加maven依賴

		<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>

我們打算註冊到Eureka上,所以也需要新增spring-cloud-starter-netflix-eureka-client


Step3 啟動類增加註解@EnableHystrixDashboard

同時我們也打算註冊到Eureka上,所以也需要新增@EnableDiscoveryClient註解

package com.artisan.micorservice;

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;

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


Step4 配置檔案application.yml

server:
  port: 8888

spring:
  application:
    name: micorservice-hystrix-dashboard

#eureka
eureka:
  client:
    service-url:
      defaultZone: http://artisan:[email protected]:8761/eureka
  instance:
    prefer-ip-address: true  # 起碼點選後 會看到ip資訊
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

Step5 啟動微服務

因為我們註冊到了Eureka上,所以需要先啟動microservice-discovery-eureka,然後再啟動該服務,啟動成功後,訪問 http://localhost:8888/hystrix

在這裡插入圖片描述

Step6 測試

  • 繼續啟動服務提供者微服務 micorservice-provider-user,
  • 然後啟動整合了Hystrix的消費者微服務 micorservice-consumer-movie-ribbon-hystrix 和 micorservice-consumer-movie-fegin-hystrix

到Eureka上看下注冊的服務列表
在這裡插入圖片描述

micorservice-consumer-movie-fegin-hystrix 對應的hystrix url為:http://localhost:7901/actuator/hystrix.stream

micorservice-consumer-movie-ribbon-hystrix 對應的hystrix url為:
http://localhost:7902/actuator/hystrix.stream

分別輸入url和自定義的title

在這裡插入圖片描述

在這裡插入圖片描述

點選Monitor Stream 進入, 一直處於loading狀態

在這裡插入圖片描述

是因為還沒有觸發hystrix,我們來呼叫這倆微服務提供的對外介面

訪問下 http://localhost:7901/movie/1 觸發hystrix收集資訊,多訪問幾次,資訊如下
在這裡插入圖片描述

訪問下 http://localhost:7902/movie/2 觸發hystrix收集資訊 ,多訪問幾次

在這裡插入圖片描述

以上就實現了通過Dashboard 對單個節點的微服務進行近乎實時的視覺化監控。


程式碼

https://github.com/yangshangwei/SpringCloudMaster/tree/master/micorservice-hystrix-dashboard