SpringCloud 断路器监控HystrixDashboard与Turbine Hoxton版本

本文主要是介绍SpringCloud 断路器监控HystrixDashboard与Turbine Hoxton版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring Cloud Hystrix Dashboard简介:Hystrix Dashboard是Spring Cloud中监控Hystrix执行情况的仪表盘组件,支持单实例监控和集群监控。

Spring Cloud Turbine简介:Spring Cloud Turbine是一个Hystrix聚合监控组件,将多个服务的Hystrix Dashboard数据进行聚合监控。

本文主要对Spring Cloud Hystrix Dashboard和Spring Cloud Turbine的基本使用进行简单总结,其中SpringBoot使用的2.2.2.RELEASE版本,SpringCloud使用的Hoxton.SR1版本。这里将沿用SpringCloud 服务注册与发现Eureka Hoxton版本的eureka-server作为注册中心,eureka-client作为服务生产者。

一、Hystrix单实例监控

首先通过使用Hystrix Dashboard监控单个Hystrix实例,通过Maven新建一个名为spring-cloud-netflix-hystrix-dashboard的项目。

1.引入依赖

SpringBoot和SpringCloud依赖这里就不列出来了,还需引入以下依赖:

<!-- Spring Cloud Eureka Client 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Spring Cloud Hystrix DashBoard 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- Actuator 起步依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.主启动类

package com.rtxtitanv;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;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.HystrixDashboardApplication* @description 主启动类* @date 2020/2/27 17:19*/
@EnableHystrixDashboard
@EnableEurekaClient
@SpringBootApplication
public class HystrixDashboardApplication {public static void main(String[] args) {SpringApplication.run(HystrixDashboardApplication.class, args);}
}

@EnableHystrixDashboard:启用Hystrix监控。

3.编写配置文件

application.yml中进行如下配置:

server:port: ${PORT:1200}spring:application:name: hystrix-dashboardeureka:client:# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册register-with-eureka: true# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取fetch-registry: true# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开service-url:defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/instance:# 将ip地址注册到Eureka注册中心prefer-ip-address: true# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ipinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒lease-renewal-interval-in-seconds: 20# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒lease-expiration-duration-in-seconds: 60

4.暴露被监控服务的hystrix.stream端点

新建两个被监控的服务hystrix-monitoredopenfeign-monitored,其中hystrix-monitored是ribbon中使用hystrix,openfeign-monitored是openfeign中使用hystrix,创建过程这里就省略了,想要用于被监控,还需要引入以下依赖:

<!-- Spring Cloud Hystrix 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- Actuator 起步依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然后在application.yml中添加以下配置,暴露hystrix.stream端点:

management:endpoints:web:exposure:# 暴露指定端点,hystrix.stream为hystrix监控端点,'*'暴露所有端点include: 'hystrix.stream'

openfeign-monitored主启动类上也需要添加@EnableHystrix注解。

5.单实例监控

IDEA启动eureka-server集群,eureka-client集群,hystrix-monitoredopenfeign-monitoredhystrix-dashboard,其中eureka-client共3个节点,hystrix-monitored端口为9350,openfeign-monitored端口为9250,访问http://localhost:1200/hystrix,进入Hystrix Dashboard面板页面:
Hystrix Dashboard 首页
在地址框中输入hystrix-monitored实例的监控地址后点击监控按钮:
输入hystrix-monitored实例的监控地址
然后不断访问http://localhost:9350/hystrix,监控信息见下图:
hystrix-monitored实例监控信息
在地址框中输入openfeign-monitored实例的监控地址后点击监控按钮:
输入openfeign-monitored实例的监控地址
然后不断访问http://localhost:9250/openfeign,监控信息见下图:
openfeign-monitored实例监控信息

二、Hystrix默认集群监控

这里使用Turbine来聚合hystrix-monitored服务和openfeign-monitored服务的监控信息,先使用默认的集群监控方式,通过Maven新建一个名为spring-cloud-netflix-turbine的项目。

1.引入依赖

SpringBoot和SpringCloud依赖这里就不列出来了,还需引入以下依赖:

<!-- Spring Cloud Eureka Client 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Spring Cloud Turbine 起步依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<!-- Actuator 起步依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.主启动类

