本文介绍: 【Springcloud篇】学习笔记四(九章):Hystrix—服务降级、熔断、限流

第九章_Hystrix服务降级

1.Hystrix简介

1.1分布式系统面临的问题

服务雪崩

image-20230921113253083

1.2Hystrix是什么

image-20230921113430280

1.3Hystrix能干嘛

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

1.4官网资料

官网资料:https://github.com/Netflix/Hystrix/wiki/How-To-Use

Hystrix官宣,停更进维:https://github.com/Netflix/Hystrix

被动修复bugs、不再接受合并请求、不再发布新版本

2.Hystrix重要概念

2.1服务降级

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

哪些情况会出发降级?

  • 程序运行异常
  • 超时
  • 服务熔断触发服务降级
  • 线程池/信号量打满也会导致服务降级

2.2服务熔断

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

就是保险丝

2.3服务限流

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

3.Hystrix案例

3.1构建Hystrix8001模块

  • 模块:cloud-provider-hystrix-payment8001
(1)pom
 	<dependencies>
        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
 
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </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>
        <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
(2)yml

image-20230921115631518

(3)主启动

image-20230921121015800

(4)业务类
①service层

image-20230921121039988

②controller层

image-20230921121104526

(5)正常测试

以上述为根基平台,从正确—>错误—降级熔断—>恢复

3.2高并发测试

(1)Jmeter测试参数

使用jmeter工具创建2w个线程进行测试

image-20230921123529354

image-20230921124321796

再来一个正常的访问:http://localhost:8001/payment/hystrix/ok/3

(2)演示结果

ok和timeout都在转圈圈

原因:

image-20230921124438659

(3)结论

image-20230921124504724

3.3消费端80服务加入

新建模块:cloud-consumer-feign-hystrix-order80

(1)pom
	<dependencies>
        <!-- hystrix -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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>
        <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <groupId>springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
(2)yml

image-20230921130323403

(3)主启动

image-20230921130330385

(4)业务类
①service层

image-20230921130340891

②controller层

image-20230921130352975

(5)正常测试

地址:

localhost/consumer/payment/hystrix/timeout/3

地址:localhost/consumer/payment/hystrix/ok/3

3.4消费端80加入高并发测试

4.服务降级

4.1故障现象以及导致原因

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

4.2如何解决?解决的要求

  • 超时导致服务器变慢(转圈):超时不再等待
  • 出错(宕机或程序运行出错):出错要有兜底
    解决:

解决:

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

4.3添加8001的fallback方法

(1)制造两个不同的异常
  • int age = 10/0; 计算异常
  • 我们能接受3秒钟,它运行5秒钟,超时异常
(2)添加fallback方法

image-20230921172302028

(3)主启动激活服务降级

image-20230921172230960

(4)测试

两个异常,均报这个温馨提示

image-20230921172148153

4.4添加80的fallback方法

上面的案例是服务端降级,现在我们服务端处理3s,然后返回。但是消费端等1s就等不住了,这时候就需要消费端也有降级方法。

80的降级,原理是一样的,上面的 @HystrixCommand 降级可以放在服务端,也可以放在消费端。但@HystrixCommand一般放在消费端

(1)yml配置feign支持hystrix

image-20230921173606134

(2)主启动添加@EnableHystrix

image-20230921173523760

(3)对80服务进行服务降级

image-20230921174509253

(4)测试

image-20230921174602076

4.5@EnableHystrix和@EnableCircuitBreaker区别

image-20230921174822758

4.6全局兜底 @DefaultProperties和降低耦合度

(1)目前存在的问题
  • 每个业务方法对应一个兜底的方法,代码膨胀
  • fallback方法和业务逻辑方法混在一起,混乱
(2)使用全局兜底

image-20230921175419854

(3)降低耦合度

服务降级:客户端调用服务端,碰上服务端宕机或者关闭

注意:本次案例服务降级是在客户端80实现完成的,与服务端8001没有关系,

只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦

  • 修改cloud-consumer-feign-hystrix-order80

    f根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理

image-20230922083328994

在@FeignClient注解添加fallback处理类

