一、Hystrix Dashboard简介
在微服务架构中为了保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
本文我们将从两个方面来看Hystrix仪表盘的使用,一方面是监控单体应用,另一方面则整合Turbine,对集群进行监控。
背景
Hystrix除了隔离依赖服务的调用外,Hystrix还提供了近乎实时的监控,Hystrix会实时的,累加的记录所有关于HystrixCommand的执行信息,包括执行了每秒执行了多少请求,多少成功,多少失败等等,更多指标请查看:https://github.com/Netflix/Hystrix/wiki/Metrics-and-Monitoring导出监控数据有了这些指标,Netflix还提供了一个类库(hystrix-metrics-event-stream:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-metrics-event-stream)把这些指标信息以‘text/event-stream’的格式开放给外部使用,用法非常简单,首先,把hystrix-metrics-event-stream库添加到项目中:
创建项目hystrix-dashboard
- 启动类添加注解 @EnableHystrixDashboard
- 更新 application.properties
1 2
spring.application.name=hystrix-dashboard server.port=2259
- 访问 http://127.0.0.1:2259/hystrix
更新项目 ribbon-consumer
- 更新 ConsumerApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.baoguoding.consumer;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate;
@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "compute-service", configuration = TestConfiguration.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value=ExcludeFromComponentScan.class)})
public class ConsumerApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1); //系统启动时加载顺序
registrationBean.addUrlMappings("/hystrix.stream");//路径
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- 更新 application.properties
1
2
3
4
5
spring.application.name=consumer-service
server.port=2230
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
feign.hystrix.enabled=true
- 更新 pom.xml
1
2
3
4
5
6
7
8
9
<!--增加hystrix的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 一定要先访问 http://127.0.0.1:2230/add?a=1&b=22
- 再访问 http://127.0.0.1:2230/hystrix.stream