【SpringCloud深入浅出系列】SpringCloud组件之集成Hystrix Dashboard

SpringCloud 组件之集成 Hystrix Dashboard

  • 一、Hystrix Dashboard 是什么?
  • 二、创建项目实现 Hystrix Dashboard
    • 1.添加依赖
    • 2.添加配置
    • 3.启动类加入注解
    • 4.启动类注入 ServletRegistrationBean
    • 5.启动服务


一、Hystrix Dashboard 是什么?

Hystrix 仪表盘主要用来监控 Hystrix 的实时运行状态,通过它我们可以看到 Hystrix 的各项指标信息,从而快速发现系统中存在的问题进而解决它。

二、创建项目实现 Hystrix Dashboard

1.添加依赖

pom.xml 文件中加入依赖 spring-boot-starter-actuator 和 spring-cloud-starter-netflix-hystrix-dashboard,spring-boot-starter-actuator 为服务监控依赖:

    
        org.springframework.boot
        spring-boot-starter-actuator
       
     
        org.springframework.cloud
        spring-cloud-starter-netflix-hystrix-dashboard
    

2.添加配置

application.yml 文件中添加配置用来暴露 endpoints,由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,所以进行指定:

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

3.启动类加入注解

在启动类中加入注解 @EnableHystrixDashboard,开启Hystrix Dashboard:

package com.chaoyue.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
public class ConsumerApplication {

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

}

4.启动类注入 ServletRegistrationBean

在启动类注入 ServletRegistrationBean,该 Bean 的作用是在 Hystrix Dashboard 中打开 hystrix.stream:

package com.chaoyue.consumer;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
public class ConsumerApplication {

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

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

5.启动服务

启动服务。浏览器输入:http://localhost:8082/hystrix
【SpringCloud深入浅出系列】SpringCloud组件之集成Hystrix Dashboard_第1张图片
监控 Stream 输入:http://localhost:8082/hystrix.stream,点击【Monitor Stream】
【SpringCloud深入浅出系列】SpringCloud组件之集成Hystrix Dashboard_第2张图片

你可能感兴趣的:(Spring,Boot深入浅出,Java,深入浅出,Spring,Cloud深入浅出,spring,cloud,dashboard,hystrix,java)