上一篇文章讲述了如何利用Hystrix Dashboard去监控断路器的Hystrix command。当我们有很多个服务的时候,此时就需要聚合监控Hystrix Dashboard的数据。这就需要用到Spring Cloud的另一个组件了,即Hystrix Turbine。
一、Hystrix Turbine简介
单独的Hystrix Dashboard的数据并没有什么多大的价值,要想看整个系统的Hystrix Dashboard数据就需要用到Hystrix Turbine。Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。Hystrix Turbine的使用非常简单,只需要引入相应的依赖和加上注解和配置就可以了。
二、准备工作
复用上一篇文章的SpringCloud_Client工程,并且创建一个SpringCloud_Client2工程、内容和结构基本相同,不同的地方如下所示:(这个application.properties属于SpringCloud_Client2),还需要在SpringCloud_Client工程的application.properties文件里面添加management.context-path=/actuator这个标签
server.port=8763
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
spring.application.name=service-lucky
management.endpoints.web.exposure.include=*
management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods=*
management.context-path=/actuator
package com.zit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
/*@EnableEurekaClient表明自己是个eureka客户端*/
@SpringBootApplication
@EnableEurekaClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class EurekaClientApplicationTwo {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplicationTwo.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/lucky")
@HystrixCommand(fallbackMethod = "luckyError")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "lucky " + name + " ,i am from port:" + port;
}
public String luckyError(String name) {
return "hi,"+name+",sorry,luckyError!";
}
}
三、创建SpringCloud_Server_Turbine工程
pom.xml内容如下:
<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">
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringCloud_Server_Turbine</artifactId>
<packaging>jar</packaging>
<name>SpringCloud_Server_Turbine</name>
<parent>
<groupId>com</groupId>
<artifactId>SpringCloud_Common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<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-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-hystrix</artifactId>
</dependency>
<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>
</dependency>
</dependencies>
</project>
application.properties内容如下
server.port=8764
spring.application.name=service-turbine
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
management.endpoints.web.exposure.include=*
management.endpoints.web.cors.allowed-origins=*
management.endpoints.web.cors.allowed-methods=*
turbine.app-config=service-hi,service-lucy
turbine.aggregator.clusterConfi=default
turbine.clusterNameExpression=new String("default")
turbine.combine-host=true
turbine.instanceUrlSuffix=/actuator/hystrix.stream
SpringCloudServerTurbine内容如下,@EnableTurbine表示开启turbine,@EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务。
package com.SpringCloud_Server_Turbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
@EnableTurbine
public class SpringCloudServerTurbine {
public static void main( String[] args )
{
SpringApplication.run(SpringCloudServerTurbine.class, args);
}
}
四、Turbine演示
依次启动SpringCloud_Server、SpringCloud_Client、SpringCloud_Client2和SpringCloud_Server_Turbine四个工程
1、打开浏览器输入:http://localhost:8764/turbine.stream,界面如下
2、打开浏览器依次输入http://localhost:8762/hi?name=tom,http://localhost:8763/lucky?name=tom
3、 打开浏览器输入:http://localhost:8763/hystrix,界面如下
4、在上面的界面中依次输入http://localhost:8764/turbine.stream、2000、aa,然后点击Monitor Stream
可以看到这个页面聚合了2个service的hystrix dashbord数据。
5、打开浏览器输入:http://localhost:8764/turbine.stream,界面如下