Dubbo + Hystrix 熔斷器儀表盤

學習完整課程請移步 互聯(lián)網(wǎng) Java 全棧工程師

本節(jié)視頻

使用熔斷器儀表盤監(jiān)控

在 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);
    }
}

創(chuàng)建 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 觸發(fā)熔斷后,監(jiān)控界面顯示效果如下:

附:Hystrix 說明

什么情況下會觸發(fā) fallback 方法

名字 描述 觸發(fā)fallback
EMIT 值傳遞 NO
SUCCESS 執(zhí)行完成,沒有錯誤 NO
FAILURE 執(zhí)行拋出異常 YES
TIMEOUT 執(zhí)行開始,但沒有在允許的時間內(nèi)完成 YES
BAD_REQUEST 執(zhí)行拋出HystrixBadRequestException NO
SHORT_CIRCUITED 斷路器打開,不嘗試執(zhí)行 YES
THREAD_POOL_REJECTED 線程池拒絕,不嘗試執(zhí)行 YES
SEMAPHORE_REJECTED 信號量拒絕,不嘗試執(zhí)行 YES

fallback 方法在什么情況下會拋出異常

名字 描述 拋異常
FALLBACK_EMIT Fallback值傳遞 NO
FALLBACK_SUCCESS Fallback執(zhí)行完成,沒有錯誤 NO
FALLBACK_FAILURE Fallback執(zhí)行拋出出錯 YES
FALLBACK_REJECTED Fallback信號量拒絕,不嘗試執(zhí)行 YES
FALLBACK_MISSING 沒有Fallback實例 YES

Hystrix Dashboard 界面監(jiān)控參數(shù)

Hystrix 常用配置信息

超時時間(默認1000ms,單位:ms)

  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:在調用方配置,被該調用方的所有方法的超時時間都是該值,優(yōu)先級低于下邊的指定配置
  • hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds:在調用方配置,被該調用方的指定方法(HystrixCommandKey 方法名)的超時時間是該值

線程池核心線程數(shù)

  • hystrix.threadpool.default.coreSize:默認為 10

Queue

  • hystrix.threadpool.default.maxQueueSize:最大排隊長度。默認 -1,使用 SynchronousQueue。其他值則使用 LinkedBlockingQueue。如果要從 -1 換成其他值則需重啟,即該值不能動態(tài)調整,若要動態(tài)調整,需要使用到下邊這個配置
  • hystrix.threadpool.default.queueSizeRejectionThreshold:排隊線程數(shù)量閾值,默認為 5,達到時拒絕,如果配置了該選項,隊列的大小是該隊列

注意: 如果 maxQueueSize=-1 的話,則該選項不起作用

斷路器

  • hystrix.command.default.circuitBreaker.requestVolumeThreshold:當在配置時間窗口內(nèi)達到此數(shù)量的失敗后,進行短路。默認 20 個(10s 內(nèi)請求失敗數(shù)量達到 20 個,斷路器開)
  • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds:短路多久以后開始嘗試是否恢復,默認 5s
  • hystrix.command.default.circuitBreaker.errorThresholdPercentage:出錯百分比閾值,當達到此閾值后,開始短路。默認 50%

fallback

  • hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests:調用線程允許請求 HystrixCommand.GetFallback() 的最大數(shù)量,默認 10。超出時將會有異常拋出,注意:該項配置對于 THREAD 隔離模式也起作用

屬性配置參數(shù)

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。