在個人第四篇文章斷路器講述瞭如何使用斷路器,並簡單的介紹了下Hystrix Dashboard組件,這篇文章更加詳細的介紹Hystrix Dashboard。git
在微服務架構中爲例保證程序的可用性,防止程序出錯致使網絡阻塞,出現了斷路器模型。斷路器的情況反應了一個程序的可用性和健壯性,它是一個重要指標。Hystrix Dashboard是做爲斷路器狀態的一個組件,提供了數據監控和友好的圖形化界面。github
本文的的工程栗子,來源於第一篇文章的栗子,在它的基礎上進行改造。web
在pom的工程文件引入相應的依賴:spring
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.架構
打開http://localhost:8762/actuator/hystrix.stream,能夠看到一些具體的數據:app
打開locahost:8762/hystrix 能夠看見如下界面:spring-boot
在界面依次輸入: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
感謝您的關注!可加QQ1羣:135430763,QQ2羣:454796847,QQ3羣:187424846。QQ羣進羣密碼:xttblog,想加微信羣的朋友,能夠微信搜索:xmtxtt,備註:「xttblog」,添加助理微信拉你進羣。備註錯誤不會贊成好友申請。再次感謝您的關注!後續有精彩內容會第一時間發給您!原創文章投稿請發送至532009913@qq.com郵箱。商務合做可添加助理微信進行溝通!