1. 程式人生 > >Spring Cloud 應用篇 之 Hystrix Turbine(斷路器聚合監控)的基本搭建

Spring Cloud 應用篇 之 Hystrix Turbine(斷路器聚合監控)的基本搭建

在講解了 Hystrix Dashboard 之後,我們知道了,Hystrix Dashboard 實現的是單節點模式,一次只能監控一個服務,當有很多服務的時候,這樣單節點模式去監控,就需要開啟多個瀏覽器視窗,顯然這是很麻煩的。這個時候,我們就可以用到 Spring Cloud 的另一個元件 Turbine,它可以聚合多個服務的 Hystrix Dashboard 的資料用以顯示。

(一)簡介

Turbine 可以聚合多個服務的 Hystrix Dashboard 的資料在一個瀏覽器視窗中顯示,關於 Turbine 的搭建也是非常簡單。

(二)搭建環境

1. 建立一個 module(spring-cloud-hystrix-dashboard-turbine)

建立步驟可以參考之前的文章(這個環境也可以在 spring-cloud-hystrix-dashboard 的基礎進行改造,為了防止程式碼產生混亂,就新建一個 module)

2. Hystrix Dashboard 是個獨立的服務,可以不註冊到 eureka server,但是 Turbine 是聚合多個服務,通過服務例項 id 聚合,所以它要註冊到 eureka server 中,獲取服務列表,用以聚合監控。pom 檔案匯入以下依賴:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3. 啟動類,添加註解 @EnableTurbine

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardTurbineApplication {

    public static void main(String[] args) {

        SpringApplication.run(HystrixDashboardTurbineApplication.class, args);
    }
}

4. 配置檔案:

server:
  port: 8581

spring:
  application:
    name: spring-cloud-hystrix-dashboard-turbine

turbine:
  # 配置 Eureka 中的 serviceId 列表,指定要監控的服務
  app-config: SPRING-DEMO-SERVICE-FEIGN,SPRING-DEMO-SERVICE-RIBBON
  aggregator:
    cluster-config: default
  # 指定叢集名稱
  cluster-name-expression: "'default'"

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/


management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
      health:
        show-details: ALWAYS

5. 依次啟動 eureka server、spring-demo-service、spring-demo-service-feign、spring-demo-service-ribbon、spring-demo-service-ribbon、spring-cloud-hystrix-dashboard-turbine,訪問 http://localhost:8581/hystrix,介面和 Hystrix Dashboard 的介面一致


輸入 http://localhost:8581/turbine.stream(在此之前為了不出現 Loading... 狀態,提前訪問被監控的服務介面,呼叫一次即可),點選 Monitor Stream


可以看到 spring-demo-service-ribbon 和 spring-demo-service-feign 兩個服務的 Hystrix Dashboard 的資料已經被聚合到這一個介面中。