spring cloud
This commit is contained in:
@ -1,55 +1,42 @@
|
||||
# spring-cloud-hystrix-turbine
|
||||
|
||||
## 目录<br/>
|
||||
<a href="#一hystrix-简介">一、hystrix 简介</a><br/>
|
||||
<a href="#11-熔断器">1.1 熔断器</a><br/>
|
||||
<a href="#12-hystrix-工作机制">1.2 hystrix 工作机制</a><br/>
|
||||
<a href="#二项目结构">二、项目结构</a><br/>
|
||||
<a href="#三整合-hystrix-以consumer模块为例">三、整合 hystrix (以consumer模块为例)</a><br/>
|
||||
<a href="#四使用turbine-实现聚合监控">四、使用turbine 实现聚合监控</a><br/>
|
||||
<a href="#五整合过程中可能出现的问题">五、整合过程中可能出现的问题</a><br/>
|
||||
<a href="#51-无法访问监控页面">5.1 无法访问监控页面</a><br/>
|
||||
<a href="#52-页面一直loading-或者访问端点页面一直出现ping">5.2 页面一直loading 或者访问端点页面一直出现ping</a><br/>
|
||||
## 正文<br/>
|
||||
# Spring-Cloud-Hystrix-Turbine
|
||||
|
||||
|
||||
## 一、hystrix 简介
|
||||
## 一、简介
|
||||
|
||||
#### 1.1 熔断器
|
||||
### 1.1 Spring Cloud Hystrix
|
||||
|
||||
在分布式系统中,由于服务之间相互的依赖调用,如果一个服务单元发生了故障就有可能导致故障蔓延至整个系统,从而衍生出一系列的保护机制,断路器就是其中之一。
|
||||
|
||||
断路器可以在服务单元发生故障的时候,及时切断与服务单元的连接,避免资源被长时间占用。spring cloud hystrix 组件实现了断路器、线程隔离等一系列基本功能,并具有服务降级、服务熔断、请求缓存、请求合并以及服务监控等配套功能。
|
||||
断路器可以在服务单元发生故障的时候,及时切断与服务单元的连接,避免资源被长时间占用。Spring Cloud Hystrix 组件实现了断路器、线程隔离等一系列基本功能,并具有服务降级、服务熔断、请求缓存、请求合并以及服务监控等配套功能。
|
||||
|
||||
|
||||
|
||||
#### 1.2 hystrix 工作机制
|
||||
### 1.2 熔断器工作机制
|
||||
|
||||
- 当一个服务的处理请求的失败次数低于阈值时候,熔断器处于关闭状态,服务正常;
|
||||
- 当一个服务的处理请求的失败次数大于阈值时候,熔断器开启,这时候所有的请求都会执行快速失败,是不会去调用实际的服务的;
|
||||
- 当熔断器处于打开状态的一段时间后,熔断器处于半打开状态,这时候一定数量的请求回去调用实际的服务,如果调用成功,则代表服务可用了,熔断器关闭;如果还是失败,则代表服务还是不可用,熔断器继续关闭。
|
||||
- 当一个服务处理请求失败的次数低于阈值时,熔断器处于关闭状态,服务正常;
|
||||
- 当一个服务处理请求失败的次数大于阈值时,熔断器开启,这时所有的请求都会执行快速失败,而不会去调用实际的服务;
|
||||
- 当熔断器处于打开状态的一段时间后,熔断器处于半打开状态,这时候一定数量的请求回去调用实际的服务,如果调用成功,则代表服务可用了,熔断器关闭;如果还是失败,则代表服务还是不可用,熔断器继续打开。
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/circuitbreaker.png"/> </div>
|
||||
|
||||
## 二、项目结构
|
||||
|
||||
[spring-cloud-ribbon](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-ribbon) 用例已经实现通过 ribbon+restTemplate 实现服务间的调用,本用例在其基础上进行 hystrix 的整合。
|
||||
[spring-cloud-ribbon](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-cloud/spring-cloud-ribbon) 用例已经实现通过 Ribbon + RestTemplate 实现服务间的调用,本用例在其基础上进行 Hystrix 的整合:
|
||||
|
||||
+ common: 公共的接口和实体类;
|
||||
+ consumer: 服务的消费者,采用 RestTemplate 调用产品服务;
|
||||
+ producer:服务的提供者;
|
||||
+ eureka: 注册中心;
|
||||
+ turbine:多个熔断器的聚合监控。
|
||||
+ **common**:公共的接口和实体类;
|
||||
+ **consumer**:服务的消费者,采用 RestTemplate 调用产品服务;
|
||||
+ **producer**:服务的提供者;
|
||||
+ **eureka**: 注册中心;
|
||||
+ **turbine**:多个熔断器的聚合监控。
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/spring-cloud-hystrix.png"/> </div>
|
||||
|
||||
## 三、整合 Hystrix
|
||||
|
||||
这里以 consumer 模块为例,说明其整合步骤:
|
||||
|
||||
## 三、整合 hystrix (以consumer模块为例)
|
||||
### 3.1 引入依赖
|
||||
|
||||
#### 3.1 引入依赖
|
||||
|
||||
hystrix 的仪表盘功能实际上是从[端点](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-actuator) 获取数据,所以需要 actuator starter 开启端点的相关功能。
|
||||
Hystrix 的仪表盘功能实际上是从 [端点](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring-boot/spring-boot-actuator) 获取数据,所以需要引入 actuator starter 开启端点的相关功能:
|
||||
|
||||
```xml
|
||||
<!--hystrix 依赖-->
|
||||
@ -69,7 +56,7 @@ hystrix 的仪表盘功能实际上是从[端点](https://github.com/heibaiying/
|
||||
</dependency>
|
||||
```
|
||||
|
||||
#### 3.2 暴露端点
|
||||
### 3.2 暴露端点
|
||||
|
||||
```java
|
||||
management:
|
||||
@ -80,7 +67,9 @@ management:
|
||||
include: "*"
|
||||
```
|
||||
|
||||
#### 3.3 在启动类上添加注解@EnableHystrix和@EnableHystrixDashboard
|
||||
### 3.3 添加注解
|
||||
|
||||
在启动类上添加注解 @EnableHystrix 和 @EnableHystrixDashboard:
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@ -95,7 +84,9 @@ public class ConsumerApplication {
|
||||
}
|
||||
```
|
||||
|
||||
#### 3.4 使用 @HystrixCommand 定义失败回退的方法
|
||||
### 3.4 服务降级
|
||||
|
||||
使用 @HystrixCommand 定义失败回退的方法:
|
||||
|
||||
```java
|
||||
@HystrixCommand(fallbackMethod = "queryProductsFail")
|
||||
@ -138,9 +129,9 @@ public List<Product> queryProductsFail() {
|
||||
</html>
|
||||
```
|
||||
|
||||
#### 3.5 模拟熔断
|
||||
### 3.5 模拟熔断
|
||||
|
||||
这里被调用方采用线程休眠的方式模拟服务超时,Hystrix 默认超时时间为 2s,调用远程服务时候超过这个时间,会触发熔断。
|
||||
这里被调用方采用线程休眠的方式模拟服务超时,Hystrix 默认超时时间为 2s,调用远程服务时候超过这个时间,会触发熔断:
|
||||
|
||||
```java
|
||||
public List<Product> queryAllProducts() {
|
||||
@ -155,41 +146,38 @@ public List<Product> queryAllProducts() {
|
||||
}
|
||||
```
|
||||
|
||||
3.5 启动服务,访问 http://localhost:8030/sell/products ,多次刷新查看熔断情况
|
||||
### 3.5 测试熔断
|
||||
|
||||
启动服务,访问 http://localhost:8030/sell/products ,多次刷新查看熔断情况:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/hystrix-8030.png"/> </div>
|
||||
### 3.7 控制台
|
||||
|
||||
#### 3.5 启动服务,访问 localhost:8030/hystrix
|
||||
启动服务后,可以访问 localhost:8030/hystrix ,依次输出 http://localhost:8030/actuator/hystrix.stream(监控地址) ,2000(延迟时间),title 可以任意填写,进入Hystrix 监控台。
|
||||
|
||||
依次输出 http://localhost:8030/actuator/hystrix.stream(监控地址) ,2000(延迟时间),title 可以任意填写,进入监控台。
|
||||
|
||||
需要注意的是在 spring cloud Finchley.SR2,监控地址中都是有/actuator 的,因为在 spring boot 2.x 的所有端点(包括自定义端点)都是暴露在这个路径下,在启动时候的控制台输出的日志可以查看到所有暴露端点的映射。
|
||||
在 Spring Cloud Finchley.SR2 中,监控地址需要以 `/actuator` 开头的,因为在 Spring Boot 2.x 中所有端点(包括自定义端点)都是暴露在这个路径下,可以通过控制台的启动日志来验证这一点。
|
||||
|
||||
**登录页面**:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/hystrix-single-login.png"/> </div>
|
||||
|
||||
**监控页面**:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/hystrix-8030-login.png"/> </div>
|
||||
|
||||
**关于各个参数的说明参见[官方 wiki](https://github.com/Netflix-Skunkworks/hystrix-dashboard/wiki) 提供的图**:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dashboard.png"/> </div>
|
||||
|
||||
|
||||
|
||||
## 四、聚合监控
|
||||
|
||||
|
||||
## 四、使用turbine 实现聚合监控
|
||||
|
||||
单体监控和聚合监控:
|
||||
如果你想要聚合监控不同服务单元下的多个断路器,可以使用 Turbine 来实现。单体监控和聚合监控的区别如下:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/dashboard-direct-vs-turbine-640.png"/> </div>
|
||||
|
||||
### 4.1 导入依赖
|
||||
|
||||
|
||||
#### 4.1 创建turbine模块,导入依赖
|
||||
创建 Turbine 模块,导入以下依赖:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
@ -244,7 +232,9 @@ public List<Product> queryAllProducts() {
|
||||
|
||||
|
||||
|
||||
#### 4.2 指定注册中心地址和聚合的项目,这里我们监控 consumer,producer 两个项目
|
||||
### 4.2 项目配置
|
||||
|
||||
指定注册中心地址和聚合的项目,这里我们监控 consumer,producer 两个项目:
|
||||
|
||||
```java
|
||||
server:
|
||||
@ -269,7 +259,9 @@ turbine:
|
||||
|
||||
|
||||
|
||||
#### 4.3 在启动类上添加注解
|
||||
### 4.3 添加注解
|
||||
|
||||
在启动类上添加注解:
|
||||
|
||||
```java
|
||||
@SpringBootApplication
|
||||
@ -288,51 +280,49 @@ public class TurbineApplication {
|
||||
|
||||
|
||||
|
||||
#### 4.4 依次启动eureka、producer、consumer、turbine四个项目
|
||||
### 4.4 启动项目
|
||||
|
||||
在 localhost:8030/hystrix 或者 localhost:8030/hystrix(consumer 和 producer 都集成了 hystrix) 页面输入 http://localhost:8040/turbine.stream,查看断路器聚合信息
|
||||
依次启动 eureka、producer、consumer、turbine 四个项目,因为 consumer 和 producer 都集成了 Hystrix ,所以可以在 localhost:8020/hystrix 或者 8030/hystrix 页面输入 http://localhost:8040/turbine.stream 来查看断路器聚合信息:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/hystrix-cluster-login.png"/> </div>
|
||||
|
||||
**显示了不同服务单元(consumer,producer)的多个断路器信息:**
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/hystrix-cluster.png"/> </div>
|
||||
## 五、常见问题
|
||||
|
||||
## 五、整合过程中可能出现的问题
|
||||
在整合过程中可能出现的一些问题如下:
|
||||
|
||||
#### 5.1 无法访问监控页面
|
||||
### 5.1 无法访问监控页面
|
||||
|
||||
1. 一般是端点链接输入不对,在 F 版本的 spring cloud 中,输入监控的端点链接是 http://localhost:8030/actuator/hystrix.stream ,中间是有/actuator/(之前版本的没有/actuator/)
|
||||
一般是端点链接输入不对,在 F 版本的 Spring Cloud 中,输入监控的端点链接是 http://localhost:8030/actuator/hystrix.stream ,中间是有 `/actuator/`(之前版本的没有)。其次是可能没有暴露端点,暴露端点有两种方式,一种是我们在上文中提到的基于配置的方式:
|
||||
|
||||
2. 没有暴露端点链接,暴露端点链接有两种方式,一种是我们在上文中提到的基于配置的方式
|
||||
```yaml
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
# 需要开启 hystrix.stream 端点的暴露 这样才能获取到监控信息 * 代表开启所有可监控端点
|
||||
include: "*"
|
||||
```
|
||||
|
||||
```yaml
|
||||
management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
# 需要开启 hystrix.stream 端点的暴露 这样才能获取到监控信息 * 代表开启所有可监控端点
|
||||
include: "*"
|
||||
```
|
||||
另一种基于代码的方式,示例如下:
|
||||
|
||||
第二种方式是基于代码的方式,如下:
|
||||
```java
|
||||
@Bean
|
||||
public ServletRegistrationBean getServlet() {
|
||||
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
|
||||
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
|
||||
registrationBean.setLoadOnStartup(1);
|
||||
registrationBean.addUrlMappings("/actuator/hystrix.stream");
|
||||
registrationBean.setName("HystrixMetricsStreamServlet");
|
||||
return registrationBean;
|
||||
}
|
||||
```
|
||||
|
||||
```java
|
||||
@Bean
|
||||
public ServletRegistrationBean getServlet() {
|
||||
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
|
||||
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
|
||||
registrationBean.setLoadOnStartup(1);
|
||||
registrationBean.addUrlMappings("/actuator/hystrix.stream");
|
||||
registrationBean.setName("HystrixMetricsStreamServlet");
|
||||
return registrationBean;
|
||||
}
|
||||
```
|
||||
这两种方式二选一即可,就算是采用代码的方式,还是建议将地址设置为 /actuator/hystrix.stream,而不是原来的 hystrix.stream,因为 Turbine 默认也是从 /actuator/hystrix.stream 去获取信息。
|
||||
|
||||
这两种方式二选一即可,就算是采用代码的方式,还是建议将地址设置为/actuator/hystrix.stream,而不是原来的 hystrix.stream,因为 turbine 默认也是从/actuator/hystrix.stream 去获取信息。
|
||||
### 5.2 页面一直 Loading 或者访问端点页面一直出现 Ping
|
||||
|
||||
#### 5.2 页面一直loading 或者访问端点页面一直出现ping
|
||||
|
||||
这种情况是熔断器所在的方法没有被调用,所以没有产生监控数据,不是整合问题,这时候调用一下熔断器所在方法即可。
|
||||
这种情况是熔断器所在的方法没有被调用,所以没有产生监控数据,不是整合问题,这时候调用一下熔断器所在方法即可:
|
||||
|
||||
<div align="center"> <img src="https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/hystrix-loading.png"/> </div>
|
||||
|
Reference in New Issue
Block a user