Dubbo + Hystrix 熔斷器儀表盤

使用熔斷器儀表盤監控

在 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.xyl.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.xyl.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)

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

線程池核心線程數

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

Queue

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

斷路器

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

fallback

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

屬性配置參數

參數說明:https://github.com/Netflix/Hystrix/wiki/Configuration
HystrixProperty 參考代碼:http://www.programcreek.com/java-api-examples/index.php?source_dir=Hystrix-master/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/configuration/command/BasicCommandPropertiesTest.java

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章