image-20230922083419507

  • 注意:要在yml开启feign对hystrix的支持

    image-20230922083516585

  • 测试:

    image-20230922083527664

    • 手动停止8001服务后:

    image-20230922083123149

  • 题外话:测试时候开启服务的顺序十分重要,首先开启Eureka7001服务,再开启80服务,最后开启8001服务,不然会出现测试失败,连接超时等问题

5.服务熔断

5.1理论知识

(1)服务熔断

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

过程:服务降级—>进而熔断—>恢复调用链路

(2)熔断机制

熔断机制是应对雪崩效应的一种微服务链路保护机制。

当扇出链路的某个微服务出错不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息,当检测到该节点微服务调用响应正常后,恢复调用链路。

在Spring Cloud框架里,熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是5秒内20次调用失败,就会启动熔断机制。

熔断机制的注解是@HystrixCommand

简单的电路:闭合就是通路,请求可以通过,断开是断路,请求无法通过,半开半闭是恢复操作,先放一部分请求通过,如果请成功,就继续闭合允许更多请求通过,若失败,则断开使更少请求通过

5.2实操

对8001服务进行修改

(1)service层

image-20230922090727446

(2)controller层

image-20230922090801746

(3)测试
断路器的作用:当断路器开启之后,即使立刻有正确的数据访问,也不会立刻显示正确的内容,而会慢慢试试是否正确

连续10次测试负数的情况,再试正数的情况

image-20230922090621274

5.3小结

(1)熔断类型

image-20230922091105375

(2)断路器起作用的情况

image-20230922091321251

(3)断路器开启或者关闭的条件

image-20230922091454152

(4)断路器打开之后

image-20230922091525194

