博客学习参考视频

一、Hystrix 断路器

① 概述

1.分布式系统面临的问题

image-20201016002519013

image-20201016002530999

2.是什么

image-20201016002559663

3.能干嘛

  • 服务降级
  • 服务熔断
  • 接近实时的监控

4.官 网 资 料

https://github.com/Netflix/Hystrix/wiki/How-To-Use

5.Hystrix 官宣,停更进维

https://github.com/Netflix/Hystrix

image-20201016003154348

  • 不在修复 Bug
  • 不在接收合并请求
  • 不在发布新版本

② Hystrix 重要概念

1.服务降级

服务器忙, 请稍候再试, 不让客户端等待并立刻返回一个友好提示, fallback

哪些情况会触发降级: 1 程序运行异常、 2 超时、 3 服务熔断触发服务降级 、4 线程池/信号量打满也会导致服务降级

2.服务熔断

​ 类比保险丝达到最大服务访问后, 直接拒绝访问, 拉闸限电, 然后调用服务降级的方法并返回友好提示,就是保险丝。

服务的降级->进而熔断->恢复调用链路

3.服务限流

​ 秒杀高并发等操作, 严禁一窝蜂的过来拥挤, 大家排队, 一秒钟 N 个, 有序进行

③ hystrix 案例

1.构建

新建 cloud-provider-hystrix-payment8001

POM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?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">
<parent>
<artifactId>clould</artifactId>
<groupId>com.oy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-provider-hystrix-payment8001</artifactId>

<dependencies>
<!--新增 hystrix-->
<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-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.oy</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

YML

1
2
3
4
5
6
7
8
server:
port: 8001
eureka:
client:
register-with-eureka: true #表示不向注册中心注册自己
fetch-registry: true #表名自己就是注册中心,职责维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

主启动类

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}

业务类

​ Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Service
public class PaymentService {

// 成功
public String paymentInfo_OK(Integer id){
return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~";
}


// 失败
public String paymentInfo_TimeOut(Integer id){
int timeNumber = 3;
try {
TimeUnit.SECONDS.sleep(timeNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}

return "线程池:" + Thread.currentThread().getName()+ "paymentInfo_TimeOut,id:" + id +"\t" +"┭┮﹏┭┮"+"耗时(秒)" + timeNumber;
}
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@RestController
@Slf4j
public class PaymentController {

@Resource
private PaymentService paymentService;

@Value("${server.port}")
private String serverPort;

@GetMapping(value = "/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_OK(id);
log.info("*************result:" + result);

return result;
}

@GetMapping(value = "/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_TimeOut(id);
log.info("******result:" + result);
return result;
}
}

测试:

image-20201016152506946

  • 上述 module 均 OK,以上述为根基平台,从正确->错误->降级熔断->恢复

2.高并发测试

Jmeter 压测测试

​ 开 启 Jmeter , 来 20000 个 并 发 压 死 8001 , 20000 个 请 求 都 去 访 问 paymentInfo_TimeOut 服务。

image-20201016154613777

3.Jmeter 压测结论

​ 上面还是服务提供者 8001 自己测试, 假如此时外部的消费者 80 也来访问,那消费者只能干等, 最终导致消费端 80 不满意, 服务端 8001 直接被拖死

4.看热闹不嫌弃事大, 80 新建加入

新建 cloud-consumer-feign-hystrix-order80

image-20201016155321248

POM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?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">
<parent>
<artifactId>clould</artifactId>
<groupId>com.oy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-consumer-feign-hystrix-order80</artifactId>

<dependencies>
<!--新增 hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.oy</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</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.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

YML

1
2
3
4
5
6
7
8
server:
port: 80
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

主启动类

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaClient
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}

业务类

PaymentHystrixService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.oy.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
* @Author OY
* @Date 2020/10/16
*/
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {

/**
* 正常访问
*
* @param id
* @return
*/
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);

/**
* 超时访问
*
* @param id
* @return
*/
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@RestController
@Slf4j
public class OrderHyrixController {

@Resource
private PaymentHystrixService paymentHystrixService;

/**
* 正常访问
*
* @param id
* @return
*/
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("*****result:" + result);
return result;
}

/**
* 超时访问
*
* @param id
* @return
*/
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
log.info("*****result:" + result);
return result;

}
}

正常测试

image-20201016163217022

image-20201016163241381

注意:测试这个时有可能会报超时错误,如果出现错误只需要在配置文件中配置

1
2
3
4
5
ribbon:
# 指的是建立连接所用的时间,适用于网络状态正常的情况下,两端连接所用的时间
ReadTimeout: 5000
# 指的是建立连接后从服务器读取到可用资源所用的时间
ConnectTimeout: 5000

image-20201016163421515

高并发测试

image-20201016163648968

​ 消费者 80, 要么转圈圈等待、 要么消费端报超时错误

5.故障现象和导致原因

  • 8001 同一层次的接口服务被困死,因为 tomcat 线程里面的工作线程已经被挤占完毕
  • 80 此时调用 8001,客户端访问的响应缓慢,转圈圈

