断路器监控Hystrix Dashboard的使用
一、Hystrix Dashboard
作用:
实时监控各个Hystrix commandKey的各种指标值
通过 Dashboards 的实时监控来动态修改各种配置
仪表盘:
二、启动Hystrix Dashboard
1、下载 standalone-hystrix-dashboard-1.5.3-all.jar
下载地址:https://search.maven.org/search ,选框中输入 standalone-hystrix-dashboard 查询,然后下载。
2、启动Hystrix Dashboard
java -jar -DserverPort=7979 -DbindAddress=localhost standalone-hystrix-dashboard-1.5.3-all.jar
注意:其中的serverPort、bindAddress是可选参数,若不添加,默认是7979和localhost
3、检测是否启动成功
浏览器输入http://localhost:7979/hystrix-dashboard/,出现小熊页面就是正确了。
三、代码
1、pom.xml
<!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-core --> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-core</artifactId> <version>${hystrix-version}</version> </dependency> <!-- https://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-javanica --> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> <version>${hystrix-version}</version> </dependency> <!-- http://mvnrepository.com/artifact/com.netflix.hystrix/hystrix-metrics-event-stream --> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-metrics-event-stream</artifactId> <version>${hystrix-version}</version> </dependency>
说明:
hystrix-core:hystrix核心接口包。
hystrix-metrics-event-stream:只要客户端连接还连着,hystrix-metrics-event-stream就会不断的向客户端以 text/event-stream 的形式推送计数结果。
2、HystrixConfig.java
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect; 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 HystrixConfig { @Bean @HystrixProperty(name = "circuitBreaker.enabled", value = "true") public HystrixCommandAspect hystrixCommandAspect() { return new HystrixCommandAspect(); } /** * Send stream message to Hystrix-dashboard */ @Bean public ServletRegistrationBean hystrixMetricsStreamServlet() { ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); registrationBean.addUrlMappings("/hystrix.stream"); return registrationBean; } }
3、Controller代码
4、测试
浏览器访问 http://localhost:7979/hystrix-dashboard/ ,在控制台输入spring-boot 应用的监控的地址。
说明: 启动服务后,输入 http://localhost:8080/hystrix.stream ,之后点击“Add Stream”,最后点击“Monitor Stream”即可。
说明:
hystrix2/test1 - commandKey(默认是方法名,可以通过@HystrixCommand的属性commandKey指定。存在2个方法名一样时必须指定)
HystrixController2 - ThreadPoolKey(默认取类名,可以通过@HystrixCommand的属性threadPoolKey指定)