詳解斷路器監控和聚合監控
2018-09-18 16:23來源:
原標題:詳解斷路器監控和聚合監控
今天我們深入學習斷路器監控hystrix dashboard,之前我們有過簡單的使用。在微服務架構中為例保證程序的可用性,防止程序出錯導致網絡阻塞,出現了斷路器模型。斷路器的狀況反應了一個程序的可用性和健壯性,它是一個重要指標。hystrix dashboard是作為斷路器狀態的一個組件,提供了數據監控和友好的圖形化界面。
一.準備工程
建議大家新建工程,因為這樣其他的單獨的服務只要進行了熔斷機制的,都可以在儀表盤上面去查看,此外,如果dashboard和被監控工程整合到了一起,即便配置feign.hystrix.enabled=true 也是檢測不到 feign 通道的數據的,在儀表盤界面一直是 loading。所以,如果使用feign ,建議dashboard成為獨立的項目。
1.創建hystrix-dashboard,引入依賴
!--客戶端負載均衡--
dependency
groupidorg.springframework.cloud/groupid
artifactidspring-cloud-starter-ribbon/artifactid
/dependency
!--eureka客戶端--
dependency
groupidorg.springframework.cloud/groupid
artifactidspring-cloud-starter-eureka/artifactid
/dependency
!--hystrix斷路器--
dependency
groupidorg.springframework.cloud/groupid
artifactidspring-cloud-starter-hystrix/artifactid
/dependency
dependency
groupidorg.springframework.boot/groupid
artifactidspring-boot-starter-actuator/artifactid
/dependency
!--hystrix斷路器儀表盤--
dependency
groupidorg.springframework.cloud/groupid
artifactidspring-cloud-starter-hystrix-dashboard/artifactid
/dependency
2.yml配置文件
eureka:
client:
serviceurl:
defaultzone:localhost:8761/eureka/
spring:
application:
name:hystrix-dashboard
server:
port:9000
3.創建一個consumercontroller控制器
@restcontroller
publicclassconsumercontroller{
@autowired
privateresttemplateresttemplate;
@hystrixcommand(fallbackmethod=defaultstores)
@getmapping(value=/hello)
publicstringhello(){
returnresttemplate.getforentity(eureka-client/,string.class).getbody();
}
publicstringdefaultstores(){
returnribbon+hystrixdashboard,提供者服務已失效;
}
}
4.啟動類上添加注解
@enablehystrix
@enablediscoveryclient
@enablehystrixdashboard
@springbootapplication
publicclassribbonconsumerapplication{
@loadbalanced
@bean
resttemplateresttemplate(){
returnnewresttemplate();
}
publicstaticvoidmain(string[]args){
springapplication.run(ribbonconsumerapplication.class,args);
}
}
5.啟動工程
拿出我們以前的小寶貝兒們,eureka-server,eureka-client,然后我們依次啟動他們,eureka-client記得至少要啟動兩個不同端口,最后啟動hystrix-dashboard。啟動好之后去localhost:8761看一下注冊有沒有成功。
訪問localhost:8766/hystrix.stream顯示
接下來我們就該訪問localhost:8766/hystrix.stream了,但是有的小伙伴會發生如下問題:
無限ping,這是為什么呢?是因為我們還沒有通過8766儀表盤訪問過兩個client,所以我們需要先訪問一下他們
接下來我們再去訪問localhost:8766/hystrix.stream
我們可以看到有大量數據了,這就說明我們剛剛已經成功調用了服務,并且監控已經記錄,現在我們需要去localhost:8766/hystrix輸入信息“localhost:8766/hystrix”,“2000”,“hi”。
點擊下面的monitor stream然后我們就能看到還算美觀的儀表盤界面:
說了這么多,他們都分別是什么意思呢,我們來看(嘔心力作之圖和一張實際生產的圖)。
以上便是hystrix dashboard的一個小詳解。
二.聚合監控 hystrix turbine
上邊我們講述了如何利用hystrix dashboard去監控斷路器的hystrix command。當我們有很多個服務的時候,看單個的hystrix dashboard的數據并沒有什么多大的價值,要聚合所以服務的hystrix dashboard的數據了。這就需要用到spring cloud的另一個組件了,即hystrix turbine。要想看這個系統的hystrix dashboard數據就需要用到hystrix turbine。hystrix turbine將每個服務hystrix dashboard數據進行了整合。
1.創建service-turbine,引入依賴
!--mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-turbine--
dependency
groupidorg.springframework.cloud/groupid
artifactidspring-cloud-starter-turbine/artifactid
version1.1.3.release/version
/dependency
dependency
groupidorg.springframework.boot/groupid
artifactidspring-boot-starter-actuator/artifactid
/dependency
2.在入口類serviceturbineapplication加上注解@enableturbine,開啟turbine,@enableturbine注解包含了@enablediscoveryclient注解,即開啟了注冊服務。
@enableturbine
@springbootapplication
publicclassserviceturbineapplication{
publicstaticvoidmain(string[]args){
springapplication.run(serviceturbineapplication.class,args);
}
}
3.配置文件application.yml
spring:
application.name:service-turbine
server:
port:8769
security:
basic:
enabled:false
turbine:
aggregator:
clusterconfig:default#指定聚合哪些集群,多個使用,分割,默認為default。
可使用.../turbine.stream?cluster={clusterconfig之一}訪問
appconfig:service-hi,service-lucy###配置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
eureka:
client:
serviceurl:
defaultzone:localhost:8761/eureka/
4.啟動工程
依次開啟eureka-server、service-hi、service-lucy、service-turbine工程。
打開瀏覽器輸入:localhost:8769/turbine.stream,界面如下
依次請求:
localhost:8762/hi?name=imooc
localhost:8763/hi?name=imooc
打開:localhost:8763/hystrix,輸入監控流localhost:8769/turbine.stream
可以看到這個頁面聚合了2個service的hystrix dashbord數據。
以上便是所有spring cloud中我所想分享給大家的內容,以這些知識作為鋪墊,祝大家的技術更上一層樓~感謝大家閱讀!
責任編輯:
聲明:該文觀點僅代表作者本人,搜狐號系信息發布平臺,搜狐僅提供信息存儲空間服務。
閱讀 ()
來源:搜狐
以上是網絡信息轉載,信息真實性自行斟酌。