6.上诉结论

​ 正因为有上述故障或不佳表现, 才有我们的降级/容错/限流等技术诞生

7.如何解决?解决的要求

  • 超时导致服务器变慢(转圈) : 超时不再等待

  • 出错(宕机或程序运行出错) : 出错要有兜底

解决:

  1. 对方服务(8001) 超时了, 调用者(80) 不能一直卡死等待,必须有服务降级。
  2. 对方服务(8001) down 机了, 调用者(80) 不能一直卡死等待, 必须有服务降级。
  3. 对方服务(8001) OK, 调用者(80) 自己出故障或有自我要求(自己的等待时间小于服务提供者) , 自己处理降级

④ 服务降级

1.降低配置: @HystrixCommand

2.8001 先从自身找问题

​ 置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,做服务降级 fallback

3.8001fallback

业务类启用

image-20201016212902803

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Service
public class PaymentService {

// 成功
public String paymentInfo_OK(Integer id){
return "线程池:" + Thread.currentThread().getName() + "paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~";
}

/**
* 超时访问,演示降级
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
@HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds",value="3000")
})
public String paymentInfo_TimeOut(Integer id){
int timeNumber = 5;
try {
TimeUnit.SECONDS.sleep(timeNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}

return "线程池:" + Thread.currentThread().getName()+ "paymentInfo_TimeOut,id:" + id +"\t" +"┭┮﹏┭┮"+"耗时(秒)" + timeNumber;
}

public String paymentInfo_TimeOutHandler(Integer id){
return "o(╥﹏╥)o 调用支付接口超时异常:\t" + "\t当前线程池名字:" + Thread.currentThread().getName();
}
}

image-20201016213119295

@HystrixCommand报异常后如何处理

一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbckMethod调用类中的指定方法

图示

image-20201016214851342

主启动类激活: 添加新注解**@EnableCircuitBreaker**

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class, args);
}
}

4.80fallback

80 订单微服务, 也可以更好的保护自己, 自己也依样画葫芦进行客户端降级保护。

题外话

切记我 们 自 己 配 置 过 的 热 部 署 方 式 对 java 代 码 的 改 动 明 显 , 但对@HystrixCommand 内属性的修改建议重启微服务

YML

1
2
3
feign:
hystrix:
enabled: true # 如果处理自身的容错就开启。开启方式与生产端不一样。

image-20201016215256041

主启动: @EnableHystrix

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class, args);
}
}

业务类

1
2
3
4
5
6
7
8
9
10
11
12
13
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "15000")
})
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
log.info("*****result:" + result);
return result;
}

public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}

5.目前方法

  • 每个业务方法对应一个兜底的方法,代码膨胀
  • 统一和自定义的需要分开

6.解决问题

每个方法配置一个? ? ? 膨胀

  1. feign 接口系列
  2. @DefaultProperties(defaultFallback = “”)

image-20201016221217405

说明

image-20201016221245143

controller 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHyrixController {

@Resource
private PaymentHystrixService paymentHystrixService;

/**
* 正常访问
*
* @param id
* @return
*/
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_OK(id);
log.info("*****result:" + result);
return result;
}

/**
* 超时访问
*
* @param id
* @return
*/
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
log.info("*****result:" + result);
return result;
}

// 兜底方法
public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}

/**
* 全局fallback
* @return
*/
public String payment_Global_FallbackMethod() {
return "Global异常处理信息,请稍后重试.o(╥﹏╥)o";
}
}

**和业务逻辑混一起? ? ? 混乱 **

  1. 服务降级,客户端去调用服务端,碰上服务器宕机或关闭

  2. 本次案例服务案例级处理是在客户端 80 实现完成的,与服务端 8001 没有关系,只需要为 Feign 客户端定义的接口添加一个服务降级处理的实现类即可实现解耦。

  3. 未来我们要面对的异常

    运行、超时、宕机

  4. 再看我们的业务类 PaymentController

image-20201016230720317

  1. 修改 cloud-consumer-feign-hystrix-order80
  2. 根 据 cloud-consumer-feign-hystrix-order80 已 经 有 的 PaymentHystrixService 接 口 , 重 新 新 建 一 个 类
    (PaymentFallbackService) 实现该接口, 统一为接口里面的方法进行异常处理 。
  3. PaymentFallbackService 类 实 现 PaymentFeignClientService 接口
1
2
3
4
5
6
7
8
9
10
11
12
@Component
public class PaymentFallbackService implements PaymentHystrixService{
@Override
public String paymentInfo_OK(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
}

@Override
public String paymentInfo_TimeOut(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬ _┬)";
}
}
  1. YML
1
2
3
feign:
hystrix:
enabled: true # 如果处理自身的容错就开启。开启方式与生产端不一样。
  1. PaymentFeignClientService 接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {

/**
* 正常访问
*
* @param id
* @return
*/
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);

/**
* 超时访问
*
* @param id
* @return
*/
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}
  1. 测试

image-20201016231719706

  • 故意关闭微服务 8001