package com.rtxtitanv;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;/*** @author rtxtitanv* @version 1.0.0* @name com.rtxtitanv.TurbineApplication* @description 主启动类* @date 2020/2/27 17:56*/
@EnableTurbine
@EnableEurekaClient
@SpringBootApplication
public class TurbineApplication {public static void main(String[] args) {SpringApplication.run(TurbineApplication.class, args);}
}

@EnableTurbine:启用Turbine聚合监控。

3.编写配置文件

application.yml中进行如下配置:

server:port: ${PORT:1300}spring:application:name: turbineeureka:client:# 服务注册,是否将服务注册到Eureka注册中心,true:注册,false:不注册register-with-eureka: true# 服务发现,是否从Eureka注册中心获取注册信息,true:获取,false:不获取fetch-registry: true# 配置Eureka注册中心即Eureka服务端的地址,集群地址以,隔开service-url:defaultZone: http://rtxtitanv:rtxtitanv@eureka-server-01:8001/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-02:8002/eureka/,http://rtxtitanv:rtxtitanv@eureka-server-03:8003/eureka/instance:# 将ip地址注册到Eureka注册中心prefer-ip-address: true# 该服务实例在注册中心的唯一实例ID,${spring.cloud.client.ip-address}获取该服务实例ipinstance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}# 该服务实例向注册中心发送心跳间隔,单位秒,默认30秒lease-renewal-interval-in-seconds: 20# Eureka注册中心在删除此实例之前收到最后一次心跳后的等待时间,单位秒,默认90秒lease-expiration-duration-in-seconds: 60# 默认集群监控
turbine:# 需要聚合监控的服务名称,多个以,号分隔app-config: hystrix-monitored,openfeign-monitoredaggregator:# 需要聚合监控的集群名称,多个以,隔开,默认defaultcluster-config: default# 指定集群表达式名称cluster-name-expression: new String("default")# 是否以主机名和端口号区分服务,true:是,false:否# 默认情况下会以host来区分不同的服务,在本机调试的时本机上不同的服务会聚合成一个服务来统计combine-host-port: true

4.默认集群监控

IDEA启动eureka-server集群,eureka-client集群,hystrix-monitored集群,openfeign-monitored集群,hystrix-dashboardturbine,其中eureka-clienthystrix-monitoredopenfeign-monitored都是3个节点,hystrix-monitored集群节点端口分别为9351,9352,9353,openfeign-monitored集群节点端口分别为9251,9252,9253,访问http://localhost:1200/hystrix,在地址框中输入默认集群监控地址后点击监控按钮:
输入默认集群监控地址
然后不断访问http://localhost:9351/hystrix,http://localhost:9352/hystrix,http://localhost:9353/hystrix,http://localhost:9251/openfeign,http://localhost:9252/openfeign,http://localhost:9253/openfeign,默认的集群监控信息见下图:
默认的集群监控信息

三、Hystrix指定集群监控

下面使用Turbine进行指定的集群监控。

1.集群分组

hystrix-monitored服务的application.yml中新增以下配置:

eureka:instance:# 自定义元数据metadata-map:# 用于Turbine聚合监控的集群名cluster: HYSTRIX

这里将服务名改为动态传入,便于之后测试,测试时会启动hystrix-monitoredhystrix-monitored2两个服务,修改如下:

spring:application:name: ${APP_NAME:hystrix-monitored}

openfeign-monitored服务的application.yml中新增以下配置:

eureka:instance:# 自定义元数据metadata-map:# 用于Turbine聚合监控的集群名cluster: OPENFEIGN

这里将服务名改为动态传入,便于之后测试,测试时会启动openfeign-monitoredopenfeign-monitored2两个服务,修改如下:

spring:application:name: ${APP_NAME:openfeign-monitored}

2.修改turbine服务配置

turbine服务的application.yml中修改以下配置:

