Hystrix提供了Hystrix Dashboard来实时监控HystrixCommand方法的执行情况。 Hystrix Dashboard可以有效地反映出每个Hystrix实例的运行情况,帮助我们快速发现系统中的问题,从而采取对应措施。

需要创建dashboard监控的服务消费者

image.png

添加pom jar依赖

  1. <?xml version="1.0"?>
  2. <project
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
  4. xmlns="http://maven.apache.org/POM/4.0.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  6. <modelVersion>4.0.0</modelVersion>
  7. <parent>
  8. <groupId>com.junjay</groupId>
  9. <artifactId>SpringCloud</artifactId>
  10. <version>0.0.1-SNAPSHOT</version>
  11. </parent>
  12. <groupId>com.junjay</groupId>
  13. <artifactId>springcloud-consumer-hystrix-dashboard</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>springcloud-consumer-hystrix-dashboard</name>
  16. <url>http://maven.apache.org</url>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>junit</groupId>
  23. <artifactId>junit</artifactId>
  24. <scope>test</scope>
  25. </dependency>
  26. <!-- 消费者只需要实体类+web -->
  27. <dependency>
  28. <groupId>com.junjay</groupId>
  29. <artifactId>springcloud-pai</artifactId>
  30. <version>0.0.1-SNAPSHOT</version>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-web</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.mybatis.spring.boot</groupId>
  38. <artifactId>mybatis-spring-boot-starter</artifactId>
  39. </dependency>
  40. <!-- 热部署工具 -->
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-devtools</artifactId>
  44. </dependency>
  45. <!-- 添加负载均衡Ribbon依赖 -->
  46. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-ribbon -->
  47. <dependency>
  48. <groupId>org.springframework.cloud</groupId>
  49. <artifactId>spring-cloud-starter-ribbon</artifactId>
  50. <version>1.4.6.RELEASE</version>
  51. </dependency>
  52. <!-- 加入eureka依赖进行服务注册 -->
  53. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-starter-eureka</artifactId>
  57. <version>1.4.6.RELEASE</version>
  58. </dependency>
  59. <!-- 增加hystrix依赖 -->
  60. <dependency>
  61. <groupId>org.springframework.cloud</groupId>
  62. <artifactId>spring-cloud-starter-hystrix</artifactId>
  63. <version>1.4.6.RELEASE</version>
  64. </dependency>
  65. <!-- 增加dashboard依赖 -->
  66. <dependency>
  67. <groupId>org.springframework.cloud</groupId>
  68. <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
  69. <version>1.4.6.RELEASE</version>
  70. </dependency>
  71. </dependencies>
  72. </project>

配置yml文件

  1. #服务消费者只需要配置启动端口
  2. server:
  3. port: 9001

编辑主启动类

  1. package org.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  6. import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
  7. @SpringBootApplication
  8. @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
  9. // 开启dashboard
  10. @EnableHystrixDashboard
  11. public class DeptConsumer_9001 {
  12. public static void main(String[] args) {
  13. SpringApplication.run(DeptConsumer_9001.class, args);
  14. }
  15. }

保证服务端都有监控actuator

  1. <!-- actuator 完善监控信息 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-actuator</artifactId>
  5. </dependency>

启动服务9001

image.png

证明可以访问,启动成功,完整访问(豪猪)

豪猪(学名:Hystrix brachyura hodgsoni

http://localhost:9001/hystrix
image.png

配置监控

在服务端启动类添加监控url
springcloud-provider-dept-8081服务添加

  1. package org.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. import org.springframework.context.annotation.Bean;
  8. import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
  9. // 启动类
  10. @SpringBootApplication
  11. // 开启eureka 在服务启动后自动将服务注册到eureka中
  12. @EnableEurekaClient
  13. // 服务发现及DeptController中的discovery方法
  14. @EnableDiscoveryClient
  15. public class DeptProvider_8081 {
  16. public static void main(String[] args) {
  17. SpringApplication.run(DeptProvider_8081.class, args);
  18. }
  19. // 增加一个servlet
  20. @Bean
  21. public ServletRegistrationBean hystrixMetricsStreamServlet() {
  22. ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
  23. // Single Hystrix应用程序: http://hystrix-app:port/actuator/hystrix.stream
  24. registrationBean.addUrlMappings("/actuator/hystrix.stream");
  25. return registrationBean;
  26. }
  27. }

访问localhost:8081/actuator/hystrix.stream

image.png

在dashboard中监控localhost:8081/actuator/hystrix.stream

image.png

Dashboard监控一直是Loading ping:一直都是空的

https://blog.csdn.net/qq_38788216/article/details/103929756
错误原因是一直链接不到eureka
image.png
image.png
需要加在有熔断机制服务中,
image.png