新建模块:cloud-consumer-hystrix-dashboard9001
打开Hystrix仪表盘,需要引入相关的jar包
spring-cloud-starter-netflix-hystrix-dashboard
pom.xml
<?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>cloud201230</artifactId>
<groupId>com.banana</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>
<dependencies>
<!-- 引入仪表盘 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- 引入自定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.banana</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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>
</dependencies>
</project>
application.yml
server:
port: 9001
HystrixDashboardMain9001 : 在主启动类上加上注解 ·@EnableHystrixDashboard
package com.banana.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
/**
* 豪猪哥 仪表盘
* @author layman
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardMain9001.class,args);
}
}
主启动类启动后,如果一切顺利,再浏览器的地址栏输入:http://localhost:9001/hystrix
应当可以看到以下画面:
接下来,我们通过一些配置,让9001模块 去监控8001 模块
准备工作(全是在8001模块操作)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
/**
* 此配置是为了服务监控而配置,与服务器容错本身无关,springCloud升级后的坑
* ServletRegistrationBean因为springBoot的默认路径不是/hystrix.stream
* 只要在自己的项目里配置上下文的servlet就可以了
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
先启动7001 Eureka,在启动8001服务端
正常启动后,可以在7001Eureka看到8001服务端已经注册成功
在9001 Hystrix Dashboad中输入需要监控的地址
http://localhost:8001/hystrix.stream
点击Monitor Stream后,界面应当是空空如也。
数据分为两种情况,一种是成功访问,不触发熔断,一种是错误访问,触发熔断
http://localhost:8001/payment//hystrix/circuit/3
狂点数次,观看仪表盘情况:
七色一圈一线
http://localhost:8001/payment//hystrix/circuit/-3
狂点数次,观看仪表盘情况:
可以看到,在单位时间内的错误率达到阈值,立刻触发熔断,后续的访问也无法通过。
当服务处于熔断状态时,即便是正确的访问,也会被拒绝。如下图所示:
Hystrix需要我们自己搭建监控平台,最重要的一点是,它现在已经停止更新了
如果是在公司的项目中,加上服务降级与熔断,不建议使用Hystrix
而是采用Alibaba的sentinel(重点!)
cs