SpringCloud进击 | 五深入:断路器监控(Hystrix Dashboard)【Finchley版本】

1.前言

在 上一篇 Hystrix 的介绍中,我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的。而这些请求情况的指标信息都是 HystrixCommand 和 HystrixObservableCommand 实例在执行过程中记录的重要度量信息,它们除了 Hystrix 断路器实现中使用之外,对于系统运维也有非常大的帮助。

Hystrix 除了隔离依赖服务的调用以外,还提供了准实时的调用监控(Hystrix Dashboard),Hystrix 会持续地记录所有通过 Hystrix 发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。

Netflix 通过 hystrix-metrics-event-stream 项目实现了对以上指标的监控。Spring Cloud 也提供了 Hystrix Dashboard 的整合,对监控内容转化成可视化界面,Hystrix Dashboard Wiki 上详细说明了图上每个指标的含义。

 

2.准备

  1. 一个服务注册中心:wei-eureka-server,端口 8090(无需修改,正常启动)
  2. 一个服务提供者:wei-service-provider,端口 8010(无需修改,正常启动)
  3. 一个服务消费者:wei-consumer-ribbon,端口 8026(修改对象)

以下实验都是对 上一节 学习到的成果的延用与迭代(只需要配置 pom 依赖和 application.yml,再添加@EnableHystrixDashboard注解 就OK)。

 

3.进击

3.1.服务消费者改造

改造服务消息者:wei-consumer-ribbon。

3.1.1.POM依赖改造

在 pom.xml 中引入以下必要的依赖:

        <!--Hystrix起步依赖,在Ribbon使用断路器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--Hystrix 监控面板-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

3.1.2.配置文件改造

server:
  port: 8026
spring:
  application:
    name: wei-consumer-ribbon    # 指定进行服务注册时该服务的名称,服务与服务之间相互调用一般都是根据这个name
  sleuth:
    web:
      client:
        enabled: true
    sampler:
      probability: 1.0                   # 将采样比例设置为1.0,也就是全部都需要。默认是0.1
  zipkin:
    base-url: http://localhost:9411/     # 指定了 Zipkin 服务器的地址
#    base-url: http://localhost:94111/     # 指向错误 Zipkin 服务器的地址,验证RabbitMQ
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8090/eureka/    # 指定进行服务注册的地址
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream     # 暴露 endpoints

在配置文件 application.yml 中添加 management.endpoints.web.exposure.include=hystrix.stream, 这个是用来暴露 endpoints 的。由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持 web 访问外,其他的默认不支持 web 访问。

 

3.1.3.启动类改造

在启动类上面引入注解 @EnableHystrixDashboard,启用 Hystrix Dashboard 断路器监控面板功能。

package com.wei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
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.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 注解@EnableHystrix,开启Hystrix熔断功能
 * 注解@EnableHystrixDashboard,开启创建 Hystrix Dashboard 断路器监控面板
 */
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class WeiConsumerRibbonApplication {

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

    /**
     * 注解@Bean,向程序注入一个Bean
     * 注解@LoadBalanced,开启RestTemplate的负载均衡功能
     * 初始化RestTemplate,用来发起 REST 请求
     */
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

 

好了,以上,对于断路器监控面板的改造就全部完工。

启动。

 

4.测试

确认已经正常启动准备工作中的 服务注册中心、服务提供者、服务消费者。

1)断路器面板首页

浏览器请求URL:http://localhost:8026/hystrix,会看到如下界面:

【断路器监控面板】

通过 Hystrix Dashboard 主页面的文字介绍,我们可以知道,Hystrix Dashboard 共支持三种不同的监控方式:

前两者都是对集群的监控,需要整合 Turbine 才能实现。这一节我们先实现对单体应用的监控,这里的单体应用就使用我们之前在 浅出第四节 使用 Ribbon 和 Hystrix 实现的服务消费者(wei-consumer-ribbon)。

 

页面上的另外两个参数:

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

 

2)监控面板

URL监控输入栏输入 http://localhost:8026/actuator/hystrix.stream,然后点击 Monitor Stream 按钮,进入监控页面。

此为错误URL,Hystrix会提示连接失败:Unable to connect to Command Metric Stream.

因为Actuator 2.x 以后 endpoints 全部在/actuator下,这里需要确保 pom 文件有依赖 spring-boot-starter-actuator,application.yml 文件有 endpoints 被暴露设置:management.endpoints.web.exposure.include=hystrix.stream

另外,还要确认一下监控的 URL 后缀是否为 /actuator/hystrix.stream

【断路器监控面板 - 链接失败画面】

URL监控输入栏输入 http://localhost:8026/actuator/hystrix.stream,然后点击 Monitor Stream 按钮,进入监控页面。

一直显示 Loading ...,等了一会儿还是一直显示 Loading ...

【断路器监控面板 - Loading画面】

对的,在此之前,如果你还没有发过请求,监控面板则会一直显示 “Loading…”

这时,去浏览器访问URL:http://localhost:8026/actuator/hystrix.stream,会得到 Hystrix 不断发出的 ping 请求。

【断路器监控面板 - hystrix.stream】

 

3)接口验证

浏览器多次请求URL:http://localhost:8026/demo/info?name=tester

返回结果:

Hi,tester,我是服务,我被调用了,服务名为:wei-service-provider,端口为:8010[Ribbon + REST]

发完请求,再看 Hystrix Dashboard,监控面板展示如下:

【断路器监控面板 - 单应用监控】

可以看到监控面板中 Thread Pools 中只有一个实例,即单应用的断路器监控。

 

5)界面解读

以上图来说明其中各元素的具体含义:

  • 实心圆:它有颜色和大小之分,分别代表实例的监控程度和流量大小。如上图所示,它的健康度从绿色、黄色、橙色、红色递减。通过该实心圆的展示,我们就可以在大量的实例中快速的发现故障实例和高压力实例。
  • 曲线:用来记录 2 分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。
  • 其他一些数量指标如下图所示
【指标元素说明】

 

至此,单个应用的熔断监控开发并验证完成。

 

5.总结

pom.xml 文件完整依赖如下:

    <dependencies>
        <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-ribbon</artifactId>
        </dependency>
        <!--Hystrix起步依赖,在Ribbon使用断路器-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--Hystrix 监控面板-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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-sleuth</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>

        <!--Spring Cloud sleuth + Zipkin + RabbitMQ 整合-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

 

6.系列

SpringCloud进击 | 一浅出:服务注册与发现(Eureka)【Finchley版本】
SpringCloud进击 | 二浅出:服务消费者(Ribbon+REST)【Finchley版本】
SpringCloud进击 | 三浅出:服务消费者(Feign)【Finchley版本】
SpringCloud进击 | 四浅出:断路器与容错(Hystrix)【Finchley版本】
SpringCloud进击 | 五浅出:服务网关 - 路由(Zuul Router)【Finchley版本】
SpringCloud进击 | 六浅出:服务网关 - 过滤器(Zuul Filter)【Finchley版本】
SpringCloud进击 | 七浅出:配置中心(Git配置与更新)【Finchley版本】
SpringCloud进击 | 一深入:配置中心(服务化与高可用)【Finchley版本】
SpringCloud进击 | 二深入:配置中心(消息总线)【Finchley版本】
SpringCloud进击 | 三深入:服务链路跟踪(Spring Cloud Sleuth)【Finchley版本】
SpringCloud进击 | 四深入:服务链路跟踪(Sleuth+Zipkin+RabbitMQ整合)【Finchley版本】
SpringCloud进击 | 五深入:断路器监控(Hystrix Dashboard)【Finchley版本】
SpringCloud进击 | 六深入:断路器聚合监控(Hystrix Turbine)【Finchley版本】
SpringCloud进击 | 七深入:高可用的服务注册中心【Finchley版本】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值