上一篇咱们介绍了Hystrix的基础使用,咱们能够经过Hystrix作到依赖隔离和熔断等操做。可是只有工具的使用而没有监控,咱们就没法在第一时间发现出现问题的依赖,也不能判断服务总体的健康状态/运行状态。因此咱们还要作好相关的监控工做。java
Hystrix提供了监控页面,本篇主要介绍如何使用Hystrix Dashboard对服务的容错状况进行监控。git
框架 | 版本 |
---|---|
Spring Boot | 2.0.0.RELEASE |
Spring Cloud | Finchley.BUILD-SNAPSHOT |
JDK | 1.8.x |
参考:https://ken.io/note/spring-cloud-feign-quickstart
源码:https://github.com/ken-io/springcloud-course/tree/master/chapter-03/github
启动Eureka Server: http://localhost:8800
启动Test Service:http://localhost:8602web
基于上一篇Feign+Hystrix:https://ken.io/note/spring-cloud-hystrix-quickstart
源码(feignclient):https://github.com/ken-io/springcloud-course/tree/master/chapter-04/feignclientspring
基于feignclient项目使用Hystrix Dashboardapp
<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-dashboard</artifactId> </dependency>
修改App.java,增长 @EnableHystrixDashboard
注解框架
package io.ken.springcloud.feignclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableHystrixDashboard @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
新建package:configuration,而后在此package下建立HystrixConfiguration.java并添加hystrixRegistrationBeanspring-boot
package io.ken.springcloud.feignclient.configuration; import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HystrixConfiguration { @Bean(name = "hystrixRegistrationBean") public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean registration = new ServletRegistrationBean( new HystrixMetricsStreamServlet(), "/hystrix.stream"); registration.setName("hystrixServlet"); registration.setLoadOnStartup(1); return registration; } }
feignclient项目启动后,访问 http://localhost:8605/hystrix工具
将会看到Hystrix Dashboard导航页测试
Hystrix Dashboard导航页不会展现具体监控信息,而是提供三种选择:
默认的集群监控,经过URL:http://turbine-hostname:port/turbine.stream,查看默认集群的监控信息。
指定的集群监控,经过URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName],查看指定集群(clusterName)的监控信息。
单个实例的监控,经过URL:http://hystrix-app:port/hystrix.stream,查看具体某个服务实例的监控信息。
咱们先经过Hystrix Dashboard看一下单个实例的监控信息
输入指定链接:http://localhost:8605/hystrix.stream
Delay(查询监控信息的延迟时间),Tile能够自定义,也能够默认。填写完毕点击 Monitor Stream
便可
此时Hystrix监控面板会显示Loanding…,这是由于咱们还没经过feignclient访问外部接口,也就还没生成stream信息。
咱们经过feignclient访问几个外部接口,stream信息生成后,监控面板会自动刷新。
在访问了 http://localhost:8605/ti ,http://localhost:8605/plus?numa=1&numb=2
这两个接口后,监控信息就天然刷新了。不一样的接口默认会分开来记录。
监控图中用圆点来表示服务的健康状态,健康度从100%-0%分别会用绿色、黄色、橙色、红色来表示。
另外,这个圆点也会随着流量的增多而变大。
监控图中会用曲线(圆点旁边)来表示服务的流量状况,经过这个曲线能够观察单个接口的流量变化/趋势
上个图中是个很是小的绿色圆心,流量曲线也是个折现,是由于 ken.io 这里访问两个接口次数不多。
若是分别狂按F5快速访问这两个接口。这个圆心和曲线就会发生变化。
这时候这个圆点比着上一张图已经变大了,服务依旧是健康状态,因此圆点仍是绿色。
另外流量曲线随着刚才的快速刷新访问也升了上去。
为了更好的展现服务不一样健康状态下面板的变化,咱们快速访问 http://localhost:8605/ti , http://localhost:8605/plus?numa=1&numb=2,而后关闭testservice,快速的访问 http://localhost:8605/plus?numa=1&numb=2 ,这首的面板会更丰富。
这时候TestService#plusService的圆点就变成了红色。因为停用testservice后咱们没有访问 http://localhost:8605/ti ,因此这个时间点TestService#plusService的圆点仍是绿色,尺寸也更小。两个Service的流量曲线变化也跟咱们的操做相吻合。
上图是Hystrix Dashboard监控图表中各项指标的说明