微服务Spring Cloud

Dubbo + Hystrix 熔断器仪表盘

2019-02-15  本文已影响74人  撸帝

学习完整课程请移步 互联网 Java 全栈工程师

本节视频

使用熔断器仪表盘监控

在 Provider 和 Consumer 项目增加 Hystrix 仪表盘功能,两个项目的改造方式相同

pom.xml 中增加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    <version>2.0.1.RELEASE</version>
</dependency>

在 Application 中增加 @EnableHystrixDashboard 注解

package com.funtl.hello.dubbo.service.user.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

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

创建 hystrix.stream 的 Servlet 配置

Spring Boot 2.x 版本开启 Hystrix Dashboard 与 Spring Boot 1.x 的方式略有不同,需要增加一个 HystrixMetricsStreamServlet 的配置,代码如下:

package com.funtl.hello.dubbo.service.user.consumer.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HystrixDashboardConfiguration {
    @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;
    }
}

测试 Hystrix Dashboard

浏览器端访问 http://localhost:9090/hystrix 界面如下:

点击 Monitor Stream,进入下一个界面,访问 http://localhost:9090/hi 触发熔断后,监控界面显示效果如下:

附:Hystrix 说明

什么情况下会触发 fallback 方法

名字 描述 触发fallback
EMIT 值传递 NO
SUCCESS 执行完成,没有错误 NO
FAILURE 执行抛出异常 YES
TIMEOUT 执行开始,但没有在允许的时间内完成 YES
BAD_REQUEST 执行抛出HystrixBadRequestException NO
SHORT_CIRCUITED 断路器打开,不尝试执行 YES
THREAD_POOL_REJECTED 线程池拒绝,不尝试执行 YES
SEMAPHORE_REJECTED 信号量拒绝,不尝试执行 YES

fallback 方法在什么情况下会抛出异常

名字 描述 抛异常
FALLBACK_EMIT Fallback值传递 NO
FALLBACK_SUCCESS Fallback执行完成,没有错误 NO
FALLBACK_FAILURE Fallback执行抛出出错 YES
FALLBACK_REJECTED Fallback信号量拒绝,不尝试执行 YES
FALLBACK_MISSING 没有Fallback实例 YES

Hystrix Dashboard 界面监控参数

Hystrix 常用配置信息

超时时间(默认1000ms,单位:ms)

线程池核心线程数

Queue

注意: 如果 maxQueueSize=-1 的话,则该选项不起作用

断路器

fallback

属性配置参数

上一篇 下一篇

猜你喜欢

热点阅读