Spring Cloud 系列之 Netflix Hystrix 服務監控


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>

  

配置文件

  

  application.yml

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 服務監控知識點就講解結束了。

  本文采用 知識共享「署名-非商業性使用-禁止演繹 4.0 國際」許可協議

  大家可以通過 分類 查看更多關於 Spring Cloud 的文章。

  

  🤗 您的點贊轉發是對我最大的支持。

  📢 掃碼關注 哈嘍沃德先生「文檔 + 視頻」每篇文章都配有專門視頻講解,學習更輕松噢 ~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM