天天看点

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

一、引入 服务降级、熔断

1.1 问题 与 解决

1.2 服务降级 与 服务熔断

(1) 服务降级

(2)服务熔断

(3)服务降级 和 服务熔断 的区别

二、服务降级、熔断 -- Hystrix

2.1 什么是 Hystrix ?

2.2 使用 JMeter 模拟超时故障发生

(1)什么是 JMeter ?

(2)说明

(3)定义接口

(4)启动服务,并使用 JMeter 测试

(5)超时故障

2.3 Hystrix 实现服务降级

(1)服务降级使用场景

(2)在服务提供者 上实现服务降级(超时自动降级)

(3)配置默认服务降级方法

2.4 OpenFeign 实现服务降级

(1)说明

(2)配置 OpenFeign 基本代码环境

(3)OpenFeign 实现服务降级

2.5 Hystrix 实现服务熔断

(2)添加接口配置服务熔断

(3)直接访问该服务测试一下(使用 JMeter 测试亦可)。

2.6 Hystrix Dashboard

(1)Dashboard

(2)使用

2.7 Dashboard 错误解决(Unable to connect to Command Metric Stream.)

(1)错误

(2)错误一与解决:

(3)错误二与解决:

三、服务降级、熔断 -- Sentinel

3.1 什么是 Sentinel?

(1)什么是 Sentinel?

(2)Sentinel 组成

(3)Hystrix 与 Sentinel 区别

3.2 基本模块构建

(2)下载并启动 sentinel-dashboard。

(3)基本模块 eureka_client_sentinel_producer_8010 创建

3.3 Sentinel Dashboard 使用 -- 流控规则

(1)什么是流量控制(flow control)?

(2)流控规则基本参数

(3)演示 -- 直接、快速失败。

(4)演示 -- 关联、快速失败。

(5)演示 -- 直接、Wram Up。

(6)演示 -- 直接、排队等待

3.4 @SentinelResource 注解

(1)@SentinelResource 注解

(2)@SentinelResource 使用举例

3.5 Sentinel Dashboard 使用 -- 降级规则、系统规则(系统自适应限流)

(1)熔断降级

(2)演示 -- 异常比例

(3)系统规则

3.6 OpenFeign 整合 Sentinel(引入 sentinel 依赖需要注意版本问题)

(2)整合 Sentinel

SpringCloud (一)-- 从单体架构到微服务架构、代码拆分(maven 聚合)

SpringCloud (二)-- 服务注册中心 Eureka、Zookeeper、Consul、Nacos

SpringCloud (三)-- 服务调用、负载均衡 Ribbon、OpenFeign

SpringCloud (四)-- 服务降级、熔断 Hystrix、Sentinel

SpringCloud (五)-- 配置中心 Config、消息总线 Bus、链路追踪 Sleuth、配置中心 Nacos

SpringCloud (六)-- 注册中心与配置中心 Nacos、网关 Gateway

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

二、服务降级、熔断 – Hystrix

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

在 eureka_client_producer_8001 中定义一个新接口 testTimeout()。

  在 eureka_client_consumer_9001 中定义一个新接口 调用 testTimeout()。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

并发度低时:

  先访问 /consumer/user/testTimeout,再访问 /consumer/user/get/{id} 可以瞬间返回结果。

注:

  看页面的刷新按钮。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

并发度高时:

  使用 JMeter 模拟 200 个线程,循环 100 次,访问 /consumer/user/testTimeout。

  此时再访问 /consumer/user/get/{id} 时,不能瞬间返回结果(等待一段时间)。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

前面已经演示了高并发情况下可能出现超时等待情况,而若 业务执行时间过长 或者 服务调用设置了超时时间,那么当访问被阻塞时,将有可能引起故障。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

服务降级 目的是 防止 服务雪崩,本质也就是在 服务调用 出问题时,应该如何处理。

在 eureka_client_producer_8001 代码基础上进行补充。

Step1:

  引入 hystrix 依赖。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step2:

  通过 @HystrixCommand 注解 编写 服务降级策略。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step3:

  在启动类上添加 @EnableCircuitBreaker 注解,开启服务降级、熔断。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step4:

  运行测试(此处演示的是 超时自动降级)。

  此处定义接口超时时间为 1.5 秒,模拟 0.5 秒业务处理时间,使用 JMeter 压测该接口时,与上面演示的类似,会出现请求超时的情况,而一旦请求超时,则会触发 fallbackMethod 方法,直接返回数据,而不会持续等待。

如下图所示。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

通过上面简单演示可以完成 服务降级,但是存在一个问题,如果为每一个接口都绑定一个 fallbackMethod,那么代码将非常冗余。

  通过 @DefaultProperties 注解 定义一个默认的 defaultFallback 方法,接口异常时调用默认的方法,并仅对特殊的接口进行单独处理,从而减少代码冗余。

如下,新增一个 运行时异常,访问接口时,将会调用 globalFallBackMethod() 方法。

