可视化的数据监控 Hystrix-dashboard

Hystrix-dashboard 是一款针对 Hystrix 进行实时监控的工具,通过 Hystrix Dashboard 我们可以在直观地看到各 Hystrix Command 的请求响应时间, 请求成功率等数据。

项目中添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</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-hystrix-dashboard</artifactId>
 </dependency>

启动类中添加注解:
@EnableHystrix:表示当前程序开启数据监控
@EnableHystrixDashboard:表示当前程序开启数据监控的可视化视图程序

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

查看当前服务的监控信息---json字串格式的数据

http://服务ip:端口/hystrix.stream

使用视图模式查看

http://服务ip:端口/hystrix

监控中心界面介绍

使用 Turbine 在多个服务与集群情况下收集数据监控

Turbine 是聚合服务器发送事件流数据的一个工具,hystrix 的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过 turbine 来监控集群服务。

添加依赖

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

application配置文件中配置Turbine

#---------------------------------------turbine--------------------------
#配置 Eureka 中的 serviceId 列表,表明监控哪些服务
turbine.appConfig=eureka-consumer-ribbon-threadpool,springcloud-eureka-consumer-feign-fallback
#指定聚合哪些集群,多个使用","分割,默认为 default。可使用http://.../turbine.stream?cluster={clusterConfig 之一}访问
turbine.aggregator.clusterConfig= default
# 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
turbine.clusterNameExpression="default"

启动类中添加@EnableTurbine注解,启用Turbine服务

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

在被聚合的项目的 pom 文件添加 dashboard 坐标

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-hystrix</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-hystrix-dashboard</artifactId>
 </dependency>

在被聚合的项目的启动类上添加@EnableHystrixDashboard注解

    @EnableFeignClients
    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableHystrixDashboard 
    @EnableCircuitBreaker 
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerApplication.class, args);
        }
    }

然后通过Hystrix视图页面输入监控地址:http:// turbine服务IP : 端口 / turbine.stream即可看到监控数据视图

采用 RabbitMQ,收集监控数据

在需要监控的Consumer 服务服务中添加依赖

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

然后再application配置文件中配置RabbitMQ连接信息

spring.rabbitmq.host=192.168.1.19
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111
spring.rabbitmq.virtualHost=/

然后再启动类中添加@EnableHystrix和@EnableHystrixDashboa注解

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

创建 Turbine 项目添加依赖

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

然后同样配置RabbitMQ连接信息

spring.rabbitmq.host=192.168.1.19
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=1111
spring.rabbitmq.virtualHost=/

Turbine 的启动类中添加@EnableTurbineStream注解

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

然后通过Hystrix视图页面输入监控地址:http:// turbine服务IP : 端口 / turbine.stream即可看到监控数据视图

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容