turbine 英[ˈtɜ:baɪn] n. 汽輪機; 渦輪機; 透平機;
OK,上文我們看了一個監控單體應用的例子,在實際應用中,我們要監控的應用往往是一個集群,這個時候我們就得采取Turbine集群監控了。Turbine有一個重要的功能就是匯聚監控信息,並將匯聚到的監控信息提供給Hystrix Dashboard來集中展示和監控。那我們就來看看Turbine集群監控如何使用。本文通過引入Turbine來聚合ribbon-consumer服務的監控信息,並輸出給hystrix dashboard來進行展示。
先上部署拓撲圖:
構建turbine項目:
1、添加依賴,pom文件:主要是:spring-cloud-starter-turbine和spring-boot-starter-actuator
<?xml version="1.0" encoding="UTF-8"?> <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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dxz.turbine</groupId> <artifactId>turbine</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hystrix-dashboard</name> <description>dashboard project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <!--配合spring cloud版本 --> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <!--設置字符編碼及java版本 --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--增加turbine的依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--依賴管理,用於管理spring-cloud的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR3</version> <!--官網為Angel.SR4版本,但是我使用的時候總是報錯 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <!--使用該插件打包 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、添加注解
啟動類TurbineApplication
package com.dxz.turbine; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.turbine.EnableTurbine; @EnableTurbine @EnableDiscoveryClient @SpringBootApplication public class TurbineApplication { public static void main(String[] args) { SpringApplication.run(TurbineApplication.class, args); } }
3、修改配置 配置信息
spring.application.name=turbine server.port=2260 management.port=8990 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ turbine.app-config=ribbon-consumer turbine.cluster-name-expression="default" turbine.combine-host-port=true
其中說明:
1.turbine.app-config=ribbon-consumer指定了要監控的應用名字為ribbon-consumer
2.turbine.cluster-name-expression="default",表示集群的名字為default
3.turbine.combine-host-port=true表示同一主機上的服務通過host和port的組合來進行區分,默認情況下是使用host來區分,這樣會使本地調試有問題
查看監控圖
OK,監控服務創建成功之后,我們再次依次啟動eureka-server、provider和consumer,其中consumer啟動兩個實例,兩個實例的端口不一致,再分別啟動hystrix-dashboard和turbine,
啟動turbine項目,
http://127.0.0.1:2260/turbine.stream
將http://127.0.0.1:2260/turbine.stream填入dashboard里,
然后在hystrix監控地址欄輸入如下地址(監控之前要記得先訪問一下服務中的任意一個接口):http://localhost:2002/turbine...,訪問結果如下:
結果:小伙伴們可以看到,集群下的主機報告一欄顯示已經有所不同了。
與消息代理結合
詳細見《spring cloud微服務實戰》
2.2 turbine AMQP
在某些環境中(如在PaaS),典型的turbine模型的指標從所有分布式Hystrix命令不起作用。在這種情況下,你可能想要你Hystrix命令推動指標turbine,和spring cloud,就要使用AMQP消息傳遞。所有您需要做的是在客戶端添加一個依賴spring-cloud-netflix-hystrix-amqp並確保代rabbitmq可用。(有關詳細信息,請參閱彈簧引導文檔如何配置客戶端憑據,但它應該工作的當地代理或雲計算)。