而前面特殊定义的 testTimeout 超时后,仍调用 testTimeout_reserve_case() 方法。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  创建模块 eureka_openfeign_client_consumer_9007。

  修改父工程 与 当前工程 pom.xml 文件。

  修改配置类。

  在启动类上添加 @EnableFeignClients 注解。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  使用 @FeignClient 编写服务调用。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  编写 controller,并进行测试 openfeign 是否能成功访问服务。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  在配置文件中,开启服务降级。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  定义一个实现类,实现 服务调用的接口。

  @Component 注解不要忘了,启动时可能会报错。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  未添加 @Component 注解,启动会报下面的错误。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  在 @FeignClient 注解上,通过 fallback 参数指定上面定义的实现类。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  简单测试一下。

  当服务提供者 宕机时,此时服务调用失败,将会执行 实现类中的逻辑。

  而服务提供者正常提供服务时,由于上面已经在 服务提供者 处配置了 服务降级,则执行 服务提供者的服务降级策略。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

在 eureka_client_producer_8001 中新增一个接口,并配置服务熔断逻辑。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

如下图所示,当请求失败达到一定比率,将会开启断路器。

  一段时间后,尝试恢复服务调用,关闭断路器。

Hystrix 提供了图形化的监控工具(Hystrix Dashboard)进行准实时的调用监控,其可以持续的记录通过 Hystrix 发送的请求执行信息,并以图形、统计报表的形式呈现给用户。

  SpringCloud 对其进行了整合,导入相关依赖即可。

  引入依赖(hystrix-dashboard 以及 actuator)。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  在启动类上添加 @EnableHystrixDashboard 注解,开启 Dashboard。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  启动服务后,访问 http://localhost:8001/hystrix,填写需要监控的地址即可。

  开启监控后,访问配置了 @HystrixCommand 注解的服务时,将会触发监控。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

使用 Dashboard 最常见的错误就是 Unable to connect to Command Metric Stream。

  根据控制台打印的 日志进行相关配置即可解决此错误。

  错误效果如下图所示,

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

三、服务降级、熔断 – Sentinel

Sentinel 主要特性如下(图片来源于网络:)

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step1:下载 sentinel-dashboard。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step2:命令行启动 jar 包。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  修改 父工程、当前工程 pom.xml 文件,并引入相关依赖。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  修改配置文件,配置 dashboard 信息。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  编写测试 controller,进行测试。

  启动 eureka_server_7000、以及当前服务 ,测试一下。

  即使服务启动,Sentinel Dashboard 也是空白的,需要调用一下当前服务的接口,其相关信息才会出现在 Sentinel 中。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

如下图所示,1 秒刷新一次是正常返回的结果,而 1 秒刷新多次后,将会输出默认的错误信息。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

如下图所示,正常访问 A 是没问题的,但是 B 在 1 秒内多次刷新后,A 将会输出默认出错信息。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

如下图所示,开始 QPS 较小,刷新容易报错,3 秒后,QPS 恢复原值,刷新不容易报错。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

如下图所示,快速刷新页面时,请求将会排队等待执行,超时后将会报错。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

@SentinelResource 可以等同于 Hystrix 中的 @HystrixCommand 注解进行理解。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  访问 testBlockHandler(),测试 blockHandler 执行回调函数。

  如下,给 testBlockHandler 添加流控规则,当 QPS 大于 1 时,将进行限流,此时将会执行 blockHandler 指定的回调函数。

  正常访问 testBlockHandler() 时,sentinel dashboard 会监控到两个资源名,此处应选择 @SentinelResource 注解中 value 定义的资源名,并配置 流控、降级 规则。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  访问 testFallback(),测试 fallback 执行回调函数。

  当 id 小于等于 10 时,正常调用。

  当 id 大于 10 时,抛出异常后被 fallback 接收并执行回调函数。

  同样,设置 流控规则,QPS 大于 1 时,限流,但由于 blockHandler 参数指定的回调方法参数 与 原方法不同,所以该回调函数不生效(空白)。

  限流、降级 等异常 执行的是 blockHandler 指定的回调函数。

  而其他异常 执行的是 fallback 指定的回调函数。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  访问 testDefaultFallback(),测试 defaultFallback 执行回调函数。

  与上例类似,只是此处 blockHandler 回调函数参数 与 原方法相同,可以调用成功。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

熔断降级相关概念,前面在 Hystrix 已经介绍了。

  此处仅演示 Sentinel 降级操作。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

在上面 testDefaultFallback() 基础上,添加 降级 规则,演示 异常比例 降级。

  删除添加的流控规则,并指定 降级规则。

  当降级发生时,将会触发 blockHandler 指定的回调方法。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step1:

  在 eureka_client_consumer_9001 基础上引入 依赖。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  修改配置文件。开启 Sentinel 对 Feign 的支持。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

  编写 controller。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step5:

  在启动类上添加 @EnableFeignClients 注解,开启 feign 功能。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel

Step6:

  测试。

  给 testDefaultFallback() 添加流控规则,QPS 大于 1 时将限流。

  QPS 小于等于 1 时,正常访问。

  若远程服务断开后,访问 testDefaultFallback() 将失败,从而执行本地添加的逻辑。

4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel
4.SpringCloud -- 服务降级、熔断 Hystrix、Sentinel