SpringCloud 2020.0.4 系列之Hystrix看板


1. 概述

老話說的好:沉默是金,有時適當的沉默,比滔滔不絕更加有效。

 

言歸正傳,前面我們聊了有關 Hystrix 降級熔斷的話題,今天我們來聊聊如何使用 turbine 和 hystrix dashboard 總覽項目的熔斷降級情況。

閑話不多說,直接上代碼。

 

2. 暴露業務服務的 actuator 接口

2.1 主要依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>

 

2.2 主要配置

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

 

2.3 啟動類中需包含 @EnableHystrix 注解

 

2.4 啟動服務,查看接口是否暴露

啟動服務后,在瀏覽器輸入 http://服務IP:端口/actuator/,查看是否有 hystrix.stream 接口

 

3. 新建 turbine 工程

3.1 主要依賴

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 健康檢查 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
            <version>2.2.9.RELEASE</version>
        </dependency>

 

3.2 主要配置

spring:
  application:
    name: my-turbine
  main:
    allow-bean-definition-overriding: true
server:
  port: 38000
management:
  server:
    port: 38001
eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Eureka Server的地址
    healthcheck:
      enabled: true    # 開啟健康檢查, 依賴於 spring-boot-starter-actuator
  instance:
    lease-renewal-interval-in-seconds: 5      # 發出續約指令的間隔,默認30秒
    lease-expiration-duration-in-seconds: 30  # 租期到期時間,默認90秒

turbine:
  app-config: my-feign,my-eureka-client    # 指定需要監控的服務名,監控多個服務逗號分隔
  cluster-name-expression: '"default"'  # 集群名稱
  combine-host-port: true     # 將端口和hostname作為區分不同服務的條件
  aggregator:
    cluster-config: default

 

3.3 在啟動類增加注解

@EnableDiscoveryClient
@EnableHystrix
@EnableTurbine
@EnableAutoConfiguration
public class MyTurbineApplication {

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

 

3.4 啟動 turbine 工程,驗證接口

啟動 turbine 工程后,在瀏覽器輸入 http://服務IP:端口/turbine.stream,調用降級接口后,會顯示很多過程數據

 

4. 新建 hystrix dashboard 工程

注意:此工程不需要注冊到 Eureka

4.1 主要依賴

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

 

4.2 主要配置

spring:
  application:
    name: my-hystrix-dashboard
  main:
    allow-bean-definition-overriding: true
server:
  port: 39000

 

4.3 在啟動類增加注解

@EnableHystrixDashboard
@SpringCloudApplication
public class MyHystrixDashboardApplication {

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

 

4.4 啟動 hystrix dashboard 服務

4.4.1 啟動 hystrix dashboard 服務,然后在瀏覽器輸入 http://服務IP:端口/hystrix

 

4.4.2 在 Hystrix Dashboard 頁面輸入 turbine 的地址:http://服務IP:端口/turbine.stream,點擊 Monitor Stream 按鈕

 

 

4.4.3 Unable to connect to Command Metric Stream. 報錯解決方案

此時,會報 Unable to connect to Command Metric Stream 字樣的錯誤。

這是因為 turbine 地址的域名不在 hystrix dashboard 的允許列表中, 我們在配置文件中增加 hystrix.dashboard.proxy-stream-allow-list 配置,重啟服務即可

spring:
  application:
    name: my-hystrix-dashboard
  main:
    allow-bean-definition-overriding: true
server:
  port: 39000


hystrix:
  dashboard:
    proxy-stream-allow-list: "localhost"

 

4.4.4 刷新頁面,查看 dashboard

 

5. 綜述

今天聊了一下 Hystrix看板,希望可以對大家的工作有所幫助。

歡迎幫忙點贊、評論、轉發、加關注 :)

關注追風人聊Java,每天更新Java干貨。

 

6. 個人公眾號

追風人聊Java,歡迎大家關注


免責聲明!

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



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