Spring Cloud(七)Hystrix 监测仪表盘

Spring Cloud(七)Hystrix 监测仪表盘

Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上, 这个工具就是Turbine.

Hystrix Dashboard仪表盘

新建工程

1、添加依赖


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

2、启动类

启动类添加启用Hystrix Dashboard和熔断器

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
public class ConsumerApplication {

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

3、配置

server.port=6001
spring.application.name=hystrix_dashboard
eureka.client.service-url.defaultZone=http://localhost:7001/eureka,http://localhost:7002/eureka

4、测试

启动工程后访问 http://localhost:6001/hystrix,将会看到如下界面:

图中会有一些提示:

Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream

从Hystrix仪表盘首页中可以看出,它支持3种监控,前两种基于Turbine,一种是基于集群,另一种是指定集群,第三种是单点监控。下面还有两个框,一个是轮询时间,也就是隔多长时间轮询一次;另一个是标题,也就是仪表盘页面的标题是什么。

单点监控

从单点监控的说明可以看出,只需要给出http://hystrix-app:port/hystrix.stream格式的URL给仪表盘即可。

前面已经在产品微服务中使用了Hystrix,只是还需要引入Spring boot的监控依赖才可以,所以在产品微服务中先引入Actuator依赖

        <!-- hystrix监控依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-actuator</artifactId>
        </dependency>

但是这样还不够,因为对于Actuator端点是不暴露的 ,为了使端点暴露,需要在产品微服务的application.properties上添加属性:

management.endpoints.web.exposure.include=health,info,hystrix.stream

但是我在做的时候,发现上面的配置并不可用,我的具体版本是spring boot 2.1,原因未知,后来用如下方法替代,在启动类中加入一个bean初始化端点

    //spring boot 2.0以后actuator改变端口,这里添加映射,开放端口
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

启动服务后在页面中输入http://localhost:9001/actuator/hystrix.stream,然后延迟选择2000ms,Title:产品,点击Monitor Stream

到监控页面就会显示如下图:

在这里插入图片描述

其实就是http://localhost:6001/hystrix.stream返回结果的图形化显示,Hystrix Dashboard Wiki上详细说明了图上每个指标的含义,如下图:

在这里插入图片描述

到此单个应用的熔断监控已经完成。

Turbine

在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

在Dashboard仪表盘微服务中

1、添加依赖

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

2、配置文件

server.port=6001
spring.application.name=hystrix_dashboard
eureka.client.service-url.defaultZone=http://localhost:7001/eureka,http://localhost:7002/eureka

turbine.app-config=product,user
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
  • turbine.appConfig :配置Eureka中的serviceId列表,表明监控哪些服务
  • turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
  • turbine.clusterNameExpression : 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

3、启动类

启动类添加@EnableTurbine,激活对Turbine的支持

package com.lay.dashboard;

@SpringBootApplication
//开启hystrix面板
@EnableHystrixDashboard
//开启集群监控面板
@EnableTurbine
public class DashboardApplication {

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

到此Turbine(hystrix-dashboard-turbine)配置完成

4、测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值