生成運行數據。
Hystrix 只監控 @HystrixCommand ,只要想對服務進行監控,就必須加 @HystrixCommand,沒有降級方法也要加。web
收集運行數據。spring
展現運行數據。併發
spring-cloud.s06.dashboard
在pom
中添加依賴app
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies>
添加配置文件application.yml
spring-boot
server: port: 35001 spring: application: name: hystrix-dashboard profiles: active: dev eureka: instance: hostname: localhost instance-id: ${spring.cloud.client.ip-address}:${server.port} client: serviceUrl: defaultZone: http://user:123123@localhost:34001/eureka/
建立啓動類 ..HystrixDashboard
spa
@SpringBootApplication @EnableHystrixDashboard public class HystrixDashboard { public static void main(String[] args) { SpringApplication.run(HystrixDashboard.class, args); } }
http://localhost:35001/hystrix
可見控制檯。HystrixDashboard 只能展現被 @HystrixCommand 標記的方法的運行數據。這裏使用 store-hystrix 項目提供。想要向外提供hystrix 的監控數據,還須要完成一些讓工做。線程
完善項目 store-hystrix
的啓動類 ..StoreHystrix
,增長code
@Bean public ServletRegistrationBean getServlet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); //監控實例 ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); //servlet註冊接口 registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/actuator/hystrix.stream"); //路徑 registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
這樣,該項目的 /actuator/hystrix.stream
端點才能夠輸入數據。server
http://localhost:31003/actuator/hystrix.stream
能夠看到 ping
信息不停刷新。http://localhost:31003/hystrix/isolation/thread
,再從新回到 http://localhost:31003/actuator/hystrix.stream
能夠看到 data
一同刷新。http://localhost:31003/actuator/hystrix.stream
輸入到對話框,點擊 Monitor Stream 後能夠看到一個空的運行曲線。接下來訪問 http://localhost:31003/hystrix/isolation/thread 讓標記 @HystrixCommand 的程序運行,能夠看到監控數據的變化。
blog
左邊圖標中,不一樣意義的數字用不一樣的顏色顯示,具體的含義對照右邊的說明。
可能產生異常的訪問
http://localhost:31003/hystrix/cf/always-exception
100% 會觸發服務降級http://localhost:31003/hystrix/isolation/thread
併發超過 10 時,超出的部分會引起服務降級http://localhost:31003/hystrix/isolation/semaphore
併發超過 10 時,超出的部分會引起服務降級