概述
一、简介
Spring Cloud Hystrix Dashboard是一个可以监控HystrixCommand的可视化图形界面,由于某种原因,如网络延迟、服务故障等,这时候可以借助dashboard提供的可视化界面监控各个Hystrix执行的成功率、调用成功数、失败数量、最近十分钟的流量图等等,根据这些数据我们就可以进行错误排查以及进行服务的优化等。Hystrix Dashboard只能对单个服务进行监控,实际项目中,服务通常集群部署,这时候可以借助Turbine进行多个服务的监控。
二、准备工程
hystrix-server:eureka服务注册中心,端口1111,本文不做介绍
hystrix-dashboard1:进行hystrix监控的服务1 端口3333
hystrix-dashboard2:进行hystrix监控的服务2 端口5555
hystrix-turbine:turbine集群监控项目 端口4444
接下来先讲讲单个服务的监控:
三、新建hystrix-dashboard1项目
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud.wsh</groupId>
<artifactId>springcloud_hystrix_dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_hystrix_dashboard</name>
<description>Spring Cloud Hystrx Dashboard仪表盘</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Camden.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-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>
四、启动类加上@EnableHystrixDashboard注解
/**
* @Description: 单个服务的Hystrix监控
* @Author: WeiShiHuai
* @Date: 2018/9/13 15:30
*/
//@SpringBootApplication
//@EnableCircuitBreaker
//@EnableDiscoveryClient
@SpringCloudApplication
@EnableHystrixDashboard
public class SpringcloudHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixDashboardApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
五、新建HystrixController
提供一个/hello的接口,并且使用@HystrixCommand注解修饰,指定Hystrix服务降级处理方法
/**
* @Title: HystrixController
* @ProjectName springcloud_hystrix_dashboard
* @Description: 测试
* @Author WeiShiHuai
* @Date 2018/9/13 15:48
*/
@RestController
public class HystrixController {
private static Logger logger = LoggerFactory.getLogger(HystrixController.class);
@RequestMapping("/hello")
@HystrixCommand(fallbackMethod = "hiError")
public String hello(@RequestParam String name) {
return "hello, " + name;
}
public String helloError(String name) {
return "hello, " + name + ",sorry,error!";
}
}
六、配置文件
server:
port: 3333
spring:
application:
name: hystrix-dashboard1
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
七、启动eureka-server以及hystrix-dashboard1项目
首先我们先访问http://localhost:3333/hello?name=weixiaohuai,如下图:
可以看到,接口已经调用成功,接着我们访问:http://localhost:3333/hystrix,进入hystrix监控界面:如下图
此时我们在监控地址栏输入:http://localhost:3333/hystrix.stream,如下图
点击Monitor Stream,进入到Dashboard仪表盘实时监控页面,这个时候我们需要请求一下接口http://localhost:3333/hello?name=weixiaohuai,这样仪表盘才能监控到请求的一些数据信息。
监控界面实时展示了两分钟内接口请求的流量、请求成功百分比、成功数、失败数、超时数等等。至此,单服务的Hystrix监控已经实现。接下来看一下集群监控Turbine的实现方法:
八、新建hystrix-dashboard2工程
端口5555,只是端口与hystrix-dashboard1工程不一致,其他与hystrix-dashboard1都一样,下面直接贴代码:
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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud.wsh</groupId>
<artifactId>springcloud_hystrix_dashboard2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_hystrix_dashboard2</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Camden.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-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>
配置文件:
server:
port: 5555
spring:
application:
name: hystrix-dashboard2
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
启动类:
@SpringCloudApplication
@EnableHystrixDashboard
public class SpringcloudHystrixDashboard2Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixDashboard2Application.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
HystrixController2:
/**
* @Title: HystrixController
* @ProjectName springcloud_hystrix_dashboard
* @Description: 测试
* @Author WeiShiHuai
* @Date 2018/9/13 15:48
*/
@RestController
public class HystrixController2 {
private static Logger logger = LoggerFactory.getLogger(HystrixController2.class);
@RequestMapping("/hello")
@HystrixCommand(fallbackMethod = "helloError")
public String hello(@RequestParam String name) {
return "hello, " + name;
}
public String helloError(String name) {
return "hello, " + name + ",sorry,error!";
}
}
九、新建hystrix-turbine工程
端口4444,注意要引入turbine的依赖:
<?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>
<groupId>com.springcloud.wsh</groupId>
<artifactId>springcloud_turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcloud_turbine</name>
<description>Spring Cloud Turbine集群监控</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Camden.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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-turbine</artifactId>
</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>
十、启动类加上@EnableTurbine注解开启Turbine集群监控功能
@SpringBootApplication
@EnableTurbine
public class SpringcloudTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudTurbineApplication.class, args);
}
}
十一、turbine的配置文件
server:
port: 4444
spring:
application:
name: hystrix-turbine
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
turbine:
# 配置注册到Eureka中的serviceId列表,表明监控哪些服务
app-config: hystrix-dashboard1,hystrix-dashboard2
cluster-name-expression: new String("default")
# 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称
# 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default
# 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
#aggregator:
#clusterConfig: default # 指定聚合哪些集群,多个使用","分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问
注意:通过turbine.appConfig指定我们需要监控哪些服务,这里对应注册到Eureka中的名称,即application-name(serviceId)
十二、启动项目eureka-server,hystrix-dashboard1,hystrix-dashhystrix2以及hystrix-turbine
浏览器访问:http://localhost:3333/hello?name=weixiaohuai、http://localhost:5555/hello?name=helloworld
我们随机访问一下这两个服务的接口,接着我们浏览器输入http://localhost:3333/hystrix进入到hystrix监控页面(http://localhost:5555/hystrix也可以),在监控地址栏输入:http://localhost:4444/turbine.stream
可以看到,turbine已经实现了对HystrixController、HystrixController2的集群监控。
十三、总结
其实,在实际项目中,这种实时监控有点耗性能,通常采用消息中间件如RabbitMQ等,我们接口调用把Hystrix的一些信息收集到RabbitMQ中,然后Turbine从RabbitMQ中获取监控的数据。本文主要讲解dashboard和turbine的基本使用方法,对于一些高级用法,还有待研究。。。
最后
以上就是现实小猫咪为你收集整理的Spring Cloud Hystrix Dashboard仪表盘 和 Turbine集群监控 (学习总结)的全部内容,希望文章能够帮你解决Spring Cloud Hystrix Dashboard仪表盘 和 Turbine集群监控 (学习总结)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复