微服务调用之间难免会出现各种问题,某个微服务不可用就得服务降级,否则会拖垮整个系统
Hystrix是一个分布式系统的延迟和容错的开源库,用来提高分布式系统的弹性
1.创建生产者模块
2.导入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
58
59
60
61<dependencies> <!--这个是hystrix依赖,剩下的都是之前常规的--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>com.lyg.springcloud</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>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</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> </dependency> </dependencies>
1.服务降级
一。先从服务提供者自己找问题
设置自身调用超时时间峰值,峰值内可以正常运行,超过峰值需要有兜底方法处理,作为服务降级fallback
在服务提供者的业务类上添加注解,添加一个兜底方法;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/** * @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"),相当于在配置文件中加了超时3秒的配置 * 异常 */ @Override @HystrixCommand(fallbackMethod = "payment_TimeoutHandler",commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") }) public String payment_Timeout(Integer id) { // int a = 1/0; try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池:"+Thread.currentThread().getName()+" payment_Timeout_id:"+id; } public String payment_TimeoutHandler(Integer id) { return "线程池:"+Thread.currentThread().getName()+" payment_TimeoutHandler_id:"+id; }
主启动类加注解
@EnableHystrix
不管是运行报错还是系统繁忙,都可以兜底
二.服务消费者端处理降级
消费方主启动类加开启hystrix注解
@EnableHystrix
调用者添加兜底方法,添加注解
测试
如果兜底方法都写到业务类中,会造成代码混乱,所以我们需要兜底方法和业务类分开,在feign调用时来处理
创建一个类实现feign调用接口
(1).实现FallbackFactory工厂方法的方式
添加配置
1
2
3
4
5
6
7
8
9
10feign: circuitbreaker: enabled: true # 开启 feign 的断路器 hystrix: command: default: fallback: enabled: true # 开启服务降级
创建工厂方法的实现类
在feign调用接口上加注解
(2).服务降级兜底类直接实现feign调用接口
在feign调用接口添加注解
服务熔断
当服务被请求失败次数太多次的时候导致跳闸,熔断器会进入打开状态,即便是正确的请求也会不会范湖正确的数据,如果请求失败次数慢慢减少服务有会慢慢自动恢复正常状态熔断器会恢复到半开状态,知直到慢慢恢复正常
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20// 服务熔断================ @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")// 失败率达到多少后跳闸 }) @Override public String paymentCircuitBreaker(Integer id){ if (id<0){ throw new RuntimeException("....id 不能为负数"); } String simpleUUID = IdUtil.simpleUUID(); return Thread.currentThread().getName()+"t"+"调用成功,流水号:"+simpleUUID; } public String paymentCircuitBreaker_fallback(Integer id){ return "id...不能为负数"+id; }
所有配置
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73@HystrixCommand(fallbackMethod = "str_fal1backMethod", groupKey = "strGroupCommand", commandKey = "strCommand", threadPoolKey = "strThreadPoo1", 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"), @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")}) ———————————————— 版权声明:本文为CSDN博主「两米以下皆凡人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_43509535/article/details/113799835
hystrix流程图
Hystrix流程说明:
1:每次调用创建一个新的HystrixCommand,把依赖调用封装在run()方法中.
2:执行execute()/queue做同步或异步调用.
3:判断熔断器(circuit-breaker)是否打开,如果打开跳到步骤8,进行降级策略,如果 关闭进入步骤.
4:判断线程池/队列/信号量是否跑满,如果跑满进入降级步骤8,否则继续后续骤.
5:调用HystrixCommand的run方法.运行依赖逻辑
5a:依赖逻辑调用超时,进入步骤8.
6:判断逻辑是否调用成功
6a:返回成功调用结果
6b:调用出错,进入步骤8.
7:计算熔断器状态,所有的运行状态(成功, 失败, 拒绝,超时)上报给熔断器,用于统计从而判断熔断器状态.
8:getFallback()降级逻辑.以下四种情况将触发getFallback调用:
(1):run()方法抛出非HystrixBadRequestException异常。
(2):run()方法调用超时
(3):熔断器开启拦截调用
(4):线程池/队列/信号量是否跑满
8a:没有实现getFallback的Command将直接抛出异常
8b:fallback降级逻辑调用成功直接返回
8c:降级逻辑调用失败抛出异常
9:返回执行成功结果
————————————————
版权声明:本文为CSDN博主「dxp33」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/douxingpeng1/article/details/117318929
最后
以上就是花痴世界最近收集整理的关于2021-11-17springcloud学习之服务降级/熔断Hystrix断路器(目前框架已不更新)的全部内容,更多相关2021-11-17springcloud学习之服务降级/熔断Hystrix断路器(目前框架已不更新)内容请搜索靠谱客的其他文章。
发表评论 取消回复