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

簡介

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

流程

實驗

工程說明

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

核心代碼

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

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 來實現。github

爲了方便,在編輯器中實現啓動工程。但 idea 不支持單個應用的屢次啓動, 須要開啓並行啓動:spring

選擇 「Edit Configurations...」express

勾選 「Allow running in parallel」微信

測試

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

訪問 http://localhost:8765/hystrix ,編輯器

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

能夠看到聚合了兩個實例的 Hystrix dashbord 數據。

源碼

https://github.com/gf-huanchu...

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

相關文章
相關標籤/搜索