天天看點

Spring cloud學習筆記6-斷路器監控(Hystrix Dashboard)1. 概述2. 步驟3. 名額說明

斷路器監控Hystrix Dashboard

  • 1. 概述
  • 2. 步驟
    • 2.1 建立工程
    • 2.2 Pom引入
    • 2.3 配置檔案application
    • 2.4 啟動類
    • 2.5 檢視結果
  • 3. 名額說明

1. 概述

Hystrix Dashboard是Hystrix的儀表盤元件,主要用來實時監控Hystrix的各項名額資訊,通過界面回報的資訊可以快速發現系統中存在的問題。

2. 步驟

2.1 建立工程

建立一個maven的module工程,名稱為hystrix-dashboard,父親pom為springcloud。

2.2 Pom引入

引入actuator、netflix-hystrix和netflix-hystrix-dashboard的jar,Dashboard服務是個獨立的結點,不需要配置eureka資訊,但是本例子提供一個rest服務,是以引入netflix-eureka-client和web的jar,pom内容如下:

<?xml version="1.0"?>
<project
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.lin</groupId>
		<artifactId>springcloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>com.lin</groupId>
	<artifactId>hystrix-dashboard</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hystrix-dashboard</name>
	<url>http://maven.apache.org</url>
	
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</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>
	</dependencies>
</project>

           

2.3 配置檔案application

建立一個resources檔案夾,在其下面建立一個application.yml檔案,配置如下:

server:
  port: 8770

spring:
  application:
    name: service-hystrixdashboard

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
      
management:  
  endpoints:  
    web:  
      exposure:  
        include: "*"  
      cors:  
        allowed-origins: "*"  
        allowed-methods: "*"

           

2.4 啟動類

建立一個啟動類ServiceHystrixDashboardApplication.java,加入注解@EnableHystrix、@EnableHystrixDashboard、@EnableCircuitBreaker用于斷路器功能,同時注冊到eureka中。代碼如下:

package org.hystrix.dashboard;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHystrixDashboardApplication {
    /**
     * 通路位址 http://localhost:8770/actuator/hystrix.stream
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run( ServiceHystrixDashboardApplication.class, args );
    }
    @Value("${server.port}")
    String port;
    @RequestMapping("/hystrixdashboard")
    @HystrixCommand(fallbackMethod = "hystrixDashboardError")
    public String home(@RequestParam(value = "name", defaultValue = "linwu") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
    public String hystrixDashboardError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}

           

2.5 檢視結果

1) 通路服務http://localhost:8770/hystrixdashboard

2) 打開網頁http://localhost:8770/actuator/hystrix.stream

可以看到資料傳輸的過程,如下圖:

Spring cloud學習筆記6-斷路器監控(Hystrix Dashboard)1. 概述2. 步驟3. 名額說明

3) 打開網頁http://localhost:8770/hystrix

輸入參數如下圖所示

Spring cloud學習筆記6-斷路器監控(Hystrix Dashboard)1. 概述2. 步驟3. 名額說明

可以看出圖形的結果,如下圖

Spring cloud學習筆記6-斷路器監控(Hystrix Dashboard)1. 概述2. 步驟3. 名額說明

3. 名額說明

Spring cloud學習筆記6-斷路器監控(Hystrix Dashboard)1. 概述2. 步驟3. 名額說明

本文參考文獻:https://blog.csdn.net/forezp/article/details/70148833/

繼續閱讀