Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix Dashboard上, 这个工具就是Turbine.
---纯洁的微笑
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
上面这个是纯洁大佬的引入,但是引进去不太对,
应该是版本的问题,so、、、继续填坑。
旧版本:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
新版本:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
只要说找不到类或者什么,一律按版本问题解决,换最新的版本,基本都可以解决。如果能力更强的可以看一下源代码,这样就可以在配置文件里确定到底怎么配置。
2、启动类
启动类添加启用Hystrix Dashboard和熔断器
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
3、测试
启动工程后访问 http://localhost:8031/hystrix,将会看到如下界面:
图中会有一些提示:
Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream
Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
Single Hystrix App: http://hystrix-app:port/hystrix.stream
大概意思就是如果查看默认集群使用第一个url,查看指定集群使用第二个url,单个应用的监控使用最后一个,我们暂时只演示单个应用的所以在输入框中输入: http://localhost:9001/hystrix.stream ,输入之后点击 monitor,进入页面。
如果没有请求会先显示Loading ...
,访问http://localhost:9001/hystrix.stream 也会不断的显示ping。
请求服务http://localhost:9001/hello/neo,就可以看到监控的效果了,首先访问http://localhost:9001/hystrix.stream,显示如下:
ping:
data: {"type":...}
data: {"type":...}
说明已经返回了监控的各项结果
到监控页面就会显示如下图:
其实就是http://localhost:9001/hystrix.stream返回结果的图形化显示,Hystrix Dashboard Wiki上详细说明了图上每个指标的含义,如下图:
到此单个应用的熔断监控已经完成。
以上是老版本,新版本这样配置的话会出现;
springboot 版本如果是2.0则需要添加 ServletRegistrationBean 因为springboot的默认路径不是 "/hystrix.stream",只要在自己的项目里配置上下面的servlet就可以了
//这个bean主要是解决/hystrix.stream视图无法打开的问题;
@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;
}
配置文件加这个:
hystrix.dashboard.proxy-stream-allow-list=*
所以springboot最大的难度就是版本匹配的问题,但是第一就是查一下匹配,第二就是读一下源码,就知道是不是哪里没有装载和配置,但是对于我来说,读源码还是有点困难,慢慢来吧。
Turbine
在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。
1、添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</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-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
2、配置文件
server.port=8041
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/mdy
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.application.name=hystrix-dashboard-turbine
feign.circuitbreaker.enabled=true
# 配置Hystrix turbine
turbine.appConfig=spring-cloud-consumer1,spring-cloud-consumer2
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
management.endpoints.web.exposure.include= *
turbine.instanceUrlSuffix.default = /hystrix.stream
hystrisfeign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8011/eureka/
# 配置Hystrix Metrics Stream
#management.endpoints.web.exposure.include=hystrix.stream
hystrix.dashboard.proxy-stream-allow-list=*
##设置hystrix超时时间(毫秒),设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑,默认1s
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds =30000
#hystrix.config.stream.maxConcurrentConnections= 50
turbine.appConfig
:配置Eureka中的serviceId列表,表明监控哪些服务turbine.aggregator.clusterConfig
:指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}
访问turbine.clusterNameExpression
: 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig
需要配置想要监控的应用名称;2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig
可以不写,因为默认就是default;3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC
,则需要配置,同时turbine.aggregator.clusterConfig: ABC
3、启动类
启动类添加@EnableTurbine
,激活对Turbine的支持
@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}
}
到此Turbine(hystrix-dashboard-turbine)配置完成
4、测试
在示例项目spring-cloud-consumer-hystrix基础上修改为两个服务的调用者
spring-cloud-consumer1
spring-cloud-consumer2
hystrix-dashboard-turbine
输入:http://localhost:8041/turbine.stream
这里面有两个,说明注册的Remote有两个方法,取决于consumer里的remote。
坑:
解决方法
因为网关中整合了hystrix,hystrix默认熔断的时间是1s, 所以当网关路由请求到下游服务的时候,下游的服务处理时间大于1s,导致网关触发1s超时,关闭链接,因此会出现上面的错误
方法:在网关的yml文件的延长hystrix熔断时间,,具体时间根据真实业务来定,本博客中修改成30s
##设置hystrix超时时间(毫秒),设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑,默认1s
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds =30000
完工。