1.引言
我们在之前讲过,在单个的Hystrix的应用中,我们使用/hystrix.stream可以查看监控数据,如果想以图表的形式更直观的查看监控数据,再结合dashboard就可以了。
但是,如何同时监控多个应用或集群呢?
我们需要使用springcloud提供的Turbine,它是将各个应用的/hystrix.stream进行聚合的组件。我们在dashboard中输入/turbine.stream即可查看所有监控应用的健康情况。
Turbine 是聚合服务器发送事件流数据的一个工具,hystrix 的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过 turbine 来监控集群服务。

2.案例介绍
本案例中我们启动两个consumer,集成的有dashboard。创建一个新的turbine项目,通过turbine项目类聚合监控数据,然后通过dashboard-view项目来展示可视化的监控数据。
2.1.创建turbine项目

2.2.添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud0310</artifactId>
<groupId>com.bruceliu.springcloud0310</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-hystrix-turbine</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--服务熔断组件-->
<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>
<!-- hystrix和 hystrix-dashboard相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- SpringCloud的依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.3.配置文件设置
配置文件中添加turbine的设置
spring:
application:
name: spring-cloud-hystrix-turbine
server:
port: 8810
turbine:
aggregator:
#监控所有微服务集群
#hytrix仪表盘:http://localhost:8810/hystrix/
#监控地址:http://localhost:8810/turbine.stream
#在hystrix仪表盘中监控上面的地址即可
clusterConfig: default
#要监控的微服务serviceId
appConfig: springcloud-consumer-turbine-test,springcloud-consumer-demo
clusterNameExpression: "'default'"
instanceUrlSuffix: /hystrix.stream
eureka:
client:
serviceUrl:
defaultZone: http://bruceliu:123456@eureka7002.com:7002/eureka,http://bruceliu:123456@eureka7001.com:7001/eureka,http://bruceliu:123456@eureka7003.com:7003/eureka
instance:
prefer-ip-address: true
instanceId: spring-cloud-hystrix-turbine:8810
2.4.启动类修改
package com.bruceliu;
/**
* @BelongsProject: springcloud0310
* @BelongsPackage: com.bruceliu
* @Author: bruceliu
* @QQ:1241488705
* @CreateTime: 2020-03-11 23:34
* @Description: TODO
*/
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.beans.factory.annotation.Value;
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.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
/**
* springboot 版本如果是2.0则需要添加 ServletRegistrationBean
* 因为springboot的默认路径不是 "/hystrix.stream"
* @return
*/
@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;
}
}
2.5.分别启动两个consumer

服务一:


服务二:


2.6.启动turbine服务
hytrix仪表盘:http://localhost:8810/hystrix/

监控地址:http://localhost:8810/turbine.stream

2.7.启动dashboard-view服务
如下即实现了同时监控多个服务的数据。

3.容易踩的坑
springboot2.1.4版本,这个版本默认路径不是/hystrix.stream,而turbine默认路径是这个,所以要修改一下,可以有两种修改方式:
一: 配置Bean,设置路径
/**
* springboot 版本如果是2.0则需要添加 ServletRegistrationBean
* 因为springboot的默认路径不是 "/hystrix.stream"
* @return
*/
@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;
}
二: 配置文件配置Bean

页面不显示服务:
这个问题挺坑的,具体原因是因为监控的服务不光要开启熔断和仪表盘,同样也要开启turbine,即启动类需要以下注解:

一直loading:
最后是这个,实际中发现无论怎么访问接口,就是没监控数据,后台测试发现,你所访问的接口必须要有熔断,即普通方法要有fallback:
@RequestMapping("/test")
@ResponseBody
@HystrixCommand(fallbackMethod = "fallback1")
public List<User> consumerTest() {
return userClientService.queryALLUsers();
}
public List<User> fallback1(){
return null;
}
567



被折叠的 条评论
为什么被折叠?



