`
白马探
  • 浏览: 14677 次
社区版块
存档分类
最新评论

SpringCloud:熔断监控Hystrix Dashboard和Turbine

 
阅读更多

1. Hystrix Dashboard

创建一个新的项目hystrix-dashboard,延用上一篇文章提到的eureka和producer两个项目。

1. hystrix-dashboard 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.springcloud</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-dashboard</name>
    <description>Demo project for Spring Boot</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-actuator</artifactId>
        </dependency>
        <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-client</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-openfeign</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>

 

前面介绍过的包我这里不再多说,讲几个前面没有见过的包:

  • actuator: 这个包是用来做服务监控的,很多监控相关的功能都会用到这个包,具体的内容我这里先不讲,后面会专门有一篇来讲这个。
  • hystrix-dashboard: 这个是今天的主角,hystrix-dashboard帮我们封装好了hystrix的监控面板。

2. 启动类 HystrixDashboardApplication.java

package com.springcloud.hystrixdashboard;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixDashboardApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

}

 

启动类添加启用Hystrix Dashboard和熔断器

 

注意: 各位看官这里一定要注意,我在这里注册了HystrixMetricsStreamServlet,在springboot1.x版本下,这里是无需注册的,在2.x版本后,这里才需要注册HystrixMetricsStreamServlet,并且显示的给出访问路径。

3. 配置文件

server:
  port: 8081
spring:
  application:
    name: spring-cloud-hystrix-dashboard
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true

 

4. 测试

又到了测试时间,我们先把昨天的eureka和producer CV到今天的工作目录下,顺次启动服务,HystrixDashboard最后启动,启动完成后我们访问:http://localhost:8081/hystrix,将会看到如下界面

 

SpringCloud系列教程 | 第五篇:熔断监控Hystrix Dashboard和Turbine
 

 

图中会有一些提示:

Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream

Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]

Single Hystrix App: http://hystrix-app:port/hystrix.stream

大致大概意思就是如果查看默认集群使用第一个url,查看指定集群使用第二个url,单个应用的监控使用最后一个,我们暂时只演示单个应用的所以在输入框中输入: http://localhost:8081/hystrix.stream ,输入之后点击 monitor,进入页面。

如果没有请求会先显示Loading …,访问http://localhost:8081/hystrix.stream 也会不断的显示ping。

我们请求一下昨天用过的链接:http://localhost:8081/hello/spring

马上就能看到统计信息了。

可以再访问一下http://localhost:8081/hystrix.stream,显示如下:

ping: 

data: {"type":...}

data: {"type":...}

 

说明已经返回了监控的各项结果.

到了监控页面就会显示如下图所示:

 

SpringCloud系列教程 | 第五篇:熔断监控Hystrix Dashboard和Turbine
 

 

其实就是http://localhost:8081/hystrix.stream返回结果的图形化显示,Hystrix Dashboard Wiki上详细说明了图上每个指标的含义,如下图:

 

SpringCloud系列教程 | 第五篇:熔断监控Hystrix Dashboard和Turbine
 

 

到此单个应用的熔断监控已经完成。

2. Turbine

在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

1. 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.springcloud</groupId>
    <artifactId>turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>turbine</name>
    <description>Demo project for Spring Boot</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-actuator</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-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. 配置文件

server:
  port: 8888
spring:
  application:
    name: hystrix-dashboard-turbine
turbine:
  app-config: node01,node02
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  combine-host-port: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • turbine.appConfig: 配置Eureka中的serviceId列表,表明监控哪些服务
  • turbine.aggregator.clusterConfig: 指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://…/turbine.stream?cluster={clusterConfig之一}访问
  • turbine.clusterNameExpression: 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

3. 启动类

启动类添加@EnableTurbine,激活对Turbine的支持

package com.springcloud.turbine;

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

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class TurbineApplication {

    public static void main(String[] args) {
        SpringApplication.run(TurbineApplication.class, args);
    }

}

 

4. 创建消费者集群

将昨天的消费者copy2份到今天的文件路径下,修改名称为consumers-node01,consumers-node02

增加依赖包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

 

启动类增加HystrixMetricsStreamServlet注册

package com.springcloud.consumers;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumersApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumersApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

 

注意: 这里注册的HystrixMetricsStreamServlet访问路径是"/actuator/hystrix.stream",这是因为Turbine默认访问的是这个路径。

两个配置文件大家自行修改启动端口号,spring.application.name分别修改为node01和node02

5. 测试

现在依次启动注册中心,两个消费者和Turbine,顺次访问两个消费者,并访问 http://localhost:8888/turbine.stream, 会返回和上面一样的一穿ping的信息。

并且会不断刷新以获取实时的监控数据,说明和单个的监控类似,返回监控项目的信息。

进行图形化监控查看,输入:http://localhost:8888/hystrix,返回酷酷的小熊界面,输入: http://localhost:8888/turbine.stream,然后点击 Monitor Stream ,可以看到出现了俩个监控列表(这是理论情况)

注意,这里本地环境无法测试成功,本地环境只能显示一个服务,Turbine日志中会报错

你的主机中的软件中止了一个已建立的连接。

原因目前不明。

分享到:
评论

相关推荐

    springcloud 熔断监控Hystrix Dashboard和Turbine

    在SpringCloud的生态系统中,Hystrix是用于实现服务容错管理的重要工具,它提供了熔断、降级、隔离和监控等功能,确保服务在高并发或服务故障时能够保持系统的稳定性和可用性。 Hystrix Dashboard是Hystrix提供的一...

    springcloud微服务框架+服务模版

    hystrix-dashboard-turbine:熔断监控Hystrix Dashboard和Turbine的示例 spring-cloud-config-git:配置中心git版本示例 spring-cloud-config-svn-refresh:配置中心svn版本示例,客户端refresh版本示例 spring-...

    spring-cloud-examples

    hystrix-dashboard-turbine:熔断监控Hystrix Dashboard和Turbine的示例 spring-cloud-config-git:配置中心git版本示例 spring-cloud-config-svn-refresh:配置中心svn版本示例,客户端refresh版本示例 spring-...

    SpringCloud10-2 Hystrix整合Dashboard教程

    1. **依赖添加**:在你的`pom.xml`文件中,需要添加`spring-cloud-starter-netflix-hystrix-dashboard`和`spring-cloud-starter-netflix-turbine`依赖。Turbine是用于聚合多个Hystrix流的组件,这样你就可以在一个...

    Hystrix-dashboard+turbine-web+说明文档

    在微服务架构中,Hystrix 和 Turbine 是两个非常重要的工具,它们为分布式系统提供了弹性和监控能力。本文将详细介绍这两个组件以及如何通过 `Hystrix-dashboard` 和 `turbine-web` 来实现有效的监控和聚合监控。 *...

    spring-cloud-netflix-hystrix应用

    其中,Spring Cloud Netflix Hystrix是一款至关重要的组件,它为企业级应用提供了强大的容错保护,实现了服务间的熔断和降级策略,极大地提升了系统的稳定性和可靠性。 Hystrix的核心概念包括: 1. **熔断器模式**...

    SpringCloud10-Hystrix熔断器及服务熔断

    5. **监控与反馈**:Hystrix提供了丰富的监控指标,包括成功率、错误率、调用延迟等,通过Hystrix Dashboard和Turbine等工具,开发者可以实时查看熔断状态和服务健康情况。 6. **命令模式封装**:Hystrix通过命令...

    spring-cloud使用的各种示例

    - [springcloud(五):熔断监控Hystrix Dashboard和Turbine](http://www.ityouknow.com/springcloud/2017/05/18/hystrix-dashboard-turbine.html) - [springcloud(六):配置中心git示例]...

    spring-cloud-examples-master:spring-cloud 学习 demo项目 ,涉及spring-cloud的一些常用知识

     | Spring Cloud使用的各种示例,以最简单、最实用为标准:eureka server单机、双机、集群示例:利用eureka实现服务提供与调用示例:Hystrix熔断的使用示例:熔断监控Hystrix Dashboard和Turbine的示例:...

    springcloud hystrix 断路由

    4. **监控(Monitoring)**: Hystrix Dashboard 和 Turbine 提供实时监控界面,展示服务的健康状况、调用统计等信息,帮助开发者快速定位和解决问题。 5. **短路(Short-circuiting)**: 当请求量超过阈值时,...

    04Hystrix Dashboard:断路器执行监控1

    这些依赖分别用于Eureka服务发现,Hystrix Dashboard本身以及Spring Boot的Actuator模块,Actuator提供了一套健康检查和监控端点,其中包括Hystrix的`hystrix.stream`。 接着,在`application.yml`配置文件中,你...

    Spring Cloud(Hystrix)使用.zip

    7. **监控与告警**:通过Hystrix Dashboard和Turbine,可以实时监控服务的运行状况,包括成功率、响应时间和熔断次数等,及时发现并解决问题。 总之,Spring Cloud Hystrix是构建高可用、健壮微服务的关键工具,它...

    SpringCloud10-Hystrix熔断器学习代码及指定默认的全局兜底方法

    同时,Spring Cloud还提供了Hystrix Dashboard和Turbine来监控和聚合Hystrix命令的运行状况,帮助开发者了解服务的健康状态和性能指标。 在实际项目中,合理地配置和使用Hystrix不仅能提高系统的容错能力,还能优化...

    spring-cloud-examples:Spring Cloud学习案例,服务发现,服务治理,布线追踪,服务监控等

    Spring Cloud使用的各种示例,以最简单,最实用为标准 eureka:eureka服务器单机,双机,替换示例:利用eureka实现服务提供与调用示例 :Hystrix熔断的使用示例 :熔断监控Hystrix仪表板和Turbine的示例 :配置中心...

    基于spring cloud项目源码源码.rar

    3. 研究Hystrix Dashboard和Turbine,了解服务性能监控和熔断效果的可视化展示。 五、进阶主题 1. Spring Cloud Stream:用于构建消息驱动的应用,理解事件驱动架构的设计和实现。 2. Spring Cloud Data Flow:数据...

    springcloud feign整合hystrix(服务降级).doc

    为了监控Hystrix的工作状态,可以引入Hystrix Dashboard和Turbine。Hystrix Dashboard是一个实时的监控界面,Turbine则是用来聚合多个Hystrix流的工具。添加相应的依赖并配置后,可以通过Dashboard来查看每个服务...

    springcloud hystrix的使用

    通常,这包括`spring-cloud-starter-netflix-hystrix`和`spring-cloud-starter-netflix-hystrix-dashboard`,后者用于可视化监控。 2. **配置Hystrix**:在`application.yml`或`application.properties`文件中配置...

    spring cloud示例程序

    9. **断路器监控**:Spring Cloud Hystrix Dashboard 和 Turbine 提供了实时监控Hystrix断路器的状态,帮助开发者理解服务的健康状况。示例将展示如何集成这些监控工具。 10. **分布式追踪**:Spring Cloud Sleuth ...

    spring cloud 教程 pdf

    8. **熔断监控**:Spring Cloud Hystrix Dashboard和Turbine提供了实时监控服务,可以查看每个服务的健康状况和性能指标,帮助我们及时发现并解决问题。 9. **分布式追踪**:Spring Cloud Sleuth与Zipkin或Jaeger...

Global site tag (gtag.js) - Google Analytics