[JAVA] SpringCloud HystrixDashboard服务监控详解

1720 0
Honkers 2022-11-6 11:25:31 | 显示全部楼层 |阅读模式
目录

    hystrixDashboard服务监控断路器演示监控
      监控8001注意事项监控测试监控状态



hystrixDashboard服务监控

除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。
1、新建cloud-consumer-hystrix-dashboard9001仪表盘监控模块
2、修改pom.xml文件引入仪表盘依赖
核心依赖:spring-cloud-starter-netflix-hystrix-dashboard
注意:所有的图形化展示,都需要引入spring-boot-starter-actuator依赖,在8001、8002上都需要引入
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <parent>
  6.         <artifactId>springcloud2022</artifactId>
  7.         <groupId>com.zcl.springcloud</groupId>
  8.         <version>1.0-SNAPSHOT</version>
  9.     </parent>
  10.     <modelVersion>4.0.0</modelVersion>
  11.     <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
  12.     <properties>
  13.         <maven.compiler.source>8</maven.compiler.source>
  14.         <maven.compiler.target>8</maven.compiler.target>
  15.     </properties>
  16.     <dependencies>
  17.         <dependency>
  18.             <groupId>org.springframework.cloud</groupId>
  19.             <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  20.         </dependency>
  21.         <dependency>
  22.             <groupId>org.springframework.boot</groupId>
  23.             <artifactId>spring-boot-starter-actuator</artifactId>
  24.         </dependency>
  25.         <dependency>
  26.             <groupId>org.springframework.boot</groupId>
  27.             <artifactId>spring-boot-devtools</artifactId>
  28.             <scope>runtime</scope>
  29.             <optional>true</optional>
  30.         </dependency>
  31.         <dependency>
  32.             <groupId>org.projectlombok</groupId>
  33.             <artifactId>lombok</artifactId>
  34.             <optional>true</optional>
  35.         </dependency>
  36.         <dependency>
  37.             <groupId>org.springframework.boot</groupId>
  38.             <artifactId>spring-boot-starter-test</artifactId>
  39.             <scope>test</scope>
  40.         </dependency>
  41.     </dependencies>
  42. </project>
复制代码
3、添加YAML配置文件
server:
  port: 9001
4、建立启动类
必须要加上@EnableHystrixDashboard注解激活
  1. package com.zcl.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  5. /**
  6. * 描述:仪表盘启动类
  7. */
  8. @SpringBootApplication
  9. @EnableHystrixDashboard
  10. public class HystrixDashboardMain9001 {
  11.     public static void main(String[] args) {
  12.         SpringApplication.run(HystrixDashboardMain9001.class, args);
  13.     }
  14. }
复制代码
5、启动项目
启动项目测试:http://localhost:901/hystrix
使用方法:在下面页面中输入需要进行监控的地址即可



断路器演示监控


监控8001注意事项

1、必须要有如下的两个依赖
  1. <!--web-->
  2. <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.springframework.boot</groupId>
  8.     <artifactId>spring-boot-starter-actuator</artifactId>
  9. </dependency>
复制代码
2、对启动类的修改
注意:新版本Hystrix需要在主启动类MainAppHystrix8001中指定监控路径,否则会出现报错
  1. package com.zcl.springcloud;
  2. import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  6. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  7. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  8. import org.springframework.context.annotation.Bean;
  9. /**
  10. * 描述:熔断限流启动类
  11. */
  12. @SpringBootApplication
  13. @EnableEurekaClient
  14. @EnableCircuitBreaker
  15. public class PaymentHystrixMain8001 {
  16.     public static void main(String[] args) {
  17.         SpringApplication.run(PaymentHystrixMain8001.class, args);
  18.     }
  19.     /**
  20.      *此配置是为了服务监控而配置,与服务容错本身无关,springcloud升级后的坑
  21.      *ServletRegistrationBean因为springboot的默认路径不是"/hystrix.stream",
  22.      *只要在自己的项目里配置上下面的servlet就可以了
  23.      */
  24.     @Bean
  25.     public ServletRegistrationBean getServlet() {
  26.         HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
  27.         ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
  28.         registrationBean.setLoadOnStartup(1);
  29.         registrationBean.addUrlMappings("/hystrix.stream");
  30.         registrationBean.setName("HystrixMetricsStreamServlet");
  31.         return registrationBean;
  32.     }
  33. }
复制代码
监控测试

启动7001Eureka服务中心
观察监控窗口
9001监控8001


8001地址测试
先访问正确地址,再访问错误地址,再正确地址,会发现图示断路器都是慢慢放开的。
http://localhost:8001/payment/circuit/31:正常的访问


http://localhost:8001/payment/circuit/-31:异常访问



监控状态



到此这篇关于SpringCloud HystrixDashboard服务监控详解的文章就介绍到这了,更多相关SpringCloud HystrixDashboard内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行