上一篇文章讲述了如何利用Hystrix Dashboard去监控断路器的Hystrix command。当我们有很多个服务的时候,这就需要聚合所有服务的Hystrix Dashboard的数据了。这就需要用到Spring Cloud的另一个组件了,即Hystrix Turbine

一、Hystrix Turbine简介

看单个的Hystrix Dashboard的数据并没有什么多大的价值,要想看这个系统的Hystrix Dashboard数据就需要用到Hystrix Turbine。Hystrix Turbine将每个服务Hystrix Dashboard数据进行了整合。Hystrix Turbine的使用非常简单,只需要引入相应的依赖和加上注解和配置就可以了。

二、准备工作

本文使用的工程为上一篇文章的工程,在此基础上进行改造。因为我们需要多个服务的Dashboard,所以需要再建一个服务,取名为service-lucy,它的基本配置同service-hi

三、创建service-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"> 
    <modelVersion>4.0.0</modelVersion> 
 
    <groupId>com.forezp</groupId> 
    <artifactId>service-turbine</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 
 
    <name>service-turbine</name> 
    <description>Demo project for Spring Boot</description> 
 
    <parent> 
        <groupId>com.forezp</groupId> 
        <artifactId>sc-f-chapter13</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> 
 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins> 
    </build> 
 
 
</project>

在其入口类ServiceTurbineApplication加上注解@EnableTurbine,开启turbine,@EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务。

package com.forezp.serviceturbine; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
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.web.bind.annotation.RestController; 
 
@SpringBootApplication 
@EnableEurekaClient 
@EnableDiscoveryClient 
@RestController 
@EnableHystrix 
@EnableHystrixDashboard 
@EnableCircuitBreaker 
@EnableTurbine 
public class ServiceTurbineApplication { 
 
    /** 
     * http://localhost:8764/turbine.stream 
     */ 
    public static void main(String[] args) { 
        SpringApplication.run(ServiceTurbineApplication.class, args); 
    } 
 
}

配置文件application.yml:

server: 
  port: 8764 
 
spring: 
  application: 
    name: service-turbine 
 
eureka: 
  client: 
    serviceUrl: 
      defaultZone: http://localhost:8761/eureka/ 
management: 
  endpoints: 
    web: 
      exposure: 
        include: "*" 
      cors: 
        allowed-origins: "*" 
        allowed-methods: "*" 
 
turbine: 
  app-config: service-hi,service-lucy 
  aggregator: 
    clusterConfig: default 
  clusterNameExpression: new String("default") 
  combine-host: true 
  instanceUrlSuffix: 
    default: actuator/hystrix.stream

四、Turbine演示

依次开启eureka-server、service-hi、service-lucy、service-turbine工程。

打开浏览器输入:http://localhost:8764/turbine.stream,界面如下:

依次请求:

http://localhost:8762/hi?name=forezp

http://localhost:8763/hi?name=forezp

打开:http://localhost:8763/hystrix,输入监控流http://localhost:8764/turbine.stream

点击monitor stream 进入页面:

 可以看到这个页面聚合了2个service的hystrix dashbord数据。

源码:IT虾米网


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!