1. 程式人生 > 實用技巧 >SpringCloud 之 Netflix Hystrix 服務監控

SpringCloud 之 Netflix Hystrix 服務監控

本文較大篇幅引用https://www.mrhelloworld.com/hystrix-dashboard-turbine/,相關內容版權歸該文章作者所有

引用上篇文章的工程資料

Actuator

Hystrix 除了可以實現服務容錯之外,還提供了近乎實時的監控功能,將服務執行結果和執行指標,請求數量成功數量等等這些狀態通過Actuator進行收集,然後訪問/actuator/hystrix.stream即可看到實時的監控資料。

在需要開啟資料監控的專案中新增actuator依賴。

<!-- spring boot actuator 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

在配置檔案中開啟hystrix.stream端點。如果希望所有端點暴露,配置為'*'

# 度量指標監控與健康檢查
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream
package
com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; // 開啟熔斷器註解 2 選 1,@EnableHystrix 封裝了 @EnableCircuitBreaker // @EnableHystrix @EnableCircuitBreaker @SpringBootApplication public class OrderServiceRestApplication { @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(OrderServiceRestApplication.class, args); } }

訪問:http://localhost:9090/actuator 可以看到已經開啟了hystrix.stream端點。如下圖

訪問:http://localhost:9090/actuator/hystrix.stream 結果如下:

此時並沒有獲取到 Hystrix 的資料。接下來請求一個肯定會出錯的方法產生服務熔斷降級處理後,結果如下:

對於這種純 JSON 的檢視方式非常不方便我們直觀的觀察到服務的執行狀態。我們可以使用 Hystrix 監控中心來進行檢視。

監控中心

監控中心就是 Hystrix 提供的一套視覺化系統Hystrix-Dashboard,可以非常友好的看到當前環境中服務執行的狀態。

Hystrix-Dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix-Dashboard我們可以直觀地看到各Hystrix Command的請求響應時間,請求成功率等資料

在需要開啟資料監控的專案中新增dashboard依賴。

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

在需要開啟資料監控的專案啟動類中新增@EnableHystrixDashboard註解。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

// 開啟熔斷器註解 2 選 1,@EnableHystrix 封裝了 @EnableCircuitBreaker
// @EnableHystrix
@EnableCircuitBreaker
// 開啟資料監控註解
@EnableHystrixDashboard
@SpringBootApplication
public class OrderServiceRestApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceRestApplication.class, args);
    }

}

訪問:http://localhost:9090/hystrix 監控中心介面如下:

輸入能夠返回監控資料的URL:http://localhost:9090/actuator/hystrix.stream

Turbine 是聚合伺服器傳送事件流資料的一個工具,dashboard 只能監控單個節點,實際生產環境中可能是叢集,因此可以通過 Turbine 來監控叢集服務

hystrix-demo父工程下建立hystrix-turbine工程。

專案引入hystrixdashboardturbine三個依賴。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>hystrix-turbine</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 繼承父依賴 -->
    <parent>
        <groupId>com.example</groupId>
        <artifactId>hystrix-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <!-- 專案依賴 -->
    <dependencies>
        <!-- spring-cloud netflix hystrix 依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!-- spring cloud netflix hystrix dashboard 依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <!-- spring cloud netflix turbine 依賴 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>
</project>
server:
  port: 8181 # 埠

spring:
  application:
    name: hystrix-turbine # 應用名稱

# 配置 Eureka Server 註冊中心
eureka:
  instance:
    prefer-ip-address: true       # 是否使用 ip 地址註冊
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    service-url:                  # 設定服務註冊中心地址
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

# 聚合監控
turbine:
  # 要監控的服務列表,多個用逗號分隔
  app-config: order-service-rest,order-service-feign
  # 指定叢集名稱
  cluster-name-expression: "'default'"

啟動類需要開啟@EnableHystrix@EnableHystrixDashboard@EnableTurbine三個註解

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

// 開啟熔斷器註解 2 選 1,@EnableHystrix 封裝了 @EnableCircuitBreaker
// @EnableHystrix
@EnableCircuitBreaker
// 開啟資料監控註解
@EnableHystrixDashboard
// 開啟聚合監控註解
@EnableTurbine
@SpringBootApplication
public class HystrixTurbineApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixTurbineApplication.class, args);
    }

}

order-service-restorder-service-feign都配置監控中心,最終環境如下:

訪問:http://localhost:8181/turbine.stream 多節點服務狀態資料如下:

訪問:http://localhost:8181/hystrix

order-service-restorder-service-feign兩個服務的執行狀態如下:

至此 Hystrix 服務監控知識點就講解結束了。