javascript
SpringCloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine
1. Hystrix Dashboard (斷路器:hystrix 儀表盤)?
Hystrix一個很重要的功能是,可以通過HystrixCommand收集相關數據指標. Hystrix Dashboard可以很高效的現實每個斷路器的健康狀況。
1). 在Ribbon服務g和Feign服務的Maven工程的pom.xml中都加入依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency>spring-boot-starter-actuator用于手機metric, 支持hystrix.stream。spring-cloud-starter-hystrix-dashboard支持dashboard的UI
2)在Spring Boot啟動類上用@EnableHystrixDashboard注解和@EnableCircuitBreaker注解。需要特別注意的是我們之前的Feign服務由于內置斷路器支持, 所以沒有@EnableCircuitBreaker注解,但要使用Dashboard則必須加,如果不加,Dashboard無法接收到來自Feign內部斷路器的監控數據,會報“Unable to connect to Command Metric Stream”錯誤
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClients@EnableCircuitBreaker@EnableHystrixDashboardpublic class ServiceFeignApplication {public static void main(String[] args) {SpringApplication.run(ServiceFeignApplication.class, args);}}3)然后就可以訪問/hystrix,這個URL將dashboard指向定義在Hystrix客戶端應用中的/hystrix.stream
在dashboard中輸入服務的URL:點擊 monitor后進入監控界面,訪問我們之前創建的Ribbon服務localhost:8901/, 或者Feign服務localhost:8902/可以看到監控UI動態變化
2. 利用Turbine在一個Dashboard上監控多個流
以上例子只能監控一個,要同時監控多個流怎么辦? 答案是, 可以單獨做一個Turbine服務,專門監控所有斷路器狀態,從而掌握整個系統中所有微服務的狀態。下面我們就來創建一個Turbine服務,來監控我們之前做的Feign服務和Ribbon服務
1). ?創建一個maven工程, 在pox.xml添加以下依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-turbine</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-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-hystrix-dashboard</artifactId></dependency>整個個pox.xml文件如下:
<?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>cm.chry</groupId><artifactId>spring.helloworld.turbine.service</artifactId><version>0.0.1-SNAPSHOT</version><name>spring.helloworld.turbine.service</name><description>Turbine service demo</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-turbine</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-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-hystrix-dashboard</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.RC1</version><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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>2). 創建Turbine Dashboard啟動類:?
用@EnableHystrixDashboard和@EnableTurbine修飾主類, 分別用于支持Hystrix Dashboard和Turbine
package spring.helloworld.turbine.service;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication@EnableHystrixDashboard@EnableTurbinepublic class DashboardApplication {public static void main(String[] args) {SpringApplication.run(DashboardApplication.class, args);}}3). 在application.yml中配置turbine參數
eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/server:port: 8903spring:application:name: hystrix-dashboard-turbineturbine:appConfig: service-feign, service-ribbonaggregator:clusterConfig: defaultclusterNameExpression: new String("default")turbine.appConfig定義了要監控的服務,這里是我們在前面章節創建的service-feign和sercice-ribbon; aggregator.clusterConfig定義了聚合方式, 此處為default.
turbine.appConfig?:配置Eureka中的serviceId列表,表明監控哪些服務
turbine.aggregator.clusterConfig?:指定聚合哪些集群,多個使用”,”分割,默認為default。可使用http://.../turbine.stream?cluster={clusterConfig之一}訪問
turbine.clusterNameExpression?:指定集群名稱,可以是三種類型的值
? ? ? ? ?- 默認表達式為appName;此時turbine.aggregator.clusterConfig需要配置想要監控的應用名稱;
? ? ? ? ?- 當為default時,turbine.aggregator.clusterConfig可以不寫,因為默認就是default;
? ? ? ? ?- 當為metadata[‘cluster’]時,假設想要監控的應用配置了eureka.instance.metadata-map.cluster: ABC,則需要配置,同時turbine.aggregator.clusterConfig: ABC
4). 依次啟動eureka服務, 2個Helloworld服務, Feign服務,ribbon服務和剛創建turbine服務。從eureka服務中我們可以看到
5)通過Turbine服務訪問HystrixDashborad, http:localhost:8903/hystrix
?
?監控流的URL填http://localhost:8903/turbine.stream, 點擊monitor stream, 進入監控頁面, 隨便刷新下feign和ribbon服務(http://localhost:8902/hello和http://localhost:8901), 可以看到監控頁面的變化。如下圖, 兩個服務的監控都會顯示在dashboard上
總結
以上是生活随笔為你收集整理的SpringCloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吴恩达机器学习笔记——第一章
- 下一篇: java 生成随机数_Java 生成随机