跟我學Spring Cloud(Finchley版)-15-Hystrix監控詳解

Hystrix提供了監控Hystrix Command的能力,本節來詳細探討。git

監控端點與數據

應用整合Hystrix,同時應用包含spring-boot-starter-actuator 依賴,就會存在一個/actuator/hystrix.stream 端點,用來監控Hystrix Command。當被@HystrixCommand 註解了的方法被調用時,就會產生監控信息,並暴露到該端點中。固然,該端點默認是不會暴露的,需使用以下配置將其暴露。github

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

此時,訪問/actuator/hystrix.stream 可返回以下結果:web

{"type":"HystrixCommand","name":"findById","group":"MovieController","currentTime":1547905939151,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"MovieController"}

對於Feign

前面講過Feign默認已經整合了Hystrix,但這個整合實際上是「不完整」,由於它默認不帶有監控端點,若是你在使用Feign的同時,也想使用監控端點,需按照以下步驟操做:spring

  • 加依賴服務器

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
  • 在啓動類上添加註解@EnableCircuitBreaker架構

  • application.yml 中添加以下配置:app

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

可視化監控數據

至此,咱們已可經過/actuator/hystrix.strem 端點觀察Hystrix運行狀況,但文字形式的監控數據很不直觀。現實項目中通常都須要一個可視化的界面,這樣才能迅速瞭解系統的運行狀況。Hystrix提供了一個輪子——Hystrix Dashboard,它的做用只有一個,那就是將文字形式的監控數據轉換成圖表展現ide

編寫Hystrix Dashboard

  • 加依賴spring-boot

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
  • 加註解:@EnableHystrixDashboard微服務

  • 寫配置:

    # 端口隨便寫,這裏只是代表下本身的端口規劃而已
    server:
      port: 8030

啓動後,訪問http://localhost:8030/hystrix 便可看到相似以下的界面:

Hystrix Dashboard首頁

將上文的/actuator/hystrix.stream 端點的地址貼到圖中,並指定Title,而後點擊Monitor Stream 按鈕,便可看到相似以下的圖表:

Hystrix Dashboard圖表

圖表解讀

Hystrix圖表解讀

小技巧

若是對http://localhost:8030/hystrix 地址中的hystrix 小尾巴不滿意怎麼辦?還記得Spring MVC的服務器端跳轉(forward)嗎?只需添加相似以下的Controller,就可使用http://localhost:8030/ 訪問到Hystrix Dashboard首頁了。

@Controller
public class HystrixIndexController {
  @GetMapping("")
  public String index() {
    return "forward:/hystrix";
  }
}

監控數據聚合-Turbine

至此,咱們已實現監控數據的可視化,然而現階段一次只能監控一個微服務實例,這顯然不能適用於生產。爲了能監控多個微服務,Netflix官方再次發揮造輪子的精神——它們又編寫了一個組件,Turbine。

TIPS

吐槽一下,Turbine是一個「發佈即死亡」的項目——2014年9月發佈1.0.0後,2014年11月又迅速推出2.0.0.DP,以後基本就不維護了,至今已經4年沒有提交過代碼了。

**Spring Cloud中,1.0.0以及2.0.0.DP兩個版本都有使用。**Turbine 1.0.0用於基於HTTP方式的數據收集(也就是本節講的這種方式)Turbine 2.0.0.DP2則用於基於MQ方式的收集(這種方式不少人遇到問題,單獨寫一篇番外吧)

Turbine簡介

Turbine是一個聚合Hystrix監控數據的工具,它可將全部相關/hystrix.stream端點的數據聚合到一個組合的/turbine.stream中,從而讓集羣的監控更加方便。

引入Turbine後,架構圖以下:

Turbine架構圖

TIPS

Turbine的GitHub:https://github.com/Netflix/Turbine

編寫Turbine Server

  • 加依賴

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    </dependency>
  • 加註解:@EnableTurbine

  • 寫配置:

    server:
      port: 8031
    spring:
      application:
        name: microservice-hystrix-turbine
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
      instance:
        prefer-ip-address: true
    turbine:
      # 要監控的微服務列表,多個用,分隔
      appConfig: microservice-consumer-movie,microservice-consumer-movie-feign
      clusterNameExpression: "'default'"

這樣,Tubine便可聚合microservice-consumer-movie,microservice-consumer-movie-feign兩個服務的/actuator/hystrix.stream 信息,並暴露在http://localhost:8031/turbine.stream,將該地址貼到Hystrix Dashboard上,便可看到相似以下的圖表:

Turbine圖表

配套代碼

通用方式暴露/actuator/hystrix.stream 端點:

Feign暴露/actuaotr/hystrix.stream 端點:

Hystrix Dashboard:

Turbine:

本文首發

http://www.itmuch.com/spring-cloud/finchley-15/

乾貨分享

全是乾貨

相關文章
相關標籤/搜索