HystrixDashboard配置与基础功能演示

HystrixDashboard是为Hystrix监控时提供比较友好的图形化界面,方便用户使用与分析。

要使用HystrixDashboard功能,需要引入三个依赖。

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

然后在Application中将功能开启,并注入ServletRegistrationBean。

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableHystrix
@EnableHystrixDashboard
public class OrderApplication {

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

    @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:18000/hystrix,18000是服务端口号,默认2秒刷新一次,界面如下。
HystrixDashboard配置与基础功能演示_第1张图片
地址栏输入:localhost:18000/actuator/hystrix.stream,就是在application中配置的url。

registrationBean.addUrlMappings("/actuator/hystrix.stream");

首次访问界面是空白的。
HystrixDashboard配置与基础功能演示_第2张图片
调用一次服务后就会有信息了,比如访问失败后如下:

HystrixDashboard配置与基础功能演示_第3张图片
达到一定的请求失败次数后,断路器打开。
HystrixDashboard配置与基础功能演示_第4张图片

你可能感兴趣的:(Spring,Cloud)