微服務熔斷限流Hystrix之流聚合


簡介

上一篇介紹了 Hystrix Dashboard 監控單體應用的例子,在生產環境中,監控的應用往往是一個集群,我們需要將每個實例的監控信息聚合起來分析,這就用到了 Turbine 工具。Turbine有一個重要的功能就是匯聚監控信息,並將匯聚到的監控信息提供給Hystrix Dashboard來集中展示和監控。

流程

實驗

工程說明

工程名 端口 作用
eureka-server 8761 注冊中心
service-hi 8762 服務提供者
service-consumer 8763 服務消費者
service-turbine 8765 Turbine服務

核心代碼

eureka-server 、service-hi、service-consumer 工程代碼與上一節 微服務熔斷限流Hystrix之Dashboard 相同,下面是 service-turbine 工程的核心代碼。

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</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>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

application.yml

server:
  port: 8765

spring:
  application:
    name: service-turbine
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

turbine:
  app-config: service-consumer
  cluster-name-expression: new String("default")
  combine-host-port: true

參數說明:

  • turbine.app-config:指定要監控的應用名
  • turbine.cluster-name-expression:指定集群的名字
  • turbine.combine-host-port:表示同一主機上的服務通過host和port的組合來進行區分,默認情況下是使用host來區分,這樣會使本地調試有問題

啟動類

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class ServiceTurbineApplication {

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

}

模擬多實例

啟動多個 service-consumer 工程,來模擬多實例,可以通過命令java -jar service-consumer.jar --server.port=XXXX 來實現。

為了方便,在編輯器中實現啟動工程。但 idea 不支持單個應用的多次啟動, 需要開啟並行啟動:

選擇 “Edit Configurations...”

勾選 “Allow running in parallel”

測試

啟動工程,訪問 http//localhost:8763/hi , http//localhost:8764/hi , http//localhost:8763/oh , http//localhost:8764/oh,來產生測試數據。

訪問 http://localhost:8765/hystrix

輸入監控流地址 http://localhost:8765/turbine.stream ,點擊 Monitor Stream 進入監控頁面

可以看到聚合了兩個實例的 Hystrix dashbord 數據。

源碼

https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter18




歡迎掃碼或微信搜索公眾號《程序員果果》關注我,關注有驚喜~


免責聲明!

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



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