天天看點

Spring Cloud 2.x系列之斷路器叢集監控Turbine

前幾篇已經實作了對單個服務執行個體的監控,當然在實際應用中,單個執行個體的監控資料沒有多大的價值,我們其實更需要的是一個叢集系統的監控資訊,這時就需要引入Turbine。Turbine能夠彙集監控資訊,并将聚合後的資訊提供給Hystrix Dashboard來集中展示和監控。

本文将結合之前學習的注冊中心Eureka、服務提供者Provider、斷路器Hystrix和儀表盤Dashboard,學習斷路器叢集監控Turbine,最終整體架構如下:

Spring Cloud 2.x系列之斷路器叢集監控Turbine

項目說明:

注冊中心:

    sc-eureka-server

服務提供者:

    sc-server-turbine-hystrix-dashboard-node1

    sc-server-turbine-hystrix-dashboard-node2

服務消費者、斷路器:

    sc-client-turbine-hystrix-dashboard-node1

    sc-client-turbine-hystrix-dashboard-node2

叢集監控、儀表盤:

    sc-client-turbine-hystrix

1、注冊中心項目sc-eureka-server參考

Spring Cloud 2.x系列之eureka注冊中心單機 2、服務提供者項目sc-server-turbine-hystrix-dashboard-node1、sc-server-turbine-hystrix-dashboard-node2由項目sc-eureka-client-provider改造而來。 參考 Spring Cloud 2.x系列之服務注冊&服務提供者 sc-server-turbine-hystrix-dashboard-node1:
Spring Cloud 2.x系列之斷路器叢集監控Turbine
sc-server-turbine-hystrix-dashboard-node2:
Spring Cloud 2.x系列之斷路器叢集監控Turbine

3、服務消費者、斷路器項目sc-client-turbine-hystrix-dashboard-node1、sc-client-turbine-hystrix-dashboard-node2由項目sc-feign-hystrix-dashboard改造而來。

sc-client-turbine-hystrix-dashboard-node1

(1)bootstrap.yml修改

server:

  port: 5801           

(2)application.yml檔案修改

Spring Cloud 2.x系列之斷路器叢集監控Turbine

(3)去掉springcloud啟動類的EnableHystrixDashboard注解

sc-client-turbine-hystrix-dashboard-node2

server:

  port: 5802           
Spring Cloud 2.x系列之斷路器叢集監控Turbine

注: turbine隻能監控Hystrix服務。不是Hystrix的服務不能監控。是以配置檔案都配置了如下配置項

Spring Cloud 2.x系列之斷路器叢集監控Turbine

4、 建立叢集監控、儀表盤項目sc-client-turbine-hystrix,對應的pom.xml檔案如下

<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>spring-cloud</groupId>

   <artifactId>sc-client-turbine-hystrix</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <packaging>jar</packaging>


   <name>sc-client-turbine-hystrix</name>

   <url>http://maven.apache.org</url>



  <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>2.0.4.RELEASE</version>

   </parent>

   <dependencyManagement>

      <dependencies>

        <dependency>

           <groupId>org.springframework.cloud</groupId>

           <artifactId>spring-cloud-dependencies</artifactId>

           <version>Finchley.RELEASE</version>

           <type>pom</type>

           <scope>import</scope>

        </dependency>


      </dependencies>

   </dependencyManagement>



   <properties>

      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

      <maven.compiler.source>1.8</maven.compiler.source>

      <maven.compiler.target>1.8</maven.compiler.target>

   </properties>


   <dependencies>

   <!-- <dependency>

         <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-turbine</artifactId>

         <version>1.4.5.RELEASE</version>

      </dependency>-->


      <dependency>

          <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-netflix-turbine</artifactId>

      </dependency>


   <!-- <dependency>

         <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

         <version>1.4.5.RELEASE</version>

      </dependency>-->



      <dependency>

          <groupId>org.springframework.cloud</groupId>

          <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

      </dependency>


      <dependency>

          <groupId>org.springframework.boot</groupId>

          <artifactId>spring-boot-starter-actuator</artifactId>

      </dependency>

      <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

      </dependency>

   </dependencies>

</project>           

5、 建立配置檔案application.yml

spring:

  application:

    name: sc-client-turbine-hystrix

eureka:

  client:

    registerWithEureka: true #是否将自己注冊到Eureka服務中,預設為true

    fetchRegistry: true #是否從Eureka中擷取注冊資訊,預設為true

    serviceUrl:

      defaultZone:http://localhost:5001/eureka/  

server:

  port: 9091


turbine:

  appConfig: sc-client-turbine-hystrix-dashboard-node1,sc-client-turbine-hystrix-dashboard-node2 # 配置Eureka中的serviceId清單,表明監控哪些服務

  clusterNameExpression: newString("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

  instanceUrlSuffix: /hystrix.stream

  aggregator:

    clusterConfig: default  # 指定聚合哪些叢集,多個使用","分割,預設為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}通路           

6、建立springboot啟動類

package sc.client.turbine;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.cloud.netflix.turbine.EnableTurbine;

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



   public static void main(String[] args) {

        SpringApplication.run(TurbineApplication.class, args);

   }


}           

7、按照如下先後順序啟動項目

(1)sc-eureka-server

(2)sc-server-turbine-hystrix-dashboard-node1

        sc-server-turbine-hystrix-dashboard-node2

(3)sc-client-turbine-hystrix-dashboard-node1

        sc-client-turbine-hystrix-dashboard-node2

(4)sc-client-turbine-hystrix

8、通路注冊中心http://127.0.0.1:5001/确認服務都啟動成功

Spring Cloud 2.x系列之斷路器叢集監控Turbine

9、驗證Turbine叢集監控

通路http://localhost:9091/turbine.stream,傳回如下資料

Spring Cloud 2.x系列之斷路器叢集監控Turbine

并且會不斷重新整理以擷取實時的監控資料,說明與單個的監控類似,傳回監控項目的資訊。進行圖形化監控檢視,輸入http://localhost:9091/hystrix,傳回如下界面

Spring Cloud 2.x系列之斷路器叢集監控Turbine

輸入http://localhost:9091/turbine.stream,然後點選Monitor Stream ,可以看到出現如下界面

Spring Cloud 2.x系列之斷路器叢集監控Turbine

沒有出現任何監控圖表。分别通路http://127.0.0.1:5801/feign/user/getUser/1和http://127.0.0.1:5802/feign/user/listUser(盡量多通路幾次)

Spring Cloud 2.x系列之斷路器叢集監控Turbine

傳回檢視Turbine叢集監控圖表界面

Spring Cloud 2.x系列之斷路器叢集監控Turbine

 源碼:

https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-client-turbine-hystrix-dashboard-node1
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-client-turbine-hystrix-dashboard-node2
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-server-turbine-hystrix-dashboard-node1
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-server-turbine-hystrix-dashboard-node2
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-client-turbine-hystrix           

本文作者: java樂園

本文來自雲栖社群合作夥伴“ 

java樂園

”,了解相關資訊可以關注“

https://mp.weixin.qq.com/s?src=11×tamp=1538989706&ver=1170&signature=8sw4zcIRWtweR04Pyln8IYIexto9U9PbfOQfEfJj5-js5g8bEQIxY6we1IhvjhXe3UU3Ps8vJN5hv7f6mI*43moB97kUgzXc9-m5Bg5AIo*iddX7XtKvy3R8yi17TNZ5&new=1

上一篇: 加密和解密