SpringCloud(七):Hystrix仪表盘与Turbine集群监控【Greenwich 版】

断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的。而这些请求情况的指标信息都是 HystrixCommand 和 HystrixObservableCommand 实例在执行过程中记录的重要度量信息,它们除了 Hystrix 断路器实现中使用之外,对于系统运维也有非常大的帮助。这些指标信息会以 “滚动时间窗” 与 “桶” 结合的方式进行汇总,并在内存中驻留一段时间,以供内部或外部进行查询使用,Hystrix Dashboard 就是这些指标内容的消费者之一。

创建Hystrix Dashboard

修改之前的hystrix-client-8007模块,添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

在启动类加上@EnableHystrixDashboard注解,启用 Hystrix Dashboard 功能

@EnableEurekaClient
@EnableCircuitBreaker
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixClient8007Application {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

到这hystrix项目改造完成,访问http://localhost:8007/hystrix,可以看到如下界面
在这里插入图片描述
通过 Hystrix Dashboard 主页面的文字介绍,我们可以知道,Hystrix Dashboard 共支持三种不同的监控方式:

  • 默认的集群监控:通过 URL:http://turbine-hostname:port/turbine.stream 开启,实现对默认集群的监控。
  • 指定的集群监控:通过 URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 开启,实现对 clusterName 集群的监控。
  • 单体应用的监控: 通过 URL:http://hystrix-app:port/actuator/hystrix.stream开启 ,实现对具体某个服务实例的监控。

前两者都对集群的监控,需要整合 Turbine 才能实现。我们先实现对单体应用的监控

改造eureka客户端

修改eureka-client-8003,eureka-client-8004模块,这里直接两个模块都修改了,方便下面直接使用Turbine集群监控。下面两个模块的改造是一摸一样的

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

添加endpoints,将hystrix.stream暴露出来,因为Hystrix Dashboard 监控单实例节点需要通过访问实例的 /actuator/hystrix.stream 接口来实现

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

启动类上加上@EnableCircuitBreaker或者@EnableHystrix注解,开启断路器功能

@EnableCircuitBreaker
@EnableEurekaClient
@SpringBootApplication
public class EurekaClient8003Application {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClient8003Application.class, args);
    }
}

提供一个方法,该方法要加上@HystrixCommand注解,因为dashboard监控的是开启了熔断的端点

@Service
public class MyHystrixService {
    //同步请求
    @HystrixCommand(fallbackMethod = "getFallBack")
    public void execute(){
        System.out.println("调用成功");
    }
    
    public void getFallBack(){
        System.out.println("调用失败");
    }
}

controller中调用该方法

@Autowired
private MyHystrixService myHystrixService;

/**
 * 测试时Hystrix仪表盘调用此方法
 */
@RequestMapping("/hystrixTest")
public void hystrixTest(){
    myHystrixService.execute();
}

下面我们启动注册中心8001,8002(启动一个就行),启动8003客户端和hystrix8007客户端,接着在Hystrix Dashboard的url 中输入http://localhost:8003/actuator/hystrix.stream,点击Monitor Stream按钮 进行对8003客户端的监控。

进入界面之后会发现一只显示loading,这个时候需要对被监测的节点(8003客户端)发出一个请求,这个请求必须调用加了@HystrixCommand注解的方法。如果被检测的节点是Feign的话,则必须调用加了@FeignClient注解的方法。

下面访问http://localhost:8003/hystrixTest,对8003客户端发送一个请求,就会看到监控台有了数据
在这里插入图片描述

界面解读

在这里插入图片描述
以上图来说明其中各元素的具体含义:

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

其他一些数量指标如下图所示:
在这里插入图片描述
Hystrix Dashboard 我们只能实现对服务单个实例的数据展现,在生产环境我们的服务是肯定需要做高可用的,那么对于多实例的情况,我们就需要将这些度量指标数据进行聚合。下面,我们就来介绍一下另外一个工具:Turbine。

创建Turbine模块

创建turbine-client-9000模块,添加依赖

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

启动类上加上@EnableTurbine注解开启 Turbine

@EnableTurbine
@SpringBootApplication
public class TurbineClient9000Application {
    public static void main(String[] args) {
        SpringApplication.run(TurbineClient9000Application.class, args);
    }
}

添加配置

spring:
  application:
    name: turbine
server:
  port: 9000
management:
  server:
    port: 9001
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/,http://localhost:8002/eureka/
turbine:
  app-config: eureka-client
  cluster-name-expression: new String("default")
  combine-host-port: true

参数说明

  • turbine.app-config 参数指定了需要收集监控信息的服务名;
  • turbine.cluster-name-expression 参数指定了集群名称为 default,当我们服务数量非常多的时候,可以启动多个 Turbine 服务来构建不同的聚合集群,而该参数可以用来区分这些不同的聚合集群,同时该参数值可以在 Hystrix 仪表盘中用来定位不同的聚合集群,只需要在 Hystrix Stream 的 URL 中通过 cluster 参数来指定;
  • turbine.combine-host-port 参数设置为 true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,默认情况下会以 host 来区分不同的服务,这会使得在本地调试的时候,本机上的不同服务聚合成一个服务来统计。

接着在Hystrix Dashboard的url中输入http://localhost:9000/turbine.stream,点击Monitor Stream按钮 进行控。可以看到目前我们有两个实例。
在这里插入图片描述

参考

Spring Cloud(五):Hystrix 监控面板【Finchley 版】

相关阅读

项目代码
SpringCloud 汇总【Greenwich 版】
SpringCloud(一):Eureka注册中心【Greenwich 版】
SpringCloud(二):Ribbon负载均衡【Greenwich 版】
SpringCloud(三):Feign声明式服务调用【Greenwich 版】
SpringCloud(四):Hystrix熔断器介绍【Greenwich 版】
SpringCloud(五):Hystrix的请求熔断与服务降级【Greenwich 版】
SpringCloud(六):Hystrix的请求合并【Greenwich 版】
SpringCloud(七):Hystrix仪表盘与Turbine集群监控【Greenwich 版】
SpringCloud(八):Zuul网关【Greenwich 版】
SpringCloud(九):Config配置中心【Greenwich 版】
SpringCloud(十):Bus消息总线【Greenwich 版】
SpringCloud(十一):Stream消息驱动 + RabbitMQ【Greenwich 版】
SpringCloud(十二):Sleuth链路跟踪【Greenwich 版】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值