SpringCloud(四):Hystrix实例监控

前言

上一篇SpringCloud(三):Hystrix服务降级与熔断介绍了 Hystrix 断路器原理、常用配置以及一个配置实例。本篇介绍Hystrix仪表盘组件 Hystrix Dashboard,用于实时监控HystrixCommand方法的执行情况。

Hystrix单实例监控

创建 cloud-hystrix-dashboard 模块,引入相关依赖如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        <version>2.2.10.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

修改项目配置文件 application.yaml 如下:

server:
  port: 9001

spring:
  application:
    name: cloud-hystrix-dashboard

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # 设置Eureka Server交互的地址查询服务和注册服务地址
      defaultZone: http://localhost:7001/eureka/, http://localhost:7002/eureka/
  instance:
    instance-id: dashboard9001
    # 显示IP地址
    prefer-ip-address: true

hystrix:
  dashboard:
    proxy-stream-allow-list: "localhost"

在主启动类上添加@EnableHystrixDashboard注解,启用仪表盘监控。

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
public class HystrixDashboardMainApplication {

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

启动该模块,访问http://localhost:9001/hystrix,看到如下页面:

监控配置页面
界面中给出了监控集群和单个实例的地址。

  • 集群实例:https://turbine-hostname:port/turbine.stream
  • 单实例:https://hystrix-app:port/actuator/hystrix.stream

由于本地不支持https,因此如果是本地地址应填入http。需要主义被监控的服务需要开启 Actuator 的 hystrix.stream端点,配置信息如下:

management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream' #暴露hystrix监控端点

启动 cloud-eureka-server 和 cloud-provider-payment 模块,在页面中填入如下信息:

填入信息
进入监控页面,由于此时没有调用 HystrixCommand 方法,界面没有信息:

在加载
多次访问http://localhost:8001/provider/testProperties/1在这里插入图片描述
此时跳转到监控页面:

监控页

Hystrix图表解释

图中小球大小表示流量大小,小球不同颜色对应不同健康状况,曲线表示流量随时间变化情况。

图解

Hystrix集群监控

在 cloud-hystrix-dashboard 引入添加如下依赖,以启用turbine:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    <version>2.2.10.RELEASE</version>
</dependency>

在 application.yaml 中添加对turbine的支持:

turbine:
  # true 同一主机上的服务通过host和port的组合来进行区分,默认为true
  # false 以host来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计,监控中host集群数会为1了因为本地host是一样的
  combine-host-port: true
  #turbine.app-config: 配置Euraka中的serviceId列表,表明监控哪些服务
  app-config: cloud-payment-service
  # 1. 省略clusterNameExpression时,此时turbine.aggregator.clusterConfig需要配置想要监控的应用名称,需要监控应用名称必须大写
  # 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
  # 3. 当clusterNameExpression: metadata[‘cluster‘]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: consumer,则需要配置,同时turbine.aggregator.clusterConfig: consumer
  cluster-name-expression: "'default'"

在主启动类加入@EnableTurbine注解,开启Turbine功能。

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardMainApplication {

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

启动 eureka-server、hystrix-dashboard 和两个 payment-service,访问eureka注册中心http://localhost:7001/,看到服务信息如下:

服务列表
访问http://localhost:9001/hystrix添加监控端端点地址:

监控端
调用几次接口,得到如下界面:
主机2
在 Hosts中可以看到主机数为2,此时实现了对集群的监控。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值