生成运行数据。
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 时,超出的部分会引起服务降级