首页 > 技术文章 > SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)

aaabbbcccddd 2020-09-03 20:14 原文

分类专栏: springcloud 史上最简单的 Spring Cloud 教程

转载请标明出处:
http://blog.csdn.net/forezp/article/details/81041113
本文出自方志朋的博客

个人博客纯净版:https://www.fangzhipeng.com/springcloud/2018/08/12/sc-f12-dash.html

在我的第四篇文章断路器讲述了如何使用断路器,并简单的介绍了下Hystrix Dashboard组件,这篇文章更加详细的介绍Hystrix Dashboard。

一、Hystrix Dashboard简介

在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。

二、准备工作

本文的的工程栗子,来源于第一篇文章的栗子,在它的基础上进行改造。

三、开始改造service-hi

在pom的工程文件引入相应的依赖:

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

其中,这三个依赖是必须的,缺一不可。

在程序的入口ServiceHiApplication类,加上@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHiApplication {

    /**
     * 访问地址 http://localhost:8762/actuator/hystrix.stream
     * @param args
     */

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

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    @HystrixCommand(fallbackMethod = "hiError")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }

}

运行程序: 依次开启eureka-server 和service-hi.

四、Hystrix Dashboard图形展示

打开http://localhost:8762/actuator/hystrix.stream,可以看到一些具体的数据:

这里写图片描述

打开localhost:8762/hystrix 可以看见以下界面:

这里写图片描述

在界面依次输入:http://localhost:8762/actuator/hystrix.stream 、2000 、miya
;点确定。

在另一个窗口输入: http://localhost:8762/hi?name=forezp

重新刷新hystrix.stream网页,你会看到良好的图形化界面:

这里写图片描述

源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter12

更多阅读

史上最简单的 SpringCloud 教程汇总

SpringBoot教程汇总

Java面试题系列汇总

五、参考资料

hystrix-dashboard

 

 

 

注意 

需要在service-hi的application.yml文件中添加

  1. server:
    port: 8762

    spring:
    application:
    name: eureka-client
    feign:
    hystrix:
    enabled: true
    eureka:
    client:
    serviceUrl:
    defaultZone: http://localhost:8761/eureka/

    management:
    endpoints:
    web:
    exposure:
    include: "*"
    cors:
    allowed-origins: "*"
    allowed-methods: "*"

2、在请求http://localhost:8762/actuator/hystrix.stream之前,需要随便请求一个接口,例如:http://localhost:8762/hi,否则会一直ping ping ping

推荐阅读