1. 程式人生 > >Hystrix監控的配置詳解

Hystrix監控的配置詳解

hystrix監控.png

在微服務架構中,hystrix處理容錯外,還有實時監控功能,在服務發生呼叫時,會將每秒請求數、成功請求數等執行指標記錄下來。

本文示例程式碼:springcloud-demo
其中本文相關的專案有:
- 服務發現 Eureka Server: discovery
- 鏈路追蹤 sleuth+zipkin:trace
- 服務提供者:hello
- 服務提供者: world
- 服務消費者: helloworld
- 服務消費者: helloworld-feign
- 服務呼叫監控: hystrix-dashboard
- 監控聚合: hystrix-turbine
- 監控聚合(訊息中介軟體): hystrix-turbine-mq

通過介面的監控

啟動helloworld專案後,訪問http://localhost:8020/message後,再次訪問http://localhost:8020/hystrix.stream可以看到介面不斷地輸出監控日誌,監控日誌裡包含了各種指標(各種指標資訊請參考官方文件)。

按照同樣的步驟操作helloworld-feign專案後,卻發現找不到該頁面,這是因為需要做一些如下配置:
pom.xml引入hystrix依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId
>
<artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>

在啟動類上加上@EnableCircuitBreaker註解,再次執行上述操作,介面不斷輸出監控日誌。
helloworld-feign的hystrix監控日誌.png

使用hystrix-board對監控進行圖形化展示

上面的日誌資訊不夠直觀,藉助hystrix-dashboard可對監控進行圖形化展示。
- 在service建立hystrix-dashboard專案
- 引入hystrix-dashboard依賴

    <dependency
>
<groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
  • 啟動類配置@EnableHystrixDashboard註解(同時配置@EnableDiscoveryClient向註冊中心註冊,方便管理)
  • 配置檔案設定埠
server:
  port: 8087

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
spring:
  application:
    name: hystrix-dashboard

  • 測試結果
    啟動hystrix-dashboard後,輸入http://localhost:8087/hystrix地址,出現文章第一張圖所示的介面。在第一個文字框輸入http://localhost:8020/hystrix.stream或者http://localhost:8030/hystrix.stream後點擊Monitor Stream則會顯示如下監控結果:

20.png

以上的hystrix-dashboard每次只能輸入一個監控地址,在微服務架構中往往有很多無法需要監控,該怎麼辦呢?

可以使用Turbine聚合監控資料,讓hystrix-dashboard顯示這個聚合資料後的地址。

Turbine聚合監控資料

  • 建立一個hystrix-turbine專案,引入如下maven依賴
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
        </dependency>
  • 啟動類上配置@EnableTurbine註解
  • 配置檔案application.yml指明要從收集哪些微服務的監控資料
server:
  port: 8088

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
    prefer-ip-address: true
spring:
  application:
    name: hystrix-turbine
turbine:
  app-config: helloworld,helloworldfeign
  cluster-name-expression: "'default'"

注意turbine的配置,這裡收集helloworldhellowordfeign日誌
- 啟動專案hystrix-turbine後,在hystrix-dashboard中輸入http://localhost:8089/turbine.stream,則展示如下聚合結果:

使用Turbine聚合hystrix監控資料

以上Turbine聚合微服務的監控資料,然後在hystrix-dashboard展示多個微服務的實時監控資料。 但是Turbine也有它的侷限性,比如服務之間無法通訊,服務不在Eureka Server上註冊,則Turbine無法收集到微服務的日誌。那麼這種情況下,需要藉助訊息中介軟體來解決。

通過訊息中介軟體RabbitMQ收集監控資料

微服務在發生呼叫時,將監控資料儲存到RabbitMQ,監控從RabbitMQ獲取資料進行實時展示,這樣有利於微服務和監控端進行解耦,可以解決不在服務中心註冊的服務的監控資料也可以被收集到。

  • 安裝RabbitMQ
    使用如下docker命令安裝RabbitMQ,並暴露相應的埠(本人開發機為linux環境,docker的使用可以極大提高軟體安裝和專案部署的效率,推薦使用docker)
 docker run -d --name rabbitmq -p 5673:5672 -p 15673:15672 docker.io/rabbitmq:3-management

這樣向外暴露5673的連線埠和15673的web埠,預設使用者名稱密碼是guest
安裝後的RabbitMQ.png

  • 微服務端修改,以便向RabbitMQ傳輸監控日誌
    本文以helloworld為例改造微服務,pom中引入依賴
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>

配置檔案中加上連線RabbitMQ的配置

spring:
  rabbitmq:
    host: localhost
    port: 5673
    username: guest
    password: guest

重啟helloworld,並在瀏覽器輸入http://localhost:8020/message形成監控資料。

  • 新建hystrix-turbine-mq專案
    專案中引入turbine和rabbitmq依賴
    <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine-stream</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

啟動類上配置@EnableTurbineStream註解
配置檔案:

server:
  port: 8089

eureka:
  client:
    serviceUrl:


      defaultZone: http://localhost:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
    prefer-ip-address: true
spring:
  application:
    name: hystrix-turbine-mq
  rabbitmq:
    host: localhost
    port: 5673
    username: guest
    password: guest

這樣指明瞭監控的資料來源。
- 測試結果
啟動hystrix-dashboard後,輸入http://localhost:8089 即可展示如下的監控結果:
RabbitMQ收集監控資料.png

本文參考了《Spring cloud 與Docker 微服務實戰》這本書。