一、Feign项目Hystrix自带的监控
在feign项目pom.xml 添加:
<!-- 1,使用 Hystrix的模块 hystrix-metrics-event-stream,就可将这些监控的指标信息以 text/event-stream的格式暴露给外部系统。 spring-cloud-starter-netflix-hystrix包含该 模块,在此基础上,只须为项目添加 spring-boot-starter-actuator依赖,就可使 用/hystrix.stream端点获得Hystrix的监控信息了。 2, Feign项目的Hystrix监控-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 健康度jar--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
注意之前Feign项目整合Hystrix时pom依赖是这样的:
<!-- 虽然Feign已经依赖Hystrix-core 但是想要监控还是不够的 要引入整个Hystrix--> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Feign项目启动类需要添加注解 @EnableCircuitBreaker
1 package com.tuling.cloud.study;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
7 import org.springframework.cloud.netflix.feign.EnableFeignClients;
8
9 @EnableDiscoveryClient
10 @SpringBootApplication
11 @EnableFeignClients
12 @EnableCircuitBreaker //这样就可以使用/hystrix.stream端点监控Hystrix了
13 public class ConsumerOrderApplication_07_stream {
14
15 public static void main(String[] args) {
16 SpringApplication.run(ConsumerOrderApplication_07_stream.class, args);
17 }
18
19 }
启动之后访问Feign 项目 : http://localhost:9020/hystrix.stream 是这样的:
这种监控数据没法看,对吧?
二、使用Hystrix Dashboard可视化监控数据
dashBoard 是一个独立的项目:
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.tuling.cloud</groupId>
6 <artifactId>microservice-hystrix-dashboard</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <packaging>jar</packaging>
9 <name>07-ms-hystrix-dashboard</name>
10
11 <!-- 引入spring boot的依赖 -->
12 <parent>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-parent</artifactId>
15 <version>1.5.9.RELEASE</version>
16 </parent>
17
18 <properties>
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <java.version>1.8</java.version>
21 </properties>
22
23 <dependencies> <!-- dashBoard包-->
24 <dependency>
25 <groupId>org.springframework.cloud</groupId>
26 <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
27 </dependency>
28 </dependencies>
29
30 <!-- 引入spring cloud的依赖 -->
31 <dependencyManagement>
32 <dependencies>
33 <dependency>
34 <groupId>org.springframework.cloud</groupId>
35 <artifactId>spring-cloud-dependencies</artifactId>
36 <version>Edgware.RELEASE</version>
37 <type>pom</type>
38 <scope>import</scope>
39 </dependency>
40 </dependencies>
41 </dependencyManagement>
42
43 <!-- 添加spring-boot的maven插件 -->
44 <build>
45 <plugins>
46 <plugin>
47 <groupId>org.springframework.boot</groupId>
48 <artifactId>spring-boot-maven-plugin</artifactId>
49 </plugin>
50 </plugins>
51 </build>
52 </project>
启动类:
1 package com.jiagoushi.cloud.study;
2
3 import org.springframework.boot.SpringApplication;
4 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
6
7 @SpringBootApplication
8 @EnableHystrixDashboard //DashBoard 支持
9 public class HystrixDashboardApplication {
10
11 public static void main(String[] args) {
12 SpringApplication.run(HystrixDashboardApplication.class, args);
13 }
14 }
applciation.yml:
server:
port: 8030
启动之后访问:http://localhost:8030/hystrix
在监控的界面有两个重要的图形信息:一个实心圆和一条曲线。
实心圆:1、通过颜色的变化代表了实例的健康程度,健康程度从绿色、黄色、橙色、
红色递减。
2、通过大小表示请求流量发生变化,流量越大该实心圆就越大。所以可以在大
量的实例中快速发现故障实例和高压实例。
曲线:用来记录2分钟内流浪的相对变化,可以通过它来观察流量的上升和下降趋势
在复杂的分布式系统中,相同服务的节点经常需要部署上百甚至上千个,很多时候,运维人员希望能够把相同服务的节点状态以一个整体集群的形式展现出来,这样可以更好的把握整个系统的状态。 为此,Netflix提供了一个开源项目(Turbine)
来提供把多个hystrix.stream的内容聚合为一个数据源供Dashboard展示。
三、使用Turbine聚合监控数据
Turbine是一个聚合 Hystrix监控数据的工具,它可将所有相关/hystrix.stream端点的数据聚合到一个组合的/turbine.stream中,从而让集群的监控更加方便
Turbine 独立的项目:
pom.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>com.tuling.cloud</groupId>
6 <artifactId>microservice-hystrix-turbine</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <packaging>jar</packaging>
9 <name>07-ms-hystrix-turbine</name>
10
11 <!-- 引入spring boot的依赖 -->
12 <parent>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-parent</artifactId>
15 <version>1.5.9.RELEASE</version>
16 </parent>
17
18 <properties>
19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20 <java.version>1.8</java.version>
21 </properties>
22
23 <dependencies>
24 <!-- turbine 依赖-->
25 <dependency>
26 <groupId>org.springframework.cloud</groupId>
27 <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
28 </dependency>
29 </dependencies>
30
31 <!-- 引入spring cloud的依赖 -->
32 <dependencyManagement>
33 <dependencies>
34 <dependency>
35 <groupId>org.springframework.cloud</groupId>
36 <artifactId>spring-cloud-dependencies</artifactId>
37 <version>Edgware.RELEASE</version>
38 <type>pom</type>
39 <scope>import</scope>
40 </dependency>
41 </dependencies>
42 </dependencyManagement>
43
44 <!-- 添加spring-boot的maven插件 -->
45 <build>
46 <plugins>
47 <plugin>
48 <groupId>org.springframework.boot</groupId>
49 <artifactId>spring-boot-maven-plugin</artifactId>
50 </plugin>
51 </plugins>
52 </build>
53 </project>
application.yml:
1 server:
2 port: 8031
3 spring:
4 application:
5 name: microservice-hystrix-turbine
6 eureka:
7 client:
8 service-url:
9 defaultZone: http://localhost:8761/eureka/
10 instance:
11 prefer-ip-address: true
12
13 #turbine 配置
14 turbine:
15 appConfig: microservice-consumer-order,microservice-consumer-order-feign-hystrix-fallback-stream #微服务名称
16 clusterNameExpression: "'default'"
17
18
19
Turbine 为什么要链接注册中心?
Turbine会在Eureka Server中找到 microservice-consumer-order和 microservice-consumer-order-feign-hystrix-fallback-stream这两个微服务,并聚合两个微服务的监控数据
TurbineApplication 启动类:
package com.jiagoushi.cloud.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableTurbine //开启turbine
public class TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(TurbineApplication.class, args);
}
}
启动Tuibine:
如果我们把user服务关闭了,那么监控就是这样的:
欢迎来群592495675一起学习