服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

2021-08-21 16:0935s Java教程

这篇文章主要介绍了SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

服务监控

  • 除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求,多少成功,多少失败等等。
  • Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控,SpringCloud也提供了HystrixDashboard的整合,对监控内容转化成可视化界面!

监控服务测试

1. 服务监控是针对客户端(消费者)的,所以客户端需要做出一些配置

2. 普通消费者只需要添加hystrix和dashboard的依赖+@EnableHystrixDashboard就可以把消费者变成一个监控中心,同时也失去了消费者的功能,不能再访问注册中心

一、客户端(消费者)

1. 新建消费者服务9001(复制),新增监控依赖

  1. <!--Hystrix-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-hystrix</artifactId>
  5. <version>1.4.7.RELEASE</version>
  6. </dependency>
  7.  
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
  11. <version>1.4.7.RELEASE</version>
  12. </dependency>

2. 修改配置文件

  1. server:
  2. port: 9001
  3. hystrix:
  4. dashboard:
  5. proxy-stream-allow-list: "*"

3. 为启动类添加支持监控的注解

SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

  1. //Eureka和Ribbon整合以后,客户端可以根据服务名称直接调用,不用关心IP地址和端口号
  2. @SpringBootApplication
  3. @EnableHystrixDashboard
  4. //@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = MyLoaderBalanceConfig.class) //在微服务启动的时候加载自定义的Ribbon
  5. public class DeptConsumer_hystrix_dashboard_9001 {
  6. public static void main(String[] args) {
  7. SpringApplication.run(DeptConsumer_hystrix_dashboard_9001.class,args);
  8. }
  9. }

二、服务端(生产者)

1. 所以的服务提供者都要添加被监控的依赖和Hystrix的依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  5.  
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-hystrix</artifactId>
  9. <version>1.4.7.RELEASE</version>
  10. </dependency>

2. 为被监控的服务提供者的启动类添加一个Bean

SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

  1. @Bean
  2. public ServletRegistrationBean hystrixMetricsStreamServlet() {
  3. ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
  4. registration.addUrlMappings("/actuator/hystrix.stream");
  5. return registration;
  6. }

三、查看

  1. 启动Eureka集群-7001、7002
  2. 启动服务提供者-8001,并查看Eureka集群,服务是否注册成功
  3. 启动服务消费者-9001
  4. 尝试直接访问服务提供者,不通过消费者和注册中心,http://localhost:8001/hystrix/dept/get/2
  5. 打开服务提供者的 http://localhost:8001/actuator/hystrix.stream,查看是否在ping
  6. 打开消费者 http://localhost:9001/hystrix

SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法
SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

疑问:9001作为一个消费者模块,为什么不能访问生产者,难道这个模块只是用来监控的平台?



tips:

 

SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法
SpringCloud-Hystrix-Dashboard客户端服务监控的实现方法

到此这篇关于SpringCloud-Hystrix-Dashboard客户端服务监控的文章就介绍到这了,更多相关SpringCloud-Hystrix-Dashboard服务监控内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_40429067/article/details/114440653

延伸 · 阅读

精彩推荐