SpringCloud Finchley基础教程:6,断路由监控hystrix和turbine

本文介绍如何配置Hystrix Dashboard实现服务监控,并使用Turbine聚合多个服务的Hystrix状态,简化集群监控操作。涵盖依赖引入、启动类注解配置及配置文件设置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 开启hystrix dashboard的监控

  1. 引入pom依赖

    <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>
  2. 启动类上开启@EnableHystrixDashboard和@EnableCircuitBreaker

    @SpringCloudApplication
    @EnableHystrixDashboard
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }

    @SpringCloudApplication已经包含了@EnableCircuitBreaker

  3. 配置文件,不需要加特殊的配置

    server:
      port: 50003
    
    management:
      endpoints:
        web:
          exposure: 
            include: "*"
          cors:
            allowed-origins: "*"
            allowed-methods: "*"
    
    eureka:
      instance:
        prefer-ip-address: true
        status-page-url-path: /actuator/info
        health-check-url-path: /actuator/health
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://localhost:50000/eureka/
    
    spring:
      application:
        name: scfl-monitor-hystrix
  4. 启动后,访问:http://localhost:50003/hystrix

    这里写图片描述

    输入一个feign服务间调用,或者有hystrix filter的gateway地址,例http://localhost:50002/actuator/hystrix.stream,注意这里不是hystrix.stream,springboot2.0默认是actuator/hystrix.stream。

    这里写图片描述

    当50002的服务有hystrix调用时,这里才会出现图表。

2. 开启turbine监控

如果只开启hystrix的dashboard,监控每个服务的hystrix状态的话,需要一个一个服务去输入http://ip:port/actuator/hystrix.stream,非常麻烦。

通过turbine可以把所有服务的hystrix.stream聚合到一起,可以进行整体监控。

hystrix dashboard—->turbine.stream—>聚合集群下各个服务的hystrix.stream
turbine会把同一个conmmandKey的Hystrix命令合并

  1. 引入pom依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
    </dependency>
  2. 启动类上加上@EnableTurbine注解

    @SpringCloudApplication
    @EnableHystrixDashboard
    @EnableTurbine
    public class App {
    
        public static void main(String[] args) {
            //new SpringApplicationBuilder(ServiceRegistryApplication.class).web(true).run(args);
            SpringApplication.run(App.class, args);
        }
    
    }
  3. turbine的配置文件

    turbine:
      app-config: scfl-service-gateway    ##需要监控的服务名
      aggregator:
        clusterConfig: default    ##需要监控的服务集群名
      clusterNameExpression: new String("default")
      combine-host: true
      instanceUrlSuffix:
        default: actuator/hystrix.stream    ##key是clusterConfig集群的名字,value是hystrix监控的后缀,springboot2.0为actuator/hystrix.stream 
    
  4. 启动引用,访问http://localhost:50003/hystrix,再输入http://localhost:50003/turbine.stream 进行监控

    这里写图片描述

    可以看到,如果用turbine的话,监控地址为本服务的ip+port+/turbine.stream,如果不用turbine的话,监控地址为各个服务的ip+port+/actuator/hystrix.stream

  5. turbine配置文件详解
    turbine监控服务的配置:

        turbine:
          app-config: i5xforyou-biz-kanjia,i5xforyou-service-gateway    ##需要监控的服务名
          aggregator:
            clusterConfig: kanjia,gateway    ##需要监控的服务集群名,default
          clusterNameExpression: metadata['cluster']      ##new String("default")
          combine-host: true
          instanceUrlSuffix:
            kanjia: kanjia/actuator/hystrix.stream    ##key为clusterConfig的集群名字,默认为default
            gateway: actuator/hystrix.stream    ##value为集群的hystrix监控url后缀,springboot2.0默认为actuator/hystrix.stream

    turbine被监控客户端的配置:需要配置集群名

    eureka:
      instance:
        metadata-map:
          cluster: kanjia

    default集群的stream路径为:http://ip:port/turbine.stream
    有cluster的stream路径为:http://ip:port/turbine.stream?cluster=xxxxx

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值