1. 程式人生 > 實用技巧 >spring-cloud-netflix-hystrix-dashboard

spring-cloud-netflix-hystrix-dashboard

Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直觀地看到各Hystrix Command的請求響應時間, 請求成功率等資料。

Hystrix Dashboard

我們使用上期spring-cloud-consumer-hystrix工程進行修改,新增hystrix Dashboard功能

新增所需要的依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>lf.liyouyou</groupId> <artifactId>spring-cloud-netflix-demo</artifactId> <version>1.0
-SNAPSHOT</version> </parent> <groupId>lf.liyouyou</groupId> <artifactId>spring-cloud-consumer-hystrix</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-cloud-consumer-hystrix</name> <description>Demo project for
Spring Boot</description> <dependencies> <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</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>

二、啟動類添加註解

package lf.liyouyou;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SpringCloudConsumerHystrixApplication {

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

三、修改配置:

spring.application.name=spring-cloud-netflix-consumer-hystrix
server.port=9091
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
feign.hystrix.enabled=true
#預設只開啟了health和info,設定為*,則包含所有的web入口端點
management.endpoints.web.exposure.include=*
hystrix.dashboard.proxy-stream-allow-list=*

啟動專案,

輸入:http://localhost:9091/hystrix進入如下頁面:

注意自己的應用是在本地還是外部,本地用http,不同版本路徑不同,2.0版本路徑為../actuator/hystrix.stream

輸入之後點選 monitor,進入頁面

出現Unable to connect to Command Metric Stream.顯示未連線

(1)訪問自己的應用服務http://localhost:9091/actuator/hystrix.stream,顯示ping,呼叫熔斷介面http://localhost:9091/hello/lf,返回data

排除的程式碼、註解、包的問題

(2)檢視debug日誌,若出現

  Proxy opening connection to: http://localhost:9091/actuator/hystrix.stream

  WARN 6980 --- [nio-9091-exec-8] ashboardConfiguration$ProxyStreamServlet : Failed opening connection to http://localhost:9091/actuator/hystrix.stream : 404 : HTTP/1.1 404

  則需要在application.properties配置中,開啟actuator訪問

management.endpoints.web.exposure.include=*

(3)檢視debug日誌,若出現

WARN 9888 --- [nio-9091-exec-3] ashboardConfiguration$ProxyStreamServlet : Origin parameter: http://localhost:9091/actuator/hystrix.stream is not in the allowed list of proxy host names.

If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

則需要在application.properties配置中,開啟代理訪問

hystrix.dashboard.proxy-stream-allow-list=*

重新啟動專案,重新訪問,進入hystrix面板

訪問熔斷介面,面板如下

If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.

則需要在application.properties配置中,開啟代理訪問

spring-cloud-netflix-consumer-hystrix