通过前面 5 篇备忘记录,我已经基本搭建好了一套能够跑起来的 Spring Cloud 系统,在第 4 篇备完 《SpringCloud 备忘 4 - Feign 风格消费者 Consumer Feign 服务搭建》使用 Feign 搭建消费者服务时集成了断路器 Hystrix,因此继续记录一下 Hystrix 的搭建备忘。
1、在父工程 “microservicecloud” 下新建一个 Module,名称为 “hystrix-dashboard-8601”
2、修改 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>microservicecloud</artifactId>
<groupId>com.lakey.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hystrix-dashboard-8601</artifactId>
<name>hystrix-dashboard-8601</name>
<description>Hystrix Dashboard 8601</description>
<dependencies>
<!-- Hystrix 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- Hystrix DashBoard 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
3、在 java 路径下创建目录 “com.lakey.springcloud” 并添加启动类 HystrixDashboard8601Application.java
package com.lakey.springcloud;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard // 开启 Hystrix 监控服务
public class HystrixDashboard8601Application {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(HystrixDashboard8601Application.class);
application.setBannerMode(Banner.Mode.OFF);// 不输出Banner
application.run(args);
System.out.println(" ____ _ ____ _ _ ____ ___ _ ____ ____ ___ __ ___ _ \n" +
" | _ \\ / \\ / ___| | | | | | __ ) / _ \\ / \\ | _ \\ | _ \\ ( _ ) / /_ / _ \\ / |\n" +
" | | | | / _ \\ \\___ \\ | |_| | | _ \\ | | | | / _ \\ | |_) | | | | | / _ \\ | '_ \\ | | | | | |\n" +
" | |_| | / ___ \\ ___) | | _ | | |_) | | |_| | / ___ \\ | _ < | |_| | | (_) | | (_) | | |_| | | |\n" +
" |____/ /_/ \\_\\ |____/ |_| |_| |____/ \\___/ /_/ \\_\\ |_| \\_\\ |____/ \\___/ \\___/ \\___/ |_|");
}
}
4、在 resources 路径下添加配置文件 application.yml
# 服务器配置
server:
port: 8601
5、运行项目,成功结果如截图所示
6、修改 Feign 消费者模块 “consumer-feign-8301”
6.1、pom.xml 文件添加 Actuator 依赖,获取项目信息
<!-- Actuator 获取监控信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
6.2、启动类头部添加注解 @EnableHystrix 开启断路器服务
6.3、当前使用的 SpringBoot 是 2.x 版本,集成的 Hystrix不再能够自动映射“/hyxstrix.stream”,需要添加链接 “/hystrix.stream”的映射 Servlet

/**
* 配置 Hystrix Dashboard 监控地址 /hystrix.stream 的 Servlet
*
* @return
*/
@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;
}
7、依次启动服务:Eureka-8101、Provider-8201、Provider-8202、Consumer-Feign-8301
7.1、先通过链接 http://localhost:8301/hello?name=liming 确认服务是否正常启动,成功结果如截图所示
7.2、通过链接 http://localhost:8301/hystrix.stream 确认 Hystrix 是否被监控,此处如果出现提示 ping 空(如下面截图图所示)的情况也不是说明集成失败,只能说明当时没有监控的请求,刷新几次 7.1 中的链接再回来一般就会出现正常的 ping 信息
8、回到第 5 步中的界面(链接:http://localhost:8601/hystrix),填入监控地址(7.2 中的链接:)、延时间隔、监控标题,点击按钮 Monitor Stream 即可浏览监控结果
本篇备忘图片较多,增长了文章篇幅,为方便阅读,监控界面的说明被放到了下一篇备忘。
9、参考资料:
https://www.cnblogs.com/wangdaijun/p/8891220.html
尚硅谷 Spring Cloud 视频教程
12、码云源码: