spring boot 2.x spring cloud Greenwich.SR1 hystrix Dashboard监控搭建使用
本次例子是基于上一次构建的
badger-spring-cloud-openfeign-hystrix
项目的基础上,使用的;
《springboot 2.x–spring cloud Greenwich.SR1 服务注册与发现–eureka搭建以及集群搭建》
《docker 、docker-compose环境部署eureka集群》
《spring boot 2.x spring cloud Greenwich.SR1 负载均衡ribbon搭建使用》
《spring boot 2.x spring cloud Greenwich.SR1 负载均衡ribbon自动装配,负载均衡部分源码解析》
《spring boot 2.x spring cloud Greenwich.SR1 openfeign自动装配部分源码解析,以及@EnableFeignClients注解详解》
《spring boot 2.x spring cloud Greenwich.SR1 断路器hystrix搭建使用》
具体代码信息,可以查看《码云》
1、项目搭建
1.1、新建项目badger-spring-cloud-hystrix-dashboard
,pom文件如下, 引入hystrix和 hystrix-dashboard相关
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.badger</groupId>
<artifactId>badger-spring-cloud-hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>bbadger-spring-cloud-hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<!-- hystrix和 hystrix-dashboard相关 -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2、新建主启动类HystrixDashboardApplication
,并且加上@EnableHystrixDashboard
注解
/**
* @EnableHystrixDashboard 开启断路器仪表盘 hystrix dashboard
http://localhost:8764/hystrix
* @author liqi
*
*/
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
1.3、ymal配置文件,就只是配置了启动端口8764
server:
port: 8764
1.4、启动项目
http://localhost:8764/hystrix
看到这个豪猪的页面,项目就搭建完成了,下面说明下页面上要表达的意思
Hystrix Dashboard共支持三种不同的监控方式
- default cluster(查看默认的集群):http://turbine-hostname:port/turbine.stream
- custom cluster(查看指定的集群节点):http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
- Single Hystrix App(单个应用):http://hystrix-app:port/actuator/hystrix.stream
三个输入框
第一个:输入需要监控应用的地址
Delay:刷新的频率 默认是2秒(2000ms)
Title:应用的标题
1.5、被监控的应用,改造上一篇搭建的badger-spring-cloud-openfeign-hystrix
应用;新增actuator
监控的jar包
<!-- actuator监控信息 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
yaml配置文件中,新增
management:
endpoints:
web:
exposure:
include: '*'
新增这个配置,是开放所有的actuator
监控路径,springboot2.x之后,只暴露了info和health两个节点,并且actuator
监控的路由,加了/actuator路由
部分actuator
监控的配置类如下org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties
@ConfigurationProperties(prefix = "management.endpoints.web")
public class WebEndpointProperties {
private final Exposure exposure = new Exposure();
/**
* Base path for Web endpoints. Relative to server.servlet.context-path or
* management.server.servlet.context-path if management.server.port is configured.
*/
private String basePath = "/actuator";
/**
* Mapping between endpoint IDs and the path that should expose them.
*/
private final Map<String, String> pathMapping = new LinkedHashMap<>();
public Exposure getExposure() {
return this.exposure;
}
yaml全部配置如下
server:
port: 7300
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
spring:
application:
name: badger-spring-cloud-openfeign-hystrix
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: '*'
2、项目启动,演示测试
启动步骤如下:
- 启动eureka的服务,端口8761;
- 修改yaml的配置文件,启动端口为7000,启动服务的提供者badger-spring-cloud-api;
- 启动服务的消费者badger-spring-cloud-openfeign-hystrix,端口为7300;
调用openfeign的服务 http://localhost:7300/feign/demo;
我的地址是-->172.16.2.50:7001
说明项目正常;
在Hystrix Dashboard的页面里,输入地址
http://localhost:7300/actuator/hystrix.stream
点击监监控的按钮,如图所示
点击结果
通过浏览器,不断刷新 http://localhost:7300/feign/demo 可以看到监控的页面,不断的在变化
实心圆:请求的频率越高,流量越大,圆也越大;圆的颜色,也表示应用的健康程度,健康度从绿色、黄色、橙色、红色递减;
曲线:就一段时间里,请求的频次
其他的指标:上图中,红色框框住的部分,根据颜色来对应的,例如图中红框里的的数字37
对应的颜色Success
;表示成功的请求;
具体代码信息,可以查看《码云》