java B2B2C Springcloud多租戶電子商城系統-斷路器聚合監控

一、Hystrix Turbine簡介

看單個的Hystrix Dashboard的數據並沒有什麼多大的價值,要想看這個系統的Hystrix Dashboard數據就需要用到Hystrix Turbine。Hystrix Turbine將每個服務Hystrix Dashboard數據進行了整合。Hystrix Turbine的使用非常簡單,只需要引入相應的依賴和加上註解和配置就可以了。

二、準備工作

本文使用的工程爲上一篇文章的工程,在此基礎上進行改造。因爲我們需要多個服務的Dashboard,所以需要再建一個服務,取名爲service-lucy,它的基本配置同service-hi,具體見源碼,在這裏就不詳細說明。

三、創建service-turbine

引入相應的依賴:

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

在其入口類ServiceTurbineApplication加上註解@EnableTurbine,開啓turbine,@EnableTurbine註解包含了@EnableDiscoveryClient註解,即開啓了註冊服務。

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine
public class ServiceTurbineApplication {
 
    /**
     * http://localhost:8764/turbine.stream
     */
 
    public static void main(String[] args) {
        SpringApplication.run( ServiceTurbineApplication.class, args );
    }
}

配置文件application.yml:

spring:
  application.name: service-turbine
server:
  port: 8769
security.basic.enabled: false
turbine:
  aggregator:
    clusterConfig: default   # 指定聚合哪些集羣,多個使用","分割,默認爲default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
  appConfig: service-hi,service-la  ### 配置Eureka中的serviceId列表,表明監控哪些服務
  clusterNameExpression: new String("default")
  # 1. clusterNameExpression指定集羣名稱,默認表達式appName;此時:turbine.aggregator.clusterConfig需要配置想要監控的應用名稱
  # 2. 當clusterNameExpression: default時,turbine.aggregator.clusterConfig可以不寫,因爲默認就是default
  # 3. 當clusterNameExpression: metadata['cluster']時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

Turbine演示

依次開啓server、service-hi、service-la、service-turbine工程。 

打開瀏覽器輸入:http://localhost:8769/turbine.stream

依次請求:

http://localhost:8762/hi?name=whhttp://localhost:8763/hi?name=wh

 打開:http://localhost:8763/hystrix,輸入監控流http://localhost:8769/turbine.stream,點擊monitor stream,可以看到這個頁面聚合了2個service的hystrix dashbord數據。

 java B2B2C springmvc mybatis電子商務平臺源碼 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章