(5)ALL配置
@HystrixCommand(fallbackMethod = "str_fallbackMethod",
        groupKey = "strGroupCommand",
        commandKey = "strCommand",
        threadPoolKey = "strThreadPool",
        commandProperties = {
                // 设置隔离策略,THREAD 表示线程池 SEMAPHORE:信号池隔离
                @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
                // 当隔离策略选择信号池隔离的时候,用来设置信号池的大小(最大并发数)
                @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "10"),
                // 配置命令执行的超时时间
                @HystrixProperty(name = "execution.isolation.thread.timeoutinMilliseconds", value = "10"),
                // 是否启用超时时间
                @HystrixProperty(name = "execution.timeout.enabled", value = "true"),
                // 执行超时的时候是否中断
                @HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout", value = "true"),
                // 执行被取消的时候是否中断
                @HystrixProperty(name = "execution.isolation.thread.interruptOnCancel", value = "true"),
                // 允许回调方法执行的最大并发数
                @HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "10"),
                // 服务降级是否启用,是否执行回调函数
                @HystrixProperty(name = "fallback.enabled", value = "true"),
                // 是否启用断路器
                @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
                // 该属性用来设置在滚动时间窗中,断路器熔断的最小请求数。例如,默认该值为 20 的时候,
                // 如果滚动时间窗(默认10秒)内仅收到了19个请求, 即使这19个请求都失败了,断路器也不会打开。
                @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
                // 该属性用来设置在滚动时间窗中,表示在滚动时间窗中,在请求数量超过
                // circuitBreaker.requestVolumeThreshold 的情况下,如果错误请求数的百分比超过50,
                // 就把断路器设置为 "打开" 状态,否则就设置为 "关闭" 状态。
                @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"),
                // 该属性用来设置当断路器打开之后的休眠时间窗。 休眠时间窗结束之后,
                // 会将断路器置为 "半开" 状态,尝试熔断的请求命令,如果依然失败就将断路器继续设置为 "打开" 状态,
                // 如果成功就设置为 "关闭" 状态。
                @HystrixProperty(name = "circuitBreaker.sleepWindowinMilliseconds", value = "5000"),
                // 断路器强制打开
                @HystrixProperty(name = "circuitBreaker.forceOpen", value = "false"),
                // 断路器强制关闭
                @HystrixProperty(name = "circuitBreaker.forceClosed", value = "false"),
                // 滚动时间窗设置,该时间用于断路器判断健康度时需要收集信息的持续时间
                @HystrixProperty(name = "metrics.rollingStats.timeinMilliseconds", value = "10000"),
                // 该属性用来设置滚动时间窗统计指标信息时划分"桶"的数量,断路器在收集指标信息的时候会根据
                // 设置的时间窗长度拆分成多个 "桶" 来累计各度量值,每个"桶"记录了一段时间内的采集指标。
                // 比如 10 秒内拆分成 10 个"桶"收集这样,所以 timeinMilliseconds 必须能被 numBuckets 整除。否则会抛异常
                @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "10"),
                // 该属性用来设置对命令执行的延迟是否使用百分位数来跟踪和计算。如果设置为 false, 那么所有的概要统计都将返回 -1。
                @HystrixProperty(name = "metrics.rollingPercentile.enabled", value = "false"),
                // 该属性用来设置百分位统计的滚动窗口的持续时间,单位为毫秒。
                @HystrixProperty(name = "metrics.rollingPercentile.timeInMilliseconds", value = "60000"),
                // 该属性用来设置百分位统计滚动窗口中使用 “ 桶 ”的数量。
                @HystrixProperty(name = "metrics.rollingPercentile.numBuckets", value = "60000"),
                // 该属性用来设置在执行过程中每个 “桶” 中保留的最大执行次数。如果在滚动时间窗内发生超过该设定值的执行次数,
                // 就从最初的位置开始重写。例如,将该值设置为100, 滚动窗口为10秒,若在10秒内一个 “桶 ”中发生了500次执行,
                // 那么该 “桶” 中只保留 最后的100次执行的统计。另外,增加该值的大小将会增加内存量的消耗,并增加排序百分位数所需的计算时间。
                @HystrixProperty(name = "metrics.rollingPercentile.bucketSize", value = "100"),
                // 该属性用来设置采集影响断路器状态的健康快照(请求的成功、 错误百分比)的间隔等待时间。
                @HystrixProperty(name = "metrics.healthSnapshot.intervalinMilliseconds", value = "500"),
                // 是否开启请求缓存
                @HystrixProperty(name = "requestCache.enabled", value = "true"),
                // HystrixCommand的执行和事件是否打印日志到 HystrixRequestLog 中
                @HystrixProperty(name = "requestLog.enabled", value = "true"),
        },
        threadPoolProperties = {
                // 该参数用来设置执行命令线程池的核心线程数,该值也就是命令执行的最大并发量
                @HystrixProperty(name = "coreSize", value = "10"),
                // 该参数用来设置线程池的最大队列大小。当设置为 -1 时,线程池将使用 SynchronousQueue 实现的队列,
                // 否则将使用 LinkedBlockingQueue 实现的队列。
                @HystrixProperty(name = "maxQueueSize", value = "-1"),
                // 该参数用来为队列设置拒绝阈值。 通过该参数, 即使队列没有达到最大值也能拒绝请求。
                // 该参数主要是对 LinkedBlockingQueue 队列的补充,因为 LinkedBlockingQueue
                // 队列不能动态修改它的对象大小,而通过该属性就可以调整拒绝请求的队列大小了。
                @HystrixProperty(name = "queueSizeRejectionThreshold", value = "5"),
        }

}

6.服务限流

后面高级篇讲解alibaba的Sentinel

7.Hystrix工作流程

Hystrix中文说明网:

Hystrix使用入门手册(中文) – 简书 (jianshu.com)

Hystrix官网,以及工作过程(需要翻墙)

https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2FNetflix%2FHystrix%2Fwiki%2FHow-it-Works

Hystrix的工作流图:

img

8.服务监控HystrixDashboard

8.1概述

8.2新建模块9001仪表盘

(1)pom
	<dependencies>
        <!-- hystrix Dashboard-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>

        <!-- 常规 jar 包 -->
        <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>
        <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

(2)yml

image-20230922094423464

(3)主启动

image-20230922094434807

(4)测试

image-20230922093602507

image-20230922093204284

8.3用9001监控8001

修改完记得重新启动8001服务

(1)修改8001
①确保8001有加actuator依赖

image-20230922094601550

②主启动添加以下内容

image-20230922094755034

(2)监控测试

启动7001eureka,再启动服务端8001

9001监控8001,填写监控地址:http://localhost:8001/hystrix.stream

时间写2000ms,Title为T3

  • 访问正确数据,断路器关闭

image-20230922100550914

  • 访问错误数据,断路器开启

image-20230922100813967

(3)监控窗口数据

image-20230922100856462

原文地址:https://blog.csdn.net/weixin_61440595/article/details/135942521

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_65885.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注