# 指定集群监控
turbine:# 需要聚合监控的服务名称,多个以,号分隔app-config: hystrix-monitored,openfeign-monitored,hystrix-monitored2,openfeign-monitored2aggregator:# 需要聚合监控的集群名称,多个以,隔开,默认defaultcluster-config: HYSTRIX,OPENFEIGN# 指定集群表达式名称cluster-name-expression: metadata['cluster']# 是否以主机名和端口号区分服务,true:是,false:否# 默认情况下会以host来区分不同的服务,在本机调试的时本机上不同的服务会聚合成一个服务来统计combine-host-port: true# 被监控服务都没配置context-path需配置为/actuator/hystrix.stream# 被监控服务都配置相同context-path情况需配置为 此处为配置的context-path/actuator/hystrix.streaminstanceUrlSuffix: /actuator/hystrix.stream

3.指定集群监控

IDEA启动eureka-server集群,eureka-client集群,hystrix-monitored集群,hystrix-monitored2集群,openfeign-monitored集群,openfeign-monitored2集群,hystrix-dashboardturbine,访问注册中心,下图为服务注册信息:
服务注册信息
访问http://localhost:1200/hystrix,在地址框中输入需要Turbine聚合监控的HYSTRIX集群的监控地址后点击监控按钮:
输入HYSTRIX集群的监控地址
然后不断访问http://localhost:9351/hystrix,http://localhost:9352/hystrix,http://localhost:9354/hystrix,http://localhost:9355/hystrix,HYSTRIX集群的监控信息见下图:
HYSTRIX集群的监控信息
访问http://localhost:1200/hystrix,在地址框中输入需要Turbine聚合监控的OPENFEIGN集群的监控地址后点击监控按钮:
输入OPENFEIGN集群的监控地址
然后不断访问http://localhost:9251/openfeign,http://localhost:9252/openfeign,http://localhost:9254/openfeign,http://localhost:9255/openfeign,OPENFEIGN集群的监控信息见下图:
OPENFEIGN集群的监控信息

四、解决服务配置context-path后Turbine聚合监控不到信息问题

1.被监控服务都配置相同context-path

这里使用默认的集群监控方式来测试,在被监控服务的application.yml中都新增以下context-path配置:

server:servlet:# 被监控服务都配置相同context-path情况context-path: /common

IDEA启动eureka-server集群,eureka-client集群,hystrix-monitored集群,openfeign-monitored集群,hystrix-dashboardturbine,访问注册中心,下图为服务注册信息:
服务注册信息
访问http://localhost:1200/hystrix,输入http://localhost:1300/turbine.stream,点击Monitor Stream,再访问两个被监控服务的接口http://localhost:9351/common/hystrix,http://localhost:9252/common/openfeign,9352和9251的链接这里省略,结果监控不到信息,见下图:
监控不到信息问题
访问http://localhost:9351/common/actuator/hystrix.stream,产生了数据,见下图:
产生了数据
为了解决这个问题,需要在turbine服务的application.yml中新增以下配置:

turbine:# 被监控服务都配置相同context-path情况,common为配置的context-path# 被监控服务都没配置context-path需省略该配置instanceUrlSuffix: common/actuator/hystrix.stream

然后重启turbine,重新访问http://localhost:1200/hystrix,输入http://localhost:1300/turbine.stream,点击Monitor Stream,然后访问两个被监控服务接口,已经监控到了信息,监控信息见下图:
在这里插入图片描述

2.被监控服务配置了不同context-path

如果被监控服务配置了不同context-path,就需要根据context-path进行分组,将配置有相同context-path的服务分为一组。这里使用指定的集群监控方式来测试,配置见上文指定集群监控。
hystrix-monitoredhystrix-monitored2服务的application.yml中新增以下context-path配置:

server:servlet:# 被监控服务配置了不同context-path情况context-path: /hystrix

openfeign-monitoredopenfeign-monitored2服务的application.yml中新增以下context-path配置:

server:servlet:# 被监控服务配置了不同context-path情况context-path: /openfeign

然后在turbine服务的application.yml中修改以下配置:

turbine:# 被监控服务配置了不同context-path或被监控服务部分配置了context-path# 根据context-path进行分组,将配置有相同context-path的服务分为一组instanceUrlSuffix:# 相同context-path服务分为的集群对应的instanceUrlSuffix# 如果有部分服务没有配置context-path,对该部分服务分组后无需配置对应的instanceUrlSuffix,为默认值/actuator/hystrix.streamHYSTRIX: hystrix/actuator/hystrix.streamOPENFEIGN: openfeign/actuator/hystrix.stream

