注意:
- hystrix基本使用:第十九章 springboot + hystrix(1)
- hystrix計數原理:附6 hystrix metrics and monitor
一、hystrixdashboard
作用:
- 監控各個hystrixcommand的各種值。
- 通過dashboards的實時監控來動態修改配置,直到滿意為止
儀表盤:
二、啟動hystrix
1、下載standalone-hystrix-dashboard-1.5.3-all.jar
- https://github.com/kennedyoliveira/standalone-hystrix-dashboard:該頁面提供了一個很好的視頻教學。
2、啟動hystrix-dashboard
java -jar -DserverPort=7979 -DbindAddress=localhost standalone-hystrix-dashboard-1.5.3-all.jar
- 注意:其中的serverPort、bindAddress是可選參數,若不添加,默認是7979和localhost
3、測試
- 瀏覽器輸入http://localhost:7979/hystrix-dashboard/,出現小熊頁面就是正確了。
三、代碼
1、pom.xml
1 <dependency> 2 <groupId>com.netflix.hystrix</groupId> 3 <artifactId>hystrix-core</artifactId> 4 <version>1.4.10</version> 5 </dependency> 6 <!-- http://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-metrics-event-stream --> 7 <dependency> 8 <groupId>com.netflix.hystrix</groupId> 9 <artifactId>hystrix-metrics-event-stream</artifactId> 10 <version>1.4.10</version> 11 </dependency>
說明:
- hystrix-core:hystrix核心接口包
- hystrix-metrics-event-stream:只要客戶端連接還連着,hystrix-metrics-event-stream就會不斷的向客戶端以text/event-stream的形式推送計數結果(metrics)
2、配置HystrixMetricsStreamServlet
1 package com.xxx.firstboot.hystrix.dashboard; 2 3 import org.springframework.boot.context.embedded.ServletRegistrationBean; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 7 import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 8 9 @Configuration 10 public class HystrixConfig { 11 12 @Bean 13 public HystrixMetricsStreamServlet hystrixMetricsStreamServlet(){ 14 return new HystrixMetricsStreamServlet(); 15 } 16 17 @Bean 18 public ServletRegistrationBean registration(HystrixMetricsStreamServlet servlet){ 19 ServletRegistrationBean registrationBean = new ServletRegistrationBean(); 20 registrationBean.setServlet(servlet); 21 registrationBean.setEnabled(true);//是否啟用該registrationBean 22 registrationBean.addUrlMappings("/hystrix.stream"); 23 return registrationBean; 24 } 25 }
說明:以上方式是springboot注入servlet並進行配置的方式。
四、測試
說明:啟動服務后,輸入localhost:8001/hystrix.stream,之后點擊"Add Stream",最后點擊"Monitor Stream"即可。
說明:
- getHotelInfo - commandKey(其實就是servicename下的一個方法)
- hotelService - ThreadPoolKey(不配置的情況下就是commandGroupKey,其實就是servicename)