SpringCloud中的熔断监控HystrixDashboard和Turbine示例详解

 更新时间:2024年09月09日 12:25:36   作者:242030  
HystrixDashboard是用于实时监控Hystrix性能的工具,展示请求响应时间和成功率等数据,本文介绍了如何配置和使用HystrixDashboard和Turbine进行熔断监控,包括依赖添加、启动类配置和测试流程,感兴趣的朋友一起看看吧

SpringCloud之熔断监控HystrixDashboard和Turbine

Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各

Hystrix Command的请求响应时间, 请求成功率等数据。但是只使用Hystrix Dashboard的话, 你只能看到单个应

用内的服务信息,这明显不够。我们需要一个工具能让我们汇总系统内多个服务的数据并显示到Hystrix

Dashboard上,这个工具就是Turbine。

1、Hystrix Dashboard

我们在熔断示例项目的基础上更改。

1.1 添加依赖

<?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 https://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>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-consumer-hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-consumer-hystrix-dashboard</name>
    <description>spring-cloud-consumer-hystrix-dashboard</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</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>

1.2 启动类

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

package com.example;
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.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SpringCloudConsumerHystrixDashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConsumerHystrixDashboardApplication.class, args);
    }
}

1.3 其它类

package com.example.remote;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name= "spring-cloud-producer", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}
package com.example.remote;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
@Component
public class HelloRemoteHystrix implements HelloRemote{
    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " +name+", this messge send failed ";
    }
}
package com.example.controller;
import com.example.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
    @Autowired
    HelloRemote helloRemote;
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name);
    }
}
spring.application.name=spring-cloud-consumer-hystrix-dashboard
server.port=9003
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

1.4 测试

启动工程后访问 http://localhost:9003/hystrix,将会看到如下界面:

图中会有一些提示:

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:9003/hystrix.stream ,输入之后点击

monitor,进入页面。如果没有请求会先显示Loading ...

在这里插入图片描述

访问 http://localhost:9003/hystrix.stream 也会不断的显示ping。

在这里插入图片描述

请求服务 http://localhost:9003/hello/neo,就可以看到监控的效果了:

在这里插入图片描述

首先访问 http://localhost:9003/hystrix.stream,显示如下:

data: {"type":"HystrixCommand","name":"HelloRemote#hello(String)","group":"spring-cloud-producer","currentTime":1662554090588,"isCircuitBreakerOpen":false,"errorPercentage":0,"errorCount":0,"requestCount":0,"rollingCountBadRequests":0,"rollingCountCollapsedRequests":0,"rollingCountEmit":0,"rollingCountExceptionsThrown":0,"rollingCountFailure":0,"rollingCountFallbackEmit":0,"rollingCountFallbackFailure":0,"rollingCountFallbackMissing":0,"rollingCountFallbackRejection":0,"rollingCountFallbackSuccess":0,"rollingCountResponsesFromCache":0,"rollingCountSemaphoreRejected":0,"rollingCountShortCircuited":0,"rollingCountSuccess":0,"rollingCountThreadPoolRejected":0,"rollingCountTimeout":0,"currentConcurrentExecutionCount":0,"rollingMaxConcurrentExecutionCount":0,"latencyExecute_mean":0,"latencyExecute":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"latencyTotal_mean":0,"latencyTotal":{"0":0,"25":0,"50":0,"75":0,"90":0,"95":0,"99":0,"99.5":0,"100":0},"propertyValue_circuitBreakerRequestVolumeThreshold":20,"propertyValue_circuitBreakerSleepWindowInMilliseconds":5000,"propertyValue_circuitBreakerErrorThresholdPercentage":50,"propertyValue_circuitBreakerForceOpen":false,"propertyValue_circuitBreakerForceClosed":false,"propertyValue_circuitBreakerEnabled":true,"propertyValue_executionIsolationStrategy":"THREAD","propertyValue_executionIsolationThreadTimeoutInMilliseconds":1000,"propertyValue_executionTimeoutInMilliseconds":1000,"propertyValue_executionIsolationThreadInterruptOnTimeout":true,"propertyValue_executionIsolationThreadPoolKeyOverride":null,"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests":10,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"propertyValue_requestCacheEnabled":true,"propertyValue_requestLogEnabled":true,"reportingHosts":1,"threadPool":"spring-cloud-producer"}
data: {"type":"HystrixThreadPool","name":"spring-cloud-producer","currentTime":1662554090588,"currentActiveCount":0,"currentCompletedTaskCount":1,"currentCorePoolSize":10,"currentLargestPoolSize":1,"currentMaximumPoolSize":10,"currentPoolSize":1,"currentQueueSize":0,"currentTaskCount":1,"rollingCountThreadsExecuted":0,"rollingMaxActiveThreads":0,"rollingCountCommandRejections":0,"propertyValue_queueSizeRejectionThreshold":5,"propertyValue_metricsRollingStatisticalWindowInMilliseconds":10000,"reportingHosts":1}
ping: 

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

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

在这里插入图片描述

其实就是 http://localhost:9003/hystrix.stream 返回结果的图形化显示,Hystrix Dashboard Wiki上详细

说明了图上每个指标的含义,如下图:

在这里插入图片描述

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

2、Turbine

在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务

的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开

源项目(Turbine)来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。

2.1 添加依赖

<?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 https://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>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-cloud-hystrix-dashboard-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-hystrix-dashboard-turbine</name>
    <description>spring-cloud-hystrix-dashboard-turbine</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-turbine</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-hystrix-dashboard</artifactId>
        </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.2 配置文件

