视频链接:2020最新版SpringCloud框架开发教程-周阳
文章源码:https://github.com/geyiwei-suzhou/cloud2020/
除了隔离依赖服务的调用外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard)
,Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求,多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面
cloud-consumer-hystrix-dashboard9001模块
1. 建module
New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-consumer-hystrix-dashboard9001] --> Finish
2. 改pom
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.antherd.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
<dependencies>
<!-- hystrix dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. 写yml
server:
port: 9001
4. 主启动
新建类:com.antherd.springcloud.HystrixDashboardMain9001
package com.antherd.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardMain9001.class, args);
}
}
5. 启动测试
启动:cloud-consumer-hystrix-dashboard9001模块
访问:http://localhost:9001/hystrix,可以看到豪猪页面
cloud-provider-hystrix-payment8001模块
所有Provider微服务提供者类(8001/8002/8003)都需要监控依赖配置
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
主启动类:com.antherd.springcloud.PaymentHystrixMain8001添加如下注解
@EnableCircuitBreaker
并添加如下Bean注解
/**
* 此配置是为了服务监控而配置,与服务容错本身无关,spring cloud升级后的坑
* ServletRegistrationBean因为spring boot的默认路径不是"/hystrix.stream"
* 只要在自己的项目里配置上下面的servlet就可以了
* 否则可能报错:Unbale to connnet to Command Metric Stream
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
启动:cloud-eureka-server7001,cloud-provider-hystrix-payment8001,cloud-consumer-hystrix-dashboard9001三个模块
访问:http://localhost:9001/hystrix。在监控地址中填入
http://localhost:8001/hystrix.stream
,Delay填入2000
ms,Title填入T3
,点击Monitor Stream。
然后多次访问:http://localhost:8001/payment/circuit/1、http://localhost:8001/payment/circuit/-1 接口,观察Hystrix Dashboard中左侧圆圈的大小变化、访问曲线及Circuit状态的变化