spring-cloud-netflix-hystrix-dashboard

Posted on 2020-10-12 14:30  FLGB  阅读(973)  评论(0编辑  收藏  举报

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

Copyright © 2024 FLGB
Powered by .NET 9.0 on Kubernetes