spring.application.name=hystrix-dashboard-turbine
server.port=8001
turbine.appConfig=node01,node02
turbine.aggregator.clusterConfig= default
turbine.clusterNameExpression= new String("default")
eureka.client.serviceUrl.defaultZone=http://localhost:8000/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

2.3 启动类

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

package com.example;
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 SpringCloudHystrixDashboardTurbineApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringCloudHystrixDashboardTurbineApplication.class, args);
	}
}

到此Turbine(hystrix-dashboard-turbine)配置完成

2.4 测试

在示例项目spring-cloud-consumer-hystrix基础上修改为两个服务的调用者spring-cloud-consumer-node1和

spring-cloud-consumer-node2。

spring-cloud-consumer-node1项目改动如下:application.properties文件内容

spring.application.name=node01
server.port=9001
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

spring-cloud-consumer-node2项目改动如下:application.properties文件内容

spring.application.name=node02
server.port=9002
feign.hystrix.enabled=true
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

HelloRemote类修改:

package com.example.remote;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "spring-cloud-producer", fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

对应的HelloRemoteHystrixConsumerController类跟随修改,具体查看代码

package com.example.remote;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
@Component
public class HelloRemoteHystrix implements HelloRemote{
    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " +name+", this messge send failed ";
    }
}
package com.example.controller;
import com.example.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
    @Autowired
    HelloRemote helloRemote;
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return helloRemote.hello(name);
    }
}

修改完毕后,依次启动spring-cloud-eurekaspring-cloud-consumer-node1spring-cloud-consumer-

node2spring-cloud-hystrix-dashboard-turbine

打开eureka后台可以看到注册了三个服务:

在这里插入图片描述

访问 http://localhost:8001/turbine.stream

返回:

: ping
data: {"reportingHostsLast10Seconds":0,"name":"meta","type":"meta","timestamp":1662554703162}
: ping
data: {"reportingHostsLast10Seconds":0,"name":"meta","type":"meta","timestamp":1662554706172}

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

进行图形化监控查看,输入:http://localhost:8001/hystrix,返回酷酷的小熊界面

在这里插入图片描述

输入:http://localhost:8001/turbine.stream,然后点击 Monitor Stream ,可以看到出现了俩个监控列表

在这里插入图片描述

访问http://localhost:9001/hello/neohttp://localhost:9002/hello/neo

在这里插入图片描述

到此这篇关于SpringCloud中的熔断监控HystrixDashboard和Turbine的文章就介绍到这了,更多相关SpringCloud熔断监控内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 通过MyBatis读取数据库数据并提供rest接口访问

    通过MyBatis读取数据库数据并提供rest接口访问

    这篇文章主要介绍了通过MyBatis读取数据库数据并提供rest接口访问 的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08
  • springboot结合mybatis-plus快速生成项目模板的方法

    springboot结合mybatis-plus快速生成项目模板的方法

    Mybatis-Plus是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生,接下来通过本文给大家分享springboot结合mybatis-plus快速生成项目模板的方法,感兴趣的朋友一起看看吧
    2021-06-06
  • Java编程中利用InetAddress类确定特殊IP地址的方法

    Java编程中利用InetAddress类确定特殊IP地址的方法

    这篇文章主要介绍了Java编程中利用InetAddress类确定特殊IP地址的方法,InetAddress类是Java网络编程中一个相当实用的类,需要的朋友可以参考下
    2015-11-11
  • 使用Java填充Word模板的方法详解

    使用Java填充Word模板的方法详解

    Java填充Word模板是一种将动态数据插入到Word文档模板中生成最终文档的过程,通常用于批量创建包含个人信息、报告结果或其他动态内容的文档,本文给大家介绍了使用Java填充Word模板的方法,需要的朋友可以参考下
    2024-07-07
  • 详解springmvc 中controller与jsp传值

    详解springmvc 中controller与jsp传值

    本篇文章主要介绍了springmvc 中controller与jsp传值,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • SpringBoot实战之SSL配置详解

    SpringBoot实战之SSL配置详解

    今天小编就为大家分享一篇关于SpringBoot实战之SSL配置详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • Springcloud ribbon负载均衡算法实现

    Springcloud ribbon负载均衡算法实现

    这篇文章主要介绍了Springcloud ribbon负载均衡算法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Sentinel结合Nacos实现数据持久化过程详解

    Sentinel结合Nacos实现数据持久化过程详解

    这篇文章主要介绍了Sentinel结合Nacos实现数据持久化过程,要持久化的原因是因为每次启动Sentinel都会使之前配置的规则就清空了,这样每次都要再去设定规则显得非常的麻烦,感兴趣想要详细了解可以参考下文
    2023-05-05
  • 一篇文章彻底拆解Java HashMap扩容机制

    一篇文章彻底拆解Java HashMap扩容机制

    在Java中HashMap是一个非常常用的数据结构,基于哈希表实现,它通过键值对的形式存储数据,这篇文章主要介绍了Java HashMap扩容机制的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2026-04-04
  • java中int、double、char等变量的取值范围详析

    java中int、double、char等变量的取值范围详析

    这篇文章主要给大家介绍了关于java中int、double、char等变量取值范围的相关资料,每个变量都给出了详细的实例代码,对大家学习或者使用java具有一定的参考学习价值,需要的朋友可以参考下
    2021-10-10

最新评论