image-20201016231758903

​ 客户端自己调用提升: 此时服务端 provider 已经 down 了, 但是我们做了服务降级处理, 让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器 。

⑤ 服务熔断

1.断路器

​ 一句话就是家里保险丝

2.熔断是什么

image-20201016232022777

大神论文:https://martinfowler.com/bliki/CircuitBreaker.html

3.实操

修改 cloud-provider-hystrix-payment8001

PaymentService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    @HystrixCommand(fallbackMethod =
"paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间范围
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
if (id < 0){
throw new RuntimeException("*****id 不能负数");
}
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"\t"+"调用成功,流水号: "+serialNumber;
}

public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
return "id 不能负数, 请稍候再试,(┬_┬)/~~ id: " +id;
}

why 配置这些参数:

image-20201016232453956

PaymentController

1
2
3
4
5
6
@GetMapping("/payment/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
String result = paymentService.paymentCircuitBreaker(id);
log.info("*******result:"+result);
return result;
}

测试

  1. 自测 cloud-provider-hystrix-payment8001

image-20201016232734315

  1. 正确: http://localhost:8001/payment/circuit/1

    image-20201016233026906

  2. 错误: http://localhost:8001/payment/circuit/-1

image-20201016233053779

  1. 一次正确一次错误 trytry
  2. 重点测试: 多次错误,然后慢慢正确, 发现刚开始不满足条件,就算是正确的访问地址也不能进行访问, 需要慢慢的恢复链路

4.原理(小总结)

大神结论

image-20201016233342518

熔断类型

  1. 熔断打开

    请求不再进行调用当前服务, 内部设置时钟一般为 MTTR(平均故障处理时间),当打开时长达到所设时钟则进入熔断状态 。

  2. 熔断关闭

    熔断关闭不会对服务进行熔断。

  3. 熔断半开

    部分请求根据规则调用当前服务, 如果请求成功且符合规则则认为当前服务恢复正常, 关闭熔断 。

官网断路器流程图

  1. 官网步骤

image-20201016233656920

  1. 断路器在什么情况下开始起作用

image-20201016233720377

  1. 断路器开启或者关闭的条件
  • 当满足一定阀值的时候(默认 10 秒内超过 20 个请求次数)

  • 当失败率达到一定的时候(默认 10 秒内超过 50%请求失败)

  • 到达以上阀值, 断路器将会开启

  • 当开启的时候, 所有请求都不会进行转发

  • 一段时间之后(默认是 5 秒), 这个时候断路器是半开状态, 会让其中一个请求进行转发。 如果成功, 断路器会关闭, 若失败, 继续开启。 重复 4 和 5 。

  1. 断路器打开之后

    image-20201016234107457

  2. All 配置

image-20201016234250739

image-20201016234229443

image-20201016234319121

image-20201016234330093

⑥ 服务限流

参考我后面的关于高级篇的 alibaba 的 Sentinel 说明

⑦ hystrix 工作流程

1.hystrix 工作流程

https://github.com/Netflix/Hystrix/wiki/How-it-Works

2.hystrix 工作流程

**官网图例 : **

image-20201017152337133

步骤说明:

image-20201017152658751

⑧ 服务监控 hystrixDashboard

1.概述

image-20201017153028883

2.仪表盘 9001

新建 cloud-consumer-hystrix-dashboard9001

image-20201017153155398

POM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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">
<parent>
<artifactId>clould</artifactId>
<groupId>com.oy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cloud-consumer-hystrix-dashboard9001</artifactId>

<dependencies><!--新增 hystrix dashboard-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

YML

1
2
server:
port: 9001

HystrixDashboardMain9001+新注解**@EnableHystrixDashboard**

1
2
3
4
5
6
7
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardMain9001.class, args);
}
}

所有 Provider 微服务提供类(8001/8002/8003) 都需要监控依赖配置

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

启动 cloud-consumer-hystrix-dashboard9001 该微服务后续将监控微服务 8001

http://localhost:9001/hystrix

image-20201017155956437

3.断路器演示(服务监控 hystrixDashboard)

修改 cloud-provider-hystrix-payment8001

注意:新版本 Hystrix 需要在主启动 PaymentHystrixMain8001 中指定监控路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 此配置是为了服务监控而配置,与服务容错本身无观,springCloud 升级之后的坑
* ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream
* 只要在自己的项目中配置上下面的servlet即可
* @return
*/
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}

image-20201017161411381

  • Unable to connect to Command Metric Stream.
  • 404

监控测试

  • 启动 1 个 eureka 或者 3 个 eureka 集群均可

  • 观察监控窗口

    9001 监控 8001

image-20201017162106329

监控结果,成功

image-20201017162411201

监控结果, 失败

image-20201017162501189

  • 如何看

7 色 1 圈

image-20201017162751506

image-20201017162643326

1 线

image-20201017162702560

整图说明

image-20201017162830940

整图说明 2

image-20201017162858611

搞懂一个才能看懂复杂的

image-20201017163035515