使用Hystrix断路器(二)监控断路器指标流,使用可视化工具Hystrix dashboard

目录

一、Hystrix断路器流

二、本文测试环境用到的项目名列表

三、在被监控项目中启用Hystrix流端点

1. 添加依赖到eurekaconsumer项目

2. 添加配置到eurekaconsumer项目启动Hystrix流端点

四、使用Hystrix dashboard(豪猪监控中心)

1. 创建Spring Boot项目hystrix-dashboard并添加依赖

2. 主配置类添加注解启用Hystrix dashboard

3. 配置文件application.yml中添加配置属性

五、聚合多个Hystrix流

1. 创建Spring Boot项目hystrix-turbine并添加依赖

2. 在hystrix-turbine项目主配置类中添加注解启用Turbine

3. 在hystrix-turbine项目application.yml中添加配置

4. 启动hystrix-dashboard项目进行测试


一、Hystrix断路器流

Hystrix 为应用中的每个断路器提供了一个指标流。每当断路器保护的方法被调用时,它会收集一些调用相关的数据,将其发布到一个HTTP流中,这些数据可以实时监控正在运行中的应用的健康状况。

二、本文测试环境用到的项目名列表

Eureka快速入门篇(一)中的两个项目:

eurekaserver(注册中心,server.port=8761)

eurekauser(微服务提供者,server.port=8081)

Eureka快速入门篇(二)中的项目:

eurekaconsumer(微服务消费者,server.port=8083)

三、在被监控项目中启用Hystrix流端点

1. 添加依赖到eurekaconsumer项目

Hystrix流是由Actuator端点提供的,通过“/actuator/hystrix.stream”路径对外暴露,可以被任意REST端点消费。所以需要添加actuator依赖

        <!--使用hystrix指标流,引入actuator依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2. 添加配置到eurekaconsumer项目启动Hystrix流端点

management.endpoints.web.exposure.include=hystrix.stream

此时启动项目注册到注册中心访问:http://localhost:8083/actuator/hystrix.stream,如下返回json格式数据。(eurekaconsumer项目中已经存在一个被hystrix保护的方法)

自行消费Hystrix流端点得到json格式数据不易于阅读或自行处理。此时可以使用Hystrix dashboard可视化监控中心。

四、使用Hystrix dashboard(豪猪监控中心)

1. 创建Spring Boot项目hystrix-dashboard并添加依赖

        <!--添加hystrix-dashboar依赖,使用豪猪可视化中心监控断路器流 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

2. 主配置类添加注解启用Hystrix dashboard

在hystrix-dashboard项目的主配置类中添加@EnableHystrixDashboard注解

package com.hystrixdashboard;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard //主配置类中添加此注解,启用Hystrix dashboard豪猪可视化监控中心
public class HystrixDashboardApplication {

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

}

3. 配置文件application.yml中添加配置属性

# 应用名称
spring:
  application:
  name: hystrix-dashboard
# 应用服务 WEB 访问端口
server:
  port: 7979

# 由于Spring Cloud 版本升级后,监控页面显示:Unable to connect to Command Metric Stream。所以添加如下属性
hystrix:
  dashboard:
    proxy-stream-allow-list: "*"

此时浏览器中访问:http://localhost:7979/hystrix出现Hystrix dashboard的主界面

我们在文本框中输入:http://localhost:8083/actuator/hystrix.stream,监控hystrix指标流。点击:Monitor Stream按钮进入监控页面。此时我访问eurekaconsumer项目中被断路器所保护的一个服务消费的REST端点进行服务消费,如图:

 多刷新,消费几次。会得到如下图的Hystrix dashboard监控页面


到此就完成了 Hystrix dashboard 对 Hystrix 指标流的监控。

五、聚合多个Hystrix流

Hystrix dashboard 一次只能监控一个流。Netflix 的另一个项目 Turbine 提供了将所有微服务的所有流聚合到一个Hystrix流中的办法。

1. 创建Spring Boot项目hystrix-turbine并添加依赖

在新建的hystrix-turbine项目中添加eureka-client和turbine的依赖,此项目作为eureka客户端启动发布到注册中心。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入eureka的客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--引入turbine依赖,聚合多个Hystrix流-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>

2. 在hystrix-turbine项目主配置类中添加注解启用Turbine

package com.hystrixturbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableEurekaClient //启动Eureka客户端
@EnableTurbine //主配置添加此注解,启动Turbine,实现聚合多个Hystrix流
public class HystrixTurbineApplication {

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

}

3. 在hystrix-turbine项目application.yml中添加配置

# 应用名称
spring:
  application:
    name: hystrix-turbine
# 应用服务 WEB 访问端口
server:
  port: 8989

#向服务注册中心注册自己(此处为注册中心地址)
eureka:
  client:
  service-url:
  defaultZone: http://localhost:8761/eureka/

turbine:
  app-config: EUREKACONSUMER,EUREKACONSUMER-2 #以逗号隔开的形式添加需要聚合hystrix指标流的服务(微服务)
  cluster-name-expression: "'default'" #表明收集名为default集群中的所有聚合流。设置这个属性很重要,否则Turbine中不会包含任何特定应用的聚合流数据。
turbine:
  app-config: EUREKACONSUMER,EUREKACONSUMER-2 #以逗号隔开的形式添加需要聚合hystrix指标流的服务(微服务)
  cluster-name-expression: "'default'" #表明收集名为default集群中的所有聚合流。设置这个属性很重要,否则Turbine中不会包含任何特定应用的聚合流数据。

聚合流项目创建完毕,现在启动此项目注册到注册中心

EUREKASERVER-1:注册中心,eureka-server

EUREKAUSER:微服务提供者,eureka-client

EUREKACONSUMER 和 EUREKACONSUMER-2:两个微服务消费者实例,eureka-client。都提供hystrix断路器保护的方法,引入actuator端点,提供hystrix流(聚合流服务项目中会调用此端点)

HYSTRIX-TURBINE:聚合流项目(服务),eureka-client。

4. 启动hystrix-dashboard项目进行测试

一切就绪,现在启动hystrix-dashboard项目,在浏览器中访问:http://localhost:7979/hystrix

在地址输入框中输入:http://localhost:8989/turbine.stream,点击监控流按钮进行监控。

浏览器中分别进行多次访问服务消费:http://localhost:8083/getmyjsonStrhttp://localhost:8085/getmyjsonStrTwohttp://localhost:8085/getmyjsonStrTwo

两个服务消费后查看Hystrix Monitor 流监控页面此时显示两个服务的指标流数据:

end。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值