前言
完整知识点:spring cloud netflix 系列技术栈
Hystrix仪表盘
Hystrix在spring cloud netflix体系中是熔断器,我对他的简单理解就是当访问的方法由于各种原因的影响,不能正常的对外提供服务,此时熔断器就是就会马上停止这个请求对该方法的访问,并作出相关的提示,告知用户该方法无法提供正常的服务,防止由于一个方法的阻塞,造成整个系统性能的下降。而熔断器仪表盘就是对这个过程的一个可视化显示。
搭建Ribbon+Hystrix下的hystrix仪表盘
-
完整项目的目录结构
-
hystrix仪表盘的pom文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
- 完整的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">
<modelVersion>4.0.0</modelVersion>
<!-- 关联统一的依赖管理 -->
<parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<!-- 指向统一的依赖管理的位置 -->
<!-- 完整项目(知识点)请查看https://blog.csdn.net/weixin_41756573/article/details/103439742-->
<relativePath>../spring-cloud-netflix-dependencies/pom.xml</relativePath>
</parent>
<groupId>com.baiyang</groupId>
<artifactId>spring-cloud-netflix-web-admin-ribbon</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- 解决 thymeleaf 模板引擎一定要执行严格的 html5 格式校验问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- 配置程序的入口类 -->
<mainClass>com.baiyang.spring.cloud.netflix.web.admin.ribbon.AdminWebRibbonApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
- application入口类
package com.baiyang.spring.cloud.netflix.web.admin.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class AdminWebRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(AdminWebRibbonApplication.class,args);
}
}
- HystrixDashboardConfiguration配置类
package com.baiyang.spring.cloud.netflix.web.admin.ribbon.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class HystrixDashboardConfiguration {
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
- application.yml
spring:
application:
name: spring-cloud-netflix-web-admin-ribbon
server:
port: 8764
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html
访问结果
http://localhost:8764/hystrix
Hystrix仪表盘如何使用
配置完成后,点击Monitor Stream按钮,进入如下界面
新开页面访问8764服务器上的一个方法
http://localhost:8764/hello?message=kino
切换到hystrix仪表盘界面
从界面中的信息(比如颜色对应)我们可以看出,我们进行了一次访问,并失败,错误率为100%。
我们使服务正常运行
再来观察hystrix仪表盘
Hystrix仪表盘总结
其他信息会慢慢在相关博客中更新