断路器监控Hystrix Dashboard
- 1. 概述
- 2. 步骤
-
- 2.1 新建工程
- 2.2 Pom引入
- 2.3 配置文件application
- 2.4 启动类
- 2.5 查看结果
- 3. 指标说明
1. 概述
Hystrix Dashboard是Hystrix的仪表盘组件,主要用来实时监控Hystrix的各项指标信息,通过界面反馈的信息可以快速发现系统中存在的问题。
2. 步骤
2.1 新建工程
新建一个maven的module工程,名称为hystrix-dashboard,父亲pom为springcloud。
2.2 Pom引入
引入actuator、netflix-hystrix和netflix-hystrix-dashboard的jar,Dashboard服务是个独立的结点,不需要配置eureka信息,但是本例子提供一个rest服务,所以引入netflix-eureka-client和web的jar,pom内容如下:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.lin</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.lin</groupId>
<artifactId>hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hystrix-dashboard</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<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>
</dependencies>
</project>
2.3 配置文件application
建立一个resources文件夹,在其下面新建一个application.yml文件,配置如下:
server:
port: 8770
spring:
application:
name: service-hystrixdashboard
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
cors:
allowed-origins: "*"
allowed-methods: "*"
2.4 启动类
新建一个启动类ServiceHystrixDashboardApplication.java,加入注解@EnableHystrix、@EnableHystrixDashboard、@EnableCircuitBreaker用于断路器功能,同时注册到eureka中。代码如下:
package org.hystrix.dashboard;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceHystrixDashboardApplication {
/**
* 访问地址 http://localhost:8770/actuator/hystrix.stream
* @param args
*/
public static void main(String[] args) {
SpringApplication.run( ServiceHystrixDashboardApplication.class, args );
}
@Value("${server.port}")
String port;
@RequestMapping("/hystrixdashboard")
@HystrixCommand(fallbackMethod = "hystrixDashboardError")
public String home(@RequestParam(value = "name", defaultValue = "linwu") String name) {
return "hi " + name + " ,i am from port:" + port;
}
public String hystrixDashboardError(String name) {
return "hi,"+name+",sorry,error!";
}
}
2.5 查看结果
1) 访问服务http://localhost:8770/hystrixdashboard
2) 打开网页http://localhost:8770/actuator/hystrix.stream
可以看到数据传输的过程,如下图:
3) 打开网页http://localhost:8770/hystrix
输入参数如下图所示
可以看出图形的结果,如下图
3. 指标说明
本文参考文献:https://blog.csdn.net/forezp/article/details/70148833/