IDEA启动eureka-server集群,eureka-client集群,hystrix-monitored集群,hystrix-monitored2集群,openfeign-monitored集群,openfeign-monitored2集群,hystrix-dashboardturbine,访问注册中心,下图为服务注册信息:
服务注册信息
访问http://localhost:1200/hystrix,输入http://localhost:1300/turbine.stream?cluster=HYSTRIX,点击Monitor Stream,然后不断访问http://localhost:9351/hystrix/hystrix,http://localhost:9352/hystrix/hystrix,http://localhost:9354/hystrix/hystrix,http://localhost:9355/hystrix/hystrix,HYSTRIX集群的监控信息见下图:
HYSTRIX集群的监控信息
访问http://localhost:1200/hystrix,输入http://localhost:1300/turbine.stream?cluster=OPENFEIGN,点击Monitor Stream,然后不断访问http://localhost:9251/openfeign/openfeign,http://localhost:9252/openfeign/openfeign,http://localhost:9254/openfeign/openfeign,http://localhost:9255/openfeign/openfeign,OPENFEIGN集群的监控信息见下图:
OPENFEIGN集群的监控信息

3.被监控服务部分配置了context-path

如果被监控服务只有部分配置了context-path,有一部分没配置context-path,可以将没配置context-path的服务分成一组或多组,不过这部分服务分组后无需配置对应的instanceUrlSuffix。下面进行测试,将openfeign-monitoredopenfeign-monitored2服务的application.yml中的context-path配置暂时注释掉,然后在turbine服务的application.yml中修改以下配置:

turbine:# 被监控服务配置了不同context-path或被监控服务部分配置了context-path# 根据context-path进行分组,将配置有相同context-path的服务分为一组instanceUrlSuffix:# 相同context-path服务分为的集群对应的instanceUrlSuffix# 如果有部分服务没有配置context-path,对该部分服务分组后无需配置对应的instanceUrlSuffix,为默认值/actuator/hystrix.streamHYSTRIX: hystrix/actuator/hystrix.stream

IDEA启动eureka-server集群,eureka-client集群,hystrix-monitored集群,hystrix-monitored2集群,openfeign-monitored集群,openfeign-monitored2集群,hystrix-dashboardturbine,访问注册中心,下图为服务注册信息:
服务注册信息
访问http://localhost:1200/hystrix,输入http://localhost:1300/turbine.stream?cluster=HYSTRIX,点击Monitor Stream,然后不断访问http://localhost:9351/hystrix/hystrix,http://localhost:9352/hystrix/hystrix,http://localhost:9354/hystrix/hystrix,http://localhost:9355/hystrix/hystrix,HYSTRIX集群的监控信息见下图:
HYSTRIX集群的监控信息
访问http://localhost:1200/hystrix,输入http://localhost:1300/turbine.stream?cluster=OPENFEIGN,点击Monitor Stream,然后不断访问http://localhost:9251/openfeign,http://localhost:9252/openfeign,http://localhost:9254/openfeign,http://localhost:9255/openfeign,OPENFEIGN集群的监控信息见下图:
OPENFEIGN集群的监控信息

代码示例

  • Github:
    https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-hystrix-dashboard
    https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-turbine
    https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-hystrix-monitored
    https://github.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-openfeign-monitored
  • Gitee:
    https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-hystrix-dashboard
    https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-turbine
    https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-netflix-hystrix-monitored
    https://gitee.com/RtxTitanV/springcloud-learning/tree/master/springcloud-hoxton-learning/spring-cloud-openfeign-monitored

这篇关于SpringCloud 断路器监控HystrixDashboard与Turbine Hoxton版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/229632

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Linux使用nload监控网络流量的方法

《Linux使用nload监控网络流量的方法》Linux中的nload命令是一个用于实时监控网络流量的工具,它提供了传入和传出流量的可视化表示,帮助用户一目了然地了解网络活动,本文给大家介绍了Linu... 目录简介安装示例用法基础用法指定网络接口限制显示特定流量类型指定刷新率设置流量速率的显示单位监控多个

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.