优化阅读格式

This commit is contained in:
heibaiying
2019-07-31 17:39:13 +08:00
parent 3020ff8efa
commit 246e5cfca1
55 changed files with 10521 additions and 10482 deletions

View File

@ -19,7 +19,7 @@
在分布式系统中,由于服务之间相互的依赖调用,如果一个服务单元发生了故障就有可能导致故障蔓延至整个系统,从而衍生出一系列的保护机制,断路器就是其中之一。
断路器可以在服务单元发生故障的时候及时切断与服务单元的连接避免资源被长时间占用。spring cloud hystrix组件实现了断路器、线程隔离等一系列基本功能并具有服务降级、服务熔断、请求缓存、请求合并以及服务监控等配套功能。
断路器可以在服务单元发生故障的时候及时切断与服务单元的连接避免资源被长时间占用。spring cloud hystrix 组件实现了断路器、线程隔离等一系列基本功能,并具有服务降级、服务熔断、请求缓存、请求合并以及服务监控等配套功能。
@ -33,10 +33,10 @@
## 二、项目结构
[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调用产品服务
+ consumer: 服务的消费者,采用 RestTemplate 调用产品服务;
+ producer服务的提供者
+ eureka: 注册中心;
+ turbine:多个熔断器的聚合监控。
@ -49,7 +49,7 @@
#### 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 依赖-->
@ -76,7 +76,7 @@ management:
endpoints:
web:
exposure:
# 需要开启hystrix.stream端点的暴露 这样才能获取到监控信息 * 代表开启所有可监控端点
# 需要开启 hystrix.stream 端点的暴露 这样才能获取到监控信息 * 代表开启所有可监控端点
include: "*"
```
@ -140,11 +140,11 @@ public List<Product> queryProductsFail() {
#### 3.5 模拟熔断
这里被调用方采用线程休眠的方式模拟服务超时Hystrix默认超时时间为2s,调用远程服务时候超过这个时间,会触发熔断。
这里被调用方采用线程休眠的方式模拟服务超时Hystrix 默认超时时间为 2s,调用远程服务时候超过这个时间,会触发熔断。
```java
public List<Product> queryAllProducts() {
// hystrix 默认超时是2
// hystrix 默认超时是 2
int i = new Random().nextInt(2500);
try {
Thread.sleep(i);
@ -155,15 +155,15 @@ 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.5 启动服务,访问 localhost:8030/hystrix
依次输出http://localhost:8030/actuator/hystrix.stream监控地址 2000延迟时间title可以任意填写进入监控台。
依次输出 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 的所有端点(包括自定义端点)都是暴露在这个路径下,在启动时候的控制台输出的日志可以查看到所有暴露端点的映射。
**登录页面**
@ -173,7 +173,7 @@ public List<Product> queryAllProducts() {
<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)提供的图**
**关于各个参数的说明参见[官方 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>
@ -290,7 +290,7 @@ public class TurbineApplication {
#### 4.4 依次启动eureka、producer、consumer、turbine四个项目
在 localhost:8030/hystrix或者localhost:8030/hystrixconsumerproducer都集成了hystrix 页面输入http://localhost:8040/turbine.stream查看断路器聚合信息
在 localhost:8030/hystrix 或者 localhost:8030/hystrixconsumerproducer 都集成了 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>
@ -302,7 +302,7 @@ public class TurbineApplication {
#### 5.1 无法访问监控页面
1. 一般是端点链接输入不对,在F版本的spring cloud 中,输入监控的端点链接是 http://localhost:8030/actuator/hystrix.stream ,中间是有/actuator/(之前版本的没有/actuator/
1. 一般是端点链接输入不对,在 F 版本的 spring cloud 中,输入监控的端点链接是 http://localhost:8030/actuator/hystrix.stream ,中间是有/actuator/(之前版本的没有/actuator/
2. 没有暴露端点链接,暴露端点链接有两种方式,一种是我们在上文中提到的基于配置的方式
@ -311,7 +311,7 @@ public class TurbineApplication {
endpoints:
web:
exposure:
# 需要开启hystrix.stream端点的暴露 这样才能获取到监控信息 * 代表开启所有可监控端点
# 需要开启 hystrix.stream 端点的暴露 这样才能获取到监控信息 * 代表开启所有可监控端点
include: "*"
```
@ -329,7 +329,7 @@ public class TurbineApplication {
}
```
这两种方式二选一即可,就算是采用代码的方式,还是建议将地址设置为/actuator/hystrix.stream而不是原来的hystrix.stream因为turbine默认也是从/actuator/hystrix.stream去获取信息。
这两种方式二选一即可,就算是采用代码的方式,还是建议将地址设置为/actuator/hystrix.stream而不是原来的 hystrix.stream因为 turbine 默认也是从/actuator/hystrix.stream 去获取信息。
#### 5.2 页面一直loading 或者访问端点页面一直出现ping