使用Turbine聚合监控数据

一、使用Turbine聚合监控数据

使用/hystrix.stream端点监控单个微服务实例。然而,使用微服务架构的应用系统一般会包含若干微服务,每个微服务通常都会部署多个实例。如果每次只能查看单个实例的监控数据,就必须在Hystrix Dashboard上切换想要监控的地址,这显然很不方便。如何解决该问题呢?

Turbine是一个聚合Hystrix监控数据的工具,它可将所有相关/hystrix.stream端点的数据聚合到一个组合的/turbine.stream中,从而让集群的监控更加方便。

这里我们使用Turbine监控2个微服务(也可监控更多)。

1、创建项目hystrix-turbine

pom.xml如下:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springclouddemo</groupId>
    <artifactId>hystrix-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-turbine</name>
    <description>turbine监控微服务</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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-server</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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、编写application配置文件

server.port=7300
spring.application.name=hystrix-turbine
eureka.client.service-url.defaultZone=http://localhost:7000/eureka/
eureka.instance.prefer-ip-address=true

turbine.app-config=eureka-client-consumer-hystrix,eureka-client-consumer-feign-fallback-stream
turbine.clusterNameExpression= new String("default")
turbine.aggregator.cluster-config=default
turbine.combine-host-port=true
turbine.instanceUrlSuffix=/hystrix.stream
  • turbine.app-config 配置监控服务的列表,表明监控哪些服务多个使用","分割;Turbine会在Eureka Server中找到eureka-client-consumer-hystrix,eureka-client-consumer-feign-fallback-stream这两个微服务,并聚合这两个微服务的监控数据
  • turbine.clusterNameExpression 用于指定集群名称,当服务数量非常多的时候,可以启动多个Turbine服务来构建不同的聚合集群,而该参数可以用来区分这些不同的聚合集群,同时该参数值可以再Hystrix仪表盘中用来定位不同的聚合集群
  • turbine.aggregator.cluster-config 指定聚合哪些集群,多个使用","分割,默认为default
  • turbine.combine-host-port 参数设置为true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,默认情况下会以host来区分不同的服务,这会使得在本机调试的时候,本机上的不同服务聚合成一个服务来统计。
  • turbine.instanceUrlSuffix 设置turbine 监控的默认路径

3、在main类添加@EnableTurbine注解

package com.springclouddemo.hystrixturbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

/**
 * @author 何昌杰
 */
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
@SpringBootApplication
public class HystrixTurbineApplication {

	public static void main(String[] args) {
		SpringApplication.run(HystrixTurbineApplication.class, args);
	}
}
  • @EnableHystrixDashboard 开启Hystrix仪表盘
  • @EnableTurbine 开启Turbine 聚合监控

测试:

  1. 启动项目eureka-server、eureka-client-provider、hystrix-turbine、eureka-client-consumer-hystrix、eureka-client-consumer-feign-fallback-stream
  2. 分别访问http://localhost:7206/hello/hcj、http://localhost:7209/hello/hcj
  3. 访问http://localhost:7300/hystrix,将http://localhost:7300/hystrix.stream添加监控,即可查看到聚合后的监控指标

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值