Hystrix Turbine聚合监控

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Hystrix Turbine聚合监控

悠然予夏   2022-09-29 我要评论

之前,我们针对的是一个微服务实例的Hystrix数据查询分析,在微服务架构下,一个微服务的实例往往是多个(集群化)。

比如自动投递微服务

  • 实例1(hystrix) ip1:port1/actuator/hystrix.stream
  • 实例2(hystrix) ip2:port2/actuator/hystrix.stream
  • 实例3(hystrix) ip3:port3/actuator/hystrix.stream

按照已有的方法,我们就可以结合dashboard仪表盘每次输入一个监控数据流url,进去查看

手工操作能否被自动功能替代?Hystrix Turbine聚合(聚合各个实例上的hystrix监控数据)监控

Turbine(涡轮)

思考:微服务架构下,一个微服务往往部署多个实例,如果每次只能查看单个实例的监控,就需要经常切换很不方便,在这样的场景下,我们可以使用 HystrixTurbine 进行聚合监控,它可以把相关微服务的监控数据聚合在一起,便于查看。

Turbine服务搭建

(1)新建项目lagou-cloud-hystrix-turbine-9001,引入依赖坐标

<?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>lagou-parent</artifactId>
        <groupId>com.lagou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>lagou-cloud-hystrix-turbine-9001</artifactId>
    <dependencies>
        <!--hystrix turbine聚合监控-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
        <!--
            引⼊eureka客户端的两个原因
                1、⽼师说过,微服务架构下的服务都尽量注册到服务中⼼去,便于统⼀管理
                2、后续在当前turbine项⽬中我们需要配置turbine聚合的服务,⽐如,我们希望聚合lagou-service-autodeliver这个服务的各个实例的hystrix数据流,
                那随后我们就需要在application.yml⽂件中配置这个服务名,那么turbine获取服务下具体实例的数据流的时候需要ip和端⼝等实例信息,
                那么怎么根据服务名称获取到这些信息呢?
                当然可以从eureka服务注册中⼼获取
        -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>

2)将需要进行Hystrix监控的多个微服务配置起来,在工程application.yml中开启Turbine及进行相关配置

server:
  port: 9001
Spring:
  application:
    name: lagou-cloud-hystrix-turbine
eureka:
  client:
    serviceUrl: # eureka server的路径
      defaultZone: http://LagouCloudEurekaServerB:8762/eureka,http://LagouCloudEurekaServerA:8761/eureka #把 eureka 集群中的所有 url 都填写了进来,也可以只写⼀台,因为各个 eureka server 可以同步注册表
  instance:
    #使⽤ip注册,否则会使⽤主机名注册了(此处考虑到对⽼版本的兼容,新版本经过实验都是ip)
      prefer-ip-address: true
      #⾃定义实例显示格式,加上版本号,便于多版本管理,注意是ip-address,早期版本是ipAddress
      instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
#turbine配置
turbine:
# appCofing配置需要聚合的服务名称,⽐如这⾥聚合⾃动投递微服务的hystrix监控数据
# 如果要聚合多个微服务的监控数据,那么可以使⽤英⽂逗号拼接,⽐如 a,b,c
  appConfig: lagou-service-autodeliver
  clusterNameExpression: "'default'" # 集群默认名称

(3)在当前项目启动类上添加注解@EnableTurbine,开启仪表盘以及Turbine聚合

package com.lagou.edu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine // 开启turbine聚合功能
public class HystrixTurbineApplication9001 {
    public static void main(String[] args) {
        SpringApplication.run(HystrixTurbineApplication9001.class, args);
    }
}

(4)浏览器访问Turbine项目,http://localhost:9001/turbine.stream,就可以看到监控数据了

我们通过dashboard的页面查看数据更直观,把刚才的地址输入dashboard地址栏

监控页面

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们