一、简介
Netflix提供的一个开源项目(Turbine),把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。
二、搭建 Hystrix Dashboard服务
<?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>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>多个服务的Hystrix Dashboard</description>
<artifactId>hystrix-dashboard-two</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-netflix-eureka-client</artifactId>
</dependency>
<!-- 熔断依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 断路器仪表盘依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
server:
# springboot设置随机端口号
port: 0
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
# 重写服务的唯一Id,防止多实例对被注册成一个服务
instance-id: ${spring.application.name}:${random.int}
# 设置优先使用ip进行注册
prefer-ip-address: true
spring:
application:
name: hystrix-dashboard-two
# actuator 安全检查开放消息推送刷新
management:
endpoints:
web:
exposure:
# 开放安全检查的端点
include: "*"
package com.mo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* EnableHystrixDashboard注解,开启HystrixDashboard,仪表盘监控服务
* SpringCloudApplication注解,支持服务注册,springboot启动,hystrix熔断
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/5/2 16:23
*/
@RestController
@SpringCloudApplication
@EnableHystrixDashboard
public class HystrixDashboardTwoApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardTwoApplication.class, args);
}
@GetMapping("/hello")
@HystrixCommand(fallbackMethod = "helloError")
public String hello() {
return "hello";
}
/**
* HystrixCommand 熔断的失败返回方法
*
* @return
*/
public String helloError() {
return "hello error";
}
/**
* 版本差异:添加actuator端点hystrix.stream
* <p>
* 访问地址 http://localhost:8762/actuator/hystrix.stream
*
* @return
*/
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/actuator/hystrix.stream");
return registration;
}
}
备注:hystrix-dashboard-two与hystrix-dashboard都是Hystrix Dashboard服务。
三、搭建Hystrix Turbine服务
<?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>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hystrix-turbine</artifactId>
<description>Hystrix Turbine综合Hystrix Dashboard数据</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- Hystrix Turbine依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
</dependencies>
</project>
server:
# springboot设置随机端口号
port: 8764
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
# 重写服务的唯一Id,防止多实例对被注册成一个服务
instance-id: ${spring.application.name}:${random.int}
# 设置优先使用ip进行注册
prefer-ip-address: true
spring:
application:
name: hystrix-turbine
# actuator 安全检查开放消息推送刷新
management:
endpoints:
web:
exposure:
# 开放安全检查的端点
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
turbine:
# turbine监控的服务
app-config: hystrix-dashboard,hystrix-dashboard-two
aggregator:
# 指定聚合哪些集群,默认default
cluster-config: default
# clusterNameExpression指定集群名称
clusterNameExpression: new String("default")
combine-host: true
instanceUrlSuffix:
default: actuator/hystrix.stream
package com.mo;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
/**
* EnableTurbine,开启turbine,综合HystrixDashboard的数据
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/5/2 17:12
*/
@SpringCloudApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixTurbine {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbine.class, args);
}
}
四、Hystrix Turbine演示
1. 访问 http://127.0.0.1:8764/turbine.stream
2. 访问 http://127.0.0.1:8764/hystrix
3. 添加 http://127.0.0.1:8764/turbine.stream端点
4. dashbord监控,访问dashbord服务的接口