業余草 SpringCloud教程 | 第十一篇: 斷路器監控(Hystrix Dashboard)(Finchley版本)
在我的第四篇文章斷路器講述了如何使用斷路器,並簡單的介紹了下Hystrix Dashboard組件,這篇文章更加詳細的介紹Hystrix Dashboard。
一、Hystrix Dashboard簡介
在微服務架構中為例保證程序的可用性,防止程序出錯導致網絡阻塞,出現了斷路器模型。斷路器的狀況反應了一個程序的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是作為斷路器狀態的一個組件,提供了數據監控和友好的圖形化界面。
二、準備工作
本文的的工程栗子,來源於第一篇文章的栗子,在它的基礎上進行改造。
三、開始改造service-hi
在pom的工程文件引入相應的依賴:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8<artifactId>spring-boot-starter-web</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-actuator</artifactId> 13 </dependency> 14<dependency> 15 <groupId>org.springframework.cloud</groupId> 16 <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> 17 </dependency> 18 <dependency> 19 <groupId>org.springframework.cloud</groupId> 20 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> 21 </dependency> 22 23 </dependencies>
其中,這三個依賴是必須的,缺一不可。
在程序的入口ServiceHiApplication類,加上@EnableHystrix註解開啟斷路器,這個是必須的,並且需要在程序中聲明斷路點HystrixCommand;加上@EnableHystrixDashboard註解,開啟HystrixDashboard
1 @SpringBootApplication 2 @EnableEurekaClient 3 @EnableDiscoveryClient 4 @RestController 5 @EnableHystrix 6 @EnableHystrixDashboard 7 @EnableCircuitBreaker 8 public class ServiceHiApplication { 9 10 /** 11 * 訪問地址 http://localhost:8762/actuator/hystrix.stream 12 * @param args 13 */ 14 15 public static void main(String[] args) { 16 SpringApplication.run( ServiceHiApplication.class, args ); 17 } 18 19 @Value("${server.port}") 20 String port; 21 22 @RequestMapping("/hi") 23 @HystrixCommand(fallbackMethod = "hiError") 24 public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) { 25 return "hi " + name + " ,i am from port:" + port; 26 } 27 28 public String hiError(String name) { 29 return "hi,"+name+",sorry,error!"; 30 } 31 32 }
運行程序: 依次開啟eureka-server 和service-hi.
四、Hystrix Dashboard圖形展示
打開http://localhost:8762/actuator/hystrix.stream,可以看到一些具體的數據:
打開locahost:8762/hystrix 可以看見以下界面:
在界面依次輸入:http://localhost:8762/actuator/hystrix.stream 、2000 、miya
;點確定。
在另一個窗口輸入: http://localhost:8762/hi?name=forezp
重新刷新hystrix.stream網頁,你會看到良好的圖形化界面:
源碼下載:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter12
五、參考資料
hystrix-dashboard
感謝您的關註!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群進群密碼:xttblog,想加微信群的朋友,可以微信搜索:xmtxtt,備註:“xttblog”,添加助理微信拉你進群。備註錯誤不會同意好友申請。再次感謝您的關註!後續有精彩內容會第一時間發給您!原創文章投稿請發送至[email protected]郵箱。商務合作可添加助理微信進行溝通!
業余草 SpringCloud教程 | 第十一篇: 斷路器監控(Hystrix Dashboard)(Finchley版本)