SpringCloud(Greenwich版)Hystrix Dashboard可视化监控数据

一、简介

  Hystrix 提供了 Hystrix Dashboard 用来实时监控 HystrixCommand 方法的执行情况。Hystrix Dashboard可以有效地反映出每个 Hystrix 实例的运行情况,帮助我们快速发现系统中的问题,从而采取对应措施。

二、开始使用Hystrix Dashboard仪表盘

1)创建gradle模块hystrix-monitoring并添加如下依赖,hystrix与actuator在仪表盘监控项目中不需要引入,能省就省嘛 (code)

dependencies {
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client', version: '2.1.5.RELEASE'

    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix-dashboard'
}

2)编写application.yaml配置文件,非常简单的代码

server:
  port: 1000
spring:
  application:
    name: hystrix-monitoring
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka/

3)编写启动类添加@EnableHystrixDashboard注解,标记开启断路器仪表盘功能

package org.wesson.springcloud.hystrix;

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;

@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixMonitoringApplication {

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

}

4)测试

Step1:访问http://localhost:8761/,注册到的服务如下图:

Step2:访问http://localhost:1000/hystrix,可以看到如下页面:

  此图是 Hystrix Dashboard 的监控首页,该页面中并没有具体的监控信息。

  从页面文字内容中我们可以知道,Hystrix Dashboard 支持三种不同的监控方式,分别是:默认的集群监控、自定义集群监控与单体应用的监控。在这次示例中我们只使用单体应用的监控,通过 URL https://hystrix-app:port/actuator/hystrix.stream 开启,实现对具体某个服务实例的监控。

  Hystrix Dashboard 的监控首页还有其他两个参数分别是 DelayTitle

  • Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认为 2000 毫秒,可以通过配置该属性来降低客户端的网络和 CPU 的消耗。

  • Title:该参数会对应点击 Monitor Stream 之后监控页面头部标题 (Hystrix Stream),默认会使用具体监控实例的 URL,可以通过配置该信息来展示更适合的标题。

 

三、Fegin整合Hystrix Dashboard

1)之前Fegin整合Hystrix服务降级的api-fegin项目工程中添加如下依赖

compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'

2)需要注意一点的是,被监控的api-fegin服务需要暴露Actuator的 hystrix.stream 端点:

management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'

3)启动类必须添加@EnableCircuitBreaker注解。我看过很多其他博主的博客,被监控的服务并不需要引入hystrix dashboard依赖,多此一举。官网解释添加actuator依赖才是重点

@EnableCircuitBreaker

4)测试

Step1:运行 eureka-server 启动类,端口为8761

Step2:运行 provider-service 启动类,端口为8081

Step3:运行 api-fegin 启动类,端口为9090

Step4:运行 hystrix-monitoring 启动类,端口为1000

Step5:先访问http://localhost:8761/,结果如下图:

Step6:多次访问http://localhost:9090/client/info,返回结果如下:

  • hello, service provider port is from:8081

Step7:本章第二节测试访问过Hystrix Dashboard首页,并填写 Input 框信息如下:

单击 Monitor Stream 按钮后,就可以看到下图统计报表的界面:

 

猜你喜欢

转载自www.cnblogs.com/wessonshin/p/12443112